Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions Veridu/SDK/API.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import urlparse
import urllib
import sys
if sys.version_info > (2, 7):
from urllib.parse import urlencode, urljoin
else:

@andriisoldatenko andriisoldatenko Apr 12, 2017

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be great to use

try:
    from urllib.parse import urlparse
except ImportError:
     from urlparse import urlparse

from urlparse import urljoin
from urllib import urlencode
import requests
import random
import time
Expand All @@ -16,7 +20,10 @@ class API(object):

def __init__(self, key, secret, version="0.3"):
self.key = key
self.secret = secret
if sys.version_info > (2, 7):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to use if version 3

self.secret = secret.encode('utf-8')
else:
self.secret = secret
self.version = version
self.session = None
self.headers = {
Expand Down Expand Up @@ -46,13 +53,15 @@ def createSignature(self, method, url):
"timestamp": int(time.time()),
"version": self.version
}
payload = urllib.urlencode(collections.OrderedDict(sorted(rawPayload.items())))
payload = urlencode(collections.OrderedDict(sorted(rawPayload.items())))
if sys.version_info > (2, 7):
payload = payload.encode('utf-8')
hmacInstance = hmac.new(self.secret, msg=payload, digestmod=hashlib.sha1)
rawPayload["signature"] = hmacInstance.hexdigest()
return rawPayload

def fetch(self, method, resource, data=None):
baseUrl = urlparse.urljoin("https://api.veridu.com", "/%s/%s" % (self.version, resource))
baseUrl = urljoin("https://api.veridu.com", "/%s/%s" % (self.version, resource))

if (method == "GET"):
response = requests.get(baseUrl, params=data, headers=self.headers)
Expand All @@ -78,7 +87,7 @@ def fetch(self, method, resource, data=None):
def signedFetch(self, method, resource, data=None):
sign = self.createSignature(
method,
urlparse.urljoin("https://api.veridu.com", "/%s/%s" % (self.version, resource))
urljoin("https://api.veridu.com", "/%s/%s" % (self.version, resource))
)

if data is None:
Expand Down