Exemple #1
0
def test_client_should_clear_headers_on_new_instance():
    client = HttpClient()
    client.set_header('SomeHeader', 'SomeValue')

    client = HttpClient()
    client.set_header('OtherHeader', 'SomeValue')

    assert client.get_header('SomeHeader') is None
    assert client.get_header('OtherHeader') == 'SomeValue'
def test_post(monkeypatch):
    response = Response()
    response.status_code = 200
    response._content = b'{"key":555}'
    monkeypatch.setattr('pynubank.utils.http.post', MagicMock(return_value=response))

    client = HttpClient()

    response = client.post('some-url', {})

    assert response['key'] == 555
def test_http_post_handler_throws_exception_on_status_different_of_200(monkeypatch, http_status):
    response = Response()
    response.status_code = http_status
    monkeypatch.setattr('pynubank.utils.http.post', MagicMock(return_value=response))

    client = HttpClient()

    with pytest.raises(NuRequestException) as ex:
        client.post('http://some-url', {})
        assert ex is not None
        assert ex.url == 'http://some-url'
        assert ex.status_code == http_status
        assert ex.response == response
Exemple #4
0
 def __init__(self, client=HttpClient()):
     self._client = client
     self._discovery = Discovery(self._client)
     self._feed_url = None
     self._query_url = None
     self._bills_url = None
     self._customer_url = None
     self._revoke_token_url = None
Exemple #5
0
 def __init__(self, login, password, device_id):
     self.login = login
     self.password = password
     self.device_id = device_id
     self.encrypted_code = None
     self.key1 = self._generate_key()
     self.key2 = self._generate_key()
     discovery = Discovery(HttpClient())
     self.url = discovery.get_app_url('gen_certificate')
Exemple #6
0
def build_discovery() -> Discovery:
    http = HttpClient()
    discovery = Discovery(http)
    return discovery
Exemple #7
0
 def __init__(self):
     self.client = HttpClient()
     self.discovery = Discovery(self.client)
Exemple #8
0
class Nubank:
    feed_url = None
    query_url = None
    bills_url = None

    def __init__(self):
        self.client = HttpClient()
        self.discovery = Discovery(self.client)

    @staticmethod
    def _get_query(query_name):
        root = os.path.abspath(os.path.dirname(__file__))
        gql_file = query_name + '.gql'
        path = os.path.join(root, 'queries', gql_file)
        with open(path) as gql:
            return gql.read()

    def _make_graphql_request(self, graphql_object, variables=None):
        if variables is None:
            variables = {}

        body = {
            'variables': variables,
            'query': self._get_query(graphql_object)
        }

        return self.client.post(self.query_url, json=body)

    def _password_auth(self, cpf: str, password: str):
        payload = {
            "grant_type": "password",
            "login": cpf,
            "password": password,
            "client_id": "other.conta",
            "client_secret": "yQPeLzoHuJzlMMSAjC-LgNUJdUecx8XO"
        }
        return self.client.post(self.discovery.get_url('login'), json=payload)

    def _save_auth_data(self, auth_data: dict) -> None:
        self.client.set_header('Authorization',
                               f'Bearer {auth_data["access_token"]}')
        self.feed_url = auth_data['_links']['events']['href']
        self.query_url = auth_data['_links']['ghostflame']['href']
        self.bills_url = auth_data['_links']['bills_summary']['href']

    def get_qr_code(self) -> Tuple[str, QRCode]:
        content = str(uuid.uuid4())
        qr = QRCode()
        qr.add_data(content)
        return content, qr

    def authenticate_with_qr_code(self, cpf: str, password, uuid: str):
        auth_data = self._password_auth(cpf, password)
        self.client.set_header('Authorization',
                               f'Bearer {auth_data["access_token"]}')

        payload = {'qr_code_id': uuid, 'type': 'login-webapp'}

        response = self.client.post(self.discovery.get_app_url('lift'),
                                    json=payload)

        self._save_auth_data(response)

    def authenticate_with_cert(self, cpf: str, password: str, cert_path: str):
        self.client.set_cert(cert_path)
        url = self.discovery.get_app_url('token')
        payload = {
            'grant_type': 'password',
            'client_id': 'legacy_client_id',
            'client_secret': 'legacy_client_secret',
            'login': cpf,
            'password': password
        }

        response = self.client.post(url, json=payload)

        self._save_auth_data(response)

        return response.get('refresh_token')

    def authenticate_with_refresh_token(self, refresh_token: str,
                                        cert_path: str):
        self.client.set_cert(cert_path)

        url = self.discovery.get_app_url('token')
        payload = {
            'grant_type': 'refresh_token',
            'client_id': 'legacy_client_id',
            'client_secret': 'legacy_client_secret',
            'refresh_token': refresh_token,
        }

        response = self.client.post(url, json=payload)

        self._save_auth_data(response)

    def get_card_feed(self):
        return self.client.get(self.feed_url)

    def get_card_statements(self):
        feed = self.get_card_feed()
        return list(
            filter(lambda x: x['category'] == 'transaction', feed['events']))

    def get_bills(self):
        request = self.client.get(self.bills_url)
        return request['bills']

    def get_bill_details(self, bill):
        return self.client.get(bill['_links']['self']['href'])

    def get_account_feed(self):
        data = self._make_graphql_request('account_feed')
        return data['data']['viewer']['savingsAccount']['feed']

    def get_account_statements(self):
        feed = self.get_account_feed()
        return list(
            filter(lambda x: x['__typename'] in PAYMENT_EVENT_TYPES, feed))

    def get_account_balance(self):
        data = self._make_graphql_request('account_balance')
        return data['data']['viewer']['savingsAccount'][
            'currentSavingsBalance']['netAmount']

    def create_boleto(self, amount: float) -> str:
        customer_id_response = self._make_graphql_request('account_id')
        customer_id = customer_id_response['data']['viewer']['id']

        payload = {"input": {"amount": str(amount), "customerId": customer_id}}

        boleto_response = self._make_graphql_request('create_boleto', payload)

        barcode = boleto_response['data']['createTransferInBoleto']['boleto'][
            'readableBarcode']

        return barcode
Exemple #9
0
 def __init__(self, client=HttpClient()):
     self.client = client
     self.discovery = Discovery(self.client)