Skip to content

Commit a178e9d

Browse files
authored
Merge pull request #2 from Pipoline/basic
Basic functionality
2 parents 7107936 + 7c88953 commit a178e9d

7 files changed

Lines changed: 118 additions & 14 deletions

File tree

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.0.1
2+
current_version = 0.0.2
33
commit = True
44
tag = True
55

cryptopay/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
"""
22
Crypto.com pay wrapper library
33
"""
4+
import os
5+
import requests
6+
from .exceptions import ApiKeyMissingException
47

8+
CRYPTOPAY_SECRET_KEY = os.environ.get('CRYPTOPAY_SECRET_KEY', None)
59

10+
if CRYPTOPAY_SECRET_KEY is None:
11+
raise ApiKeyMissingException("All methods require API key")
612

13+
session = requests.Session()
14+
session.headers['Authorization'] = f"Bearer {CRYPTOPAY_SECRET_KEY}"
715

16+
from .api import Client

cryptopay/api.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,34 @@
22
Crypto.com API module
33
"""
44

5+
from . import session
56

6-
class Client (object):
77

8-
endpoint = 'https://pay.crypto.com'
9-
settings = {}
8+
class Client:
9+
headers = {}
10+
11+
def __init__(self, secret_key=None, api_endpoint_url='https://pay.crypto.com/api', timeout=30,
12+
ssl_verify=True, proxies=None):
13+
self.timeout = timeout
14+
self.api_endpoint_url = api_endpoint_url
15+
self.ssl_verify = ssl_verify
16+
self.proxies = proxies
17+
if secret_key:
18+
session.headers['Authorization'] = f"Bearer {secret_key}"
19+
20+
def _call_api(self, location, method="GET", **kwargs):
21+
if method == "GET":
22+
return session.get(f'{self.api_endpoint_url}/{location}',
23+
verify=self.ssl_verify,
24+
proxies=self.proxies,
25+
timeout=self.timeout
26+
)
27+
28+
def get_payments(self, payment_id=None):
29+
if payment_id:
30+
location = f'payments/{payment_id}'
31+
else:
32+
location = 'payments'
33+
return self._call_api(location)
1034

11-
def __init__(self, settings=None):
12-
self.settings = settings
1335

14-
def login(self):
15-
pass

cryptopay/exceptions.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
3+
class UnauthorizedApiException(Exception):
4+
pass
5+
6+
7+
class ApiKeyMissingException(Exception):
8+
pass
9+
10+
11+
class RequestFailedException(Exception):
12+
pass
13+
14+
15+
class BadRequestException(Exception):
16+
pass
17+
18+
19+
class BadRequestException(Exception):
20+
pass
21+
22+
23+
class TooManyRequestsException(Exception):
24+
pass
25+
26+
27+
class ServerErrorException(Exception):
28+
pass
29+
30+
31+
class ConflictException(Exception):
32+
pass

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="cryptopay",
8-
version="0.0.1",
8+
version="0.0.2",
99
author="Peter Gonda",
1010
author_email="peter@pipoline.com",
1111
description="Crypto.com pay module",

tests/test_api.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1-
from cryptopay.api import Client
1+
import pytest
2+
import os
3+
from importlib import reload
24

5+
import cryptopay
6+
from cryptopay.exceptions import ApiKeyMissingException
7+
from requests import Response
38

4-
class TestApi():
5-
api = Client()
69

7-
def test_login(self):
8-
self.api.login()
10+
@pytest.fixture()
11+
def client():
12+
from cryptopay import Client
13+
return Client()
14+
15+
16+
def test_get_payments(client):
17+
response = client.get_payments()
18+
assert(isinstance(response, Response))
19+
assert response.status_code == 200
20+
21+
22+
def test_get_payments(client):
23+
payment_id = client.get_payments().json()[-1]['id']
24+
response = client.get_payments(payment_id=payment_id)
25+
assert(isinstance(response, Response))
26+
assert response.status_code == 200

tests/test_permissions.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
from importlib import reload
3+
4+
import cryptopay
5+
from cryptopay.exceptions import ApiKeyMissingException
6+
7+
8+
def test_missing_api_key():
9+
tmp = os.environ['CRYPTOPAY_SECRET_KEY']
10+
del os.environ['CRYPTOPAY_SECRET_KEY']
11+
try:
12+
reload(cryptopay)
13+
except ApiKeyMissingException:
14+
pass
15+
16+
os.environ['CRYPTOPAY_SECRET_KEY'] = tmp
17+
reload(cryptopay)
18+
19+
20+
def test_custom_secret_key():
21+
client = cryptopay.Client('custom_secret_key')
22+
assert client
23+
24+
25+

0 commit comments

Comments
 (0)