Skip to content

Commit

Permalink
Merge pull request #1 from Klynox/development
Browse files Browse the repository at this point in the history
Merge changes from main to development branch
  • Loading branch information
ConfidenceYobo authored Sep 14, 2021
2 parents f99eed3 + 3a0df89 commit 509fe18
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 142 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Klynox

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 0 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ verify_ssl = true
name = "pypi"

[packages]
requests = "==2.26.0"
flake8 = "*"
autopep8 = "*"

Expand Down
107 changes: 0 additions & 107 deletions Pipfile.lock

This file was deleted.

Empty file added README.md
Empty file.
3 changes: 3 additions & 0 deletions coinpayments/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from coinpayments.coinpayments import CoinPayments

name = "coinpayments"
59 changes: 26 additions & 33 deletions apiConfig.py → coinpayments/apiConfig.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import os
import urllib.request
import urllib.parse
import urllib.error
import urllib.request
import urllib.error
import urllib.parse
import hmac
import hashlib
import urllib.parse
import json

import requests

from .errors import (MissingAuthKeyError,
InvalidMethodError, ImproperlyConfigured)
from coinpayments.errors import (MissingAuthKeyError, ImproperlyConfigured)


class ApiConfig:
"""
API configuration for the coinpayments SDK
"""
_CONTENT_TYPE = "application/json"
_CONTENT_TYPE = "application/x-www-form-urlencoded"
_API_END_POINT = "https://www.coinpayments.net/api.php"

def __init__(self, private_key=None, public_key=None, ipn_url=None):
Expand Down Expand Up @@ -65,38 +67,29 @@ def create_hmac(self, **params):
encoded, hashlib.sha512).hexdigest()

def _parse_json(self, response_obj):
parsed_response = response_obj.json()
print(parsed_response)
return response_obj.status_code, parsed_response
response_body = response_obj.read()
response_body_decoded = json.loads(
response_body)
response_body_decoded.update(response_body_decoded['result'])
response_body_decoded.pop('result', None)
return response_body_decoded

def _handle_request(self, method, url, data=None):
"""
Generic function to handle all API url calls
Returns a python tuple of status code, status(bool), message, data
"""
method_map = {
'GET': requests.get,
'POST': requests.post,
'PUT': requests.put,
'DELETE': requests.delete
}

payload = json.dumps(data) if data else data
encoded, sig = self.create_hmac(**data)
self.request_headers['hmac'] = sig
request = method_map.get(method)

if not request:
raise InvalidMethodError(
"Request method not recognised or implemented")

response = request(url, headers=self.request_headers,
data=payload, verify=True)
if response.status_code == 404:
return response.status_code, "The object request cannot be found"

if response.status_code in [200, 201]:
return self._parse_json(response)
else:
# body = response.json()
return response.status_code, "Real error"
req = urllib.request.Request(
url, data=encoded, headers=self.request_headers)

try:
response = urllib.request.urlopen(req)
response_body_decoded = self._parse_json(response)
except urllib.error.HTTPError as e:
response_body = e.read()
response_body_decoded = json.loads(
response_body)
return response_body_decoded
return response_body_decoded
41 changes: 40 additions & 1 deletion coinpayments.py → coinpayments/coinpayments.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .apiConfig import ApiConfig
from coinpayments.apiConfig import ApiConfig


class CoinPayments(ApiConfig):
Expand All @@ -12,6 +12,8 @@ def create_transaction(self, params=None):
if self.ipn_url:
params.update({'ipn_url': self.ipn_url})
params.update({'cmd': 'create_transaction',
'version': 1,
'format': 'json',
'key': self.COINPAYMENTS_PUBLIC_KEY})
url = self._url('')
return self._handle_request('POST', url, params)
Expand All @@ -24,6 +26,8 @@ def get_basic_info(self, params=None):
if params is None:
params = {}
params.update({'cmd': 'get_basic_info',
'version': 1,
'format': 'json',
'key': self.COINPAYMENTS_PUBLIC_KEY})
url = self._url('')
return self._handle_request('POST', url, params)
Expand All @@ -36,6 +40,8 @@ def rates(self, params=None):
if params is None:
params = {}
params.update({'cmd': 'rates',
'version': 1,
'format': 'json',
'key': self.COINPAYMENTS_PUBLIC_KEY})
url = self._url('')
return self._handle_request('POST', url, params)
Expand All @@ -48,6 +54,8 @@ def balances(self, params=None):
if params is None:
params = {}
params.update({'cmd': 'balances',
'version': 1,
'format': 'json',
'key': self.COINPAYMENTS_PUBLIC_KEY})
url = self._url('')
return self._handle_request('POST', url, params)
Expand All @@ -60,6 +68,8 @@ def get_deposit_address(self, params=None):
if params is None:
params = {}
params.update({'cmd': 'get_deposit_address',
'version': 1,
'format': 'json',
'key': self.COINPAYMENTS_PUBLIC_KEY})
url = self._url('')
return self._handle_request('POST', url, params)
Expand All @@ -74,6 +84,8 @@ def get_callback_address(self, params=None):
if self.ipn_url:
params.update({'ipn_url': self.ipn_url})
params.update({'cmd': 'get_callback_address',
'version': 1,
'format': 'json',
'key': self.COINPAYMENTS_PUBLIC_KEY})
url = self._url('')
return self._handle_request('POST', url, params)
Expand All @@ -88,6 +100,8 @@ def create_transfer(self, params=None):
if params is None:
params = {}
params.update({'cmd': 'create_transfer',
'version': 1,
'format': 'json',
'key': self.COINPAYMENTS_PUBLIC_KEY})
url = self._url('')
return self._handle_request('POST', url, params)
Expand All @@ -101,6 +115,8 @@ def create_withdrawal(self, params=None):
if params is None:
params = {}
params.update({'cmd': 'create_withdrawal',
'version': 1,
'format': 'json',
'key': self.COINPAYMENTS_PUBLIC_KEY})
url = self._url('')
return self._handle_request('POST', url, params)
Expand All @@ -113,6 +129,7 @@ def convert_coins(self, params=None):
if params is None:
params = {}
params.update({'cmd': 'convert',
'version': 1,
'key': self.COINPAYMENTS_PUBLIC_KEY})
url = self._url('')
return self._handle_request('POST', url, params)
Expand All @@ -125,6 +142,8 @@ def get_withdrawal_history(self, params=None):
if params is None:
params = {}
params.update({'cmd': 'get_withdrawal_history',
'version': 1,
'format': 'json',
'key': self.COINPAYMENTS_PUBLIC_KEY})
url = self._url('')
return self._handle_request('POST', url, params)
Expand All @@ -137,6 +156,8 @@ def get_withdrawal_info(self, params=None):
if params is None:
params = {}
params.update({'cmd': 'get_withdrawal_info',
'version': 1,
'format': 'json',
'key': self.COINPAYMENTS_PUBLIC_KEY})
url = self._url('')
return self._handle_request('POST', url, params)
Expand All @@ -149,6 +170,22 @@ def get_conversion_info(self, params=None):
if params is None:
params = {}
params.update({'cmd': 'get_conversion_info',
'version': 1,
'format': 'json',
'key': self.COINPAYMENTS_PUBLIC_KEY})
url = self._url('')
return self._handle_request('POST', url, params)

def get_tx_info(self, params=None):
"""
Get single tx info
https://www.coinpayments.net/apidoc-get-tx-info
"""
if params is None:
params = {}
params.update({'cmd': 'get_tx_info',
'version': 1,
'format': 'json',
'key': self.COINPAYMENTS_PUBLIC_KEY})
url = self._url('')
return self._handle_request('POST', url, params)
Expand All @@ -161,6 +198,8 @@ def get_tx_info_multi(self, params=None):
if params is None:
params = {}
params.update({'cmd': 'get_tx_info_multi',
'version': 1,
'format': 'json',
'key': self.COINPAYMENTS_PUBLIC_KEY})
url = self._url('')
return self._handle_request('POST', url, params)
File renamed without changes.
30 changes: 30 additions & 0 deletions coinpayments/sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from coinpayments import CoinPayments

create_transaction_params = {
'amount': 10,
'currency1': 'USDT.BEP20',
'currency2': 'USDT.BEP20',
'buyer_email': 'confiyobo@gmail.com'
}
cp = CoinPayments(
'PrivateKey',
'PublicKey')

transaction = cp.create_transaction(create_transaction_params)

if transaction['error'] == 'ok':
print(transaction['amount'])
print(transaction['address'])
else:
print(transaction['error'])

post_params1 = {
'txid': transaction['txn_id'],
}
transactionInfo = cp.get_tx_info(post_params1)

if transactionInfo['error'] == 'ok':
print(transactionInfo['amountf'])
print(transactionInfo['payment_address'])
else:
print(transactionInfo['error'])
Loading

0 comments on commit 509fe18

Please sign in to comment.