|
1
|
|
|
#!/usr/bin/python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
1 |
|
import os |
|
4
|
|
|
|
|
5
|
1 |
|
import dotenv |
|
6
|
1 |
|
import pyotp |
|
7
|
1 |
|
import requests |
|
8
|
|
|
|
|
9
|
1 |
|
from kuon.api_response import APIResponse |
|
10
|
1 |
|
from kuon.bitskins.api.exceptions import * |
|
11
|
1 |
|
from kuon.selenium_helper import SeleniumHelper |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
1 |
|
class BitSkins(object): |
|
15
|
1 |
|
_selenium_helper = None |
|
16
|
|
|
|
|
17
|
1 |
|
def __init__(self, api_key: str = None, secret: str = None) -> None: |
|
18
|
|
|
"""Initializing function |
|
19
|
|
|
According to the API documentation (https://bitskins.com/api/python#totp_code) they |
|
20
|
|
|
require OTPs and the API key so we verify the API key and the secret and generate the OTPs with a property |
|
21
|
|
|
|
|
22
|
|
|
:type api_key: str |
|
23
|
|
|
:type secret: str |
|
24
|
|
|
""" |
|
25
|
|
|
dotenv_path = os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir, '.env') |
|
26
|
|
|
dotenv.load_dotenv(dotenv_path) |
|
27
|
|
|
|
|
28
|
|
|
self._api_key = api_key |
|
29
|
|
|
self._secret = secret |
|
30
|
|
|
|
|
31
|
|
|
if not api_key: |
|
32
|
|
|
self._api_key = os.environ.get('BITSKINS_API_KEY') |
|
33
|
|
|
|
|
34
|
|
|
if not secret: |
|
35
|
|
|
self._secret = os.environ.get('BITSKINS_2FA_SECRET') |
|
36
|
|
|
|
|
37
|
|
|
if not self._api_key: |
|
38
|
|
|
raise NoAPIKeyProvidedException('Please provide an API key via .env or as argument') |
|
39
|
|
|
|
|
40
|
|
|
if not self._secret: |
|
41
|
|
|
raise NoSecretProvidedException('Please provide a secret to generate One Time Passwords for the API usage') |
|
42
|
|
|
|
|
43
|
|
|
self._pyotp = pyotp.TOTP(self._secret) |
|
44
|
|
|
self.validate_api_key() |
|
45
|
|
|
|
|
46
|
1 |
|
def api_request(self, api_url: str, params: dict = None, headers: dict = None) -> APIResponse: |
|
47
|
|
|
"""Insert API key and OTP code to the payload and return the parsed response |
|
48
|
|
|
|
|
49
|
|
|
:type api_url: str |
|
50
|
|
|
:type params: dict |
|
51
|
|
|
:type headers: dict |
|
52
|
|
|
:return: |
|
53
|
|
|
""" |
|
54
|
|
|
if headers is None: |
|
55
|
|
|
headers = {} |
|
56
|
|
|
if params is None: |
|
57
|
|
|
params = {} |
|
58
|
|
|
|
|
59
|
|
|
params['api_key'] = self._api_key |
|
60
|
|
|
params['code'] = self.secret |
|
61
|
|
|
|
|
62
|
|
|
link = requests.get(url=api_url, params=params, headers=headers) |
|
63
|
|
|
|
|
64
|
|
|
return APIResponse(link.text) |
|
65
|
|
|
|
|
66
|
1 |
|
def validate_api_key(self) -> None: |
|
67
|
|
|
"""Validate the api key and the 2 FA secret |
|
68
|
|
|
|
|
69
|
|
|
:return: |
|
70
|
|
|
""" |
|
71
|
|
|
api_url = "https://bitskins.com/api/v1/get_account_balance/" |
|
72
|
|
|
response = self.api_request(api_url=api_url) |
|
73
|
|
|
|
|
74
|
|
|
if response.status != "success": |
|
75
|
|
|
raise InvalidOrWrongApiKeyException('Please provide a valid API key and 2 FA secret') |
|
76
|
|
|
|
|
77
|
1 |
|
@property |
|
78
|
1 |
|
def selenium_helper(self) -> SeleniumHelper: |
|
79
|
|
|
"""Use property to make this lazy since it takes 3-4 seconds to load |
|
80
|
|
|
|
|
81
|
|
|
:return: |
|
82
|
|
|
""" |
|
83
|
|
|
if not self._selenium_helper: |
|
84
|
|
|
self._selenium_helper = SeleniumHelper() |
|
85
|
|
|
return self._selenium_helper |
|
86
|
|
|
|
|
87
|
1 |
|
@property |
|
88
|
1 |
|
def secret(self) -> str: |
|
89
|
|
|
"""Getter for One Time Passwords based on the given secret |
|
90
|
|
|
|
|
91
|
|
|
:return: |
|
92
|
|
|
""" |
|
93
|
|
|
return self._pyotp.now() |
|
94
|
|
|
|