Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.5 kB
2
Indexable
Never
#!/bin/python3
import time
import requests
import hmac
import hashlib
import base64
import json
from requests_toolbelt.utils import dump

class TestIconomi():

    # test api secret
    API_URL = "https://api.iconomi.com"
    API_KEY = "123"
    API_SECRET = "123"

    def test_strategies(self):
        self.get('/v1/strategies')

    def test_activity(self):
        self.get('/v1/user/activity?type=FEES_AND_EARNINGS')

    def test_get_structure(self):
        ticker = 'ECA'
        self.get('/v1/strategies/' + ticker + '/structure')

    def test_set_structure(self):
        ticker = "<YOUR_TICKER>"
        payload = {
          'ticker': ticker,
          'values': [
             {
               'rebalancedWeight': '0.5', 'assetTicker': 'ETH'
             },
             {
               'rebalancedWeight': '0.5', 'assetTicker': 'BTC'
             }
          ]
        }

        self.post('/v1/strategies/' + ticker + '/structure', payload)

    def test_withdraw(self):
        payload = {
            'amount': '0.02',
            'currency': 'ETH',
            'address': '<YOU_ETH_ADDRESS>'
        }

        self.post('/v1/user/withdraw', payload)

    def generate_signature(self, payload, request_type, request_path, timestamp): 
        query = request_path.find('?')
        if query != -1:
            request_path = request_path[0:query]
            
        default_encoding = "utf8"
            
        message = (str(timestamp) + request_type.upper() + request_path + payload).encode(default_encoding)
        signature_digest = hmac.new(self.API_SECRET.encode(default_encoding), message, hashlib.sha512).digest() #here digest is byte
        b64_signature_digest = base64.b64encode(signature_digest).decode(default_encoding)
        print(b64_signature_digest)
        return b64_signature_digest

    def get(self, api):      
        self.call('GET', api, '')

    def post(self, api, payload):
        self.call('POST', api, payload)
        
    def call(self, method, api, payload):
        timestamp = int(time.time() * 1000.0)
        
        jsonPayload = payload
        if method == 'POST':
            jsonPayload = json.dumps(payload)      
        
        requestHeaders = { 
            'ICN-API-KEY' : self.API_KEY,
            'ICN-SIGN' : self.generate_signature(jsonPayload, method, api, timestamp),
            'ICN-TIMESTAMP' : str(timestamp),
            'Content-Type': 'application/json'
        }
        
        s = requests.session()
        if method == 'GET': 
          response = s.get(self.API_URL + api, headers = requestHeaders, timeout=3, verify=True)
          data = dump.dump_all(response)
          print(data.decode('utf-8'))
          if response.status_code == 200:
            print(json.dumps(json.loads(response._content), indent=4, sort_keys=True))
          else:
            print('Request did not succeed: ' + response.reason)
            print(json.dumps(json.loads(response._content), indent=4, sort_keys=True))
        elif method == 'POST':
          response = requests.post(self.API_URL + api, json = payload, headers = requestHeaders)
          if response.status_code == 200:
            print(json.dumps(json.loads(response._content), indent=4, sort_keys=True))
          else:
            print('Request did not succeed: ' + response.reason)
            
def main():
    ico = TestIconomi()
    ico.test_get_structure()

if __name__ == "__main__":
    main()