Created
September 25, 2019 10:23
-
-
Save dongyi/49ee8655684226b3412bf7e17e2168ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# new build worked ebury client | |
import json | |
import requests | |
from base64 import b64encode | |
from urllib.parse import urlencode | |
from uuid import uuid4 | |
from service.fx import FXService | |
from django.utils import timezone | |
# docs : https://docs.ebury.io/?python#introduction | |
CLIENT_ID = ' ' | |
REDIRECT_URI = 'https://tradex-api.zeno-dev.com/ebury/callback/' | |
CLIENT_SECRET = ' ' | |
API_KEY = ' ' | |
CONTACT_ID = '' | |
class EburyFX(FXService): | |
def __init__(self): | |
super().__init__() | |
self.client_id = CLIENT_ID | |
self.redirect_uri = REDIRECT_URI | |
self.client_secret = CLIENT_SECRET | |
self.api_key = API_KEY | |
self.contact_id = CONTACT_ID | |
self.auth_code = '' | |
def get_auth(self): | |
response = requests.get( | |
f'https://auth.ebury.io/authenticate?scope=openid&response_type=code&client_id={self.client_id}&state={uuid4().hex}&redirect_uri={self.redirect_uri}').json() | |
# see auth code | |
return response | |
def auth_header(self): | |
return { | |
'x-api-key': self.api_key, | |
'Authorization': f'Bearer {self.access_token}', | |
'X-Contact-ID': self.contact_id, | |
'Content-Type': 'application/json' | |
} | |
def login(self, auth_code): | |
credentials = b64encode(bytes(f'{self.client_id}{self.client_secret}')).decode('utf-8') | |
response = requests.post( | |
'https://auth.ebury.io/token', | |
body=urlencode({ | |
'grant_type': 'authorization_code', | |
'code': auth_code, | |
'redirect_uri': self.redirect_uri | |
}), | |
headers={ | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Authorization': f'Basic {credentials}' | |
} | |
) | |
ret = response.json() | |
self.access_token = ret['access_token'] | |
self.refres_token = ret['refresh_token'] | |
self.expire_in = ret['expires_in'] | |
self.token_type = ret['token_type'] | |
return ret | |
def get_access_token(self): | |
credentials = b64encode(bytes(f'{self.client_id}{self.client_secret}')).decode('utf-8') | |
response = requests.post( | |
'https://auth.ebury.io/token', | |
body=urlencode({ | |
'grant_type': 'refresh_token', | |
'refresh_token': self.refres_token, | |
'scope': 'openid' | |
}), | |
headers={ | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Authorization': 'Basic %s' % credentials | |
} | |
) | |
ret = response.json() | |
self.contact_id = ret['sub'] | |
return ret | |
def get_refresh_token(self): | |
response = requests.post( | |
f'https://auth.ebury.io/token', | |
body=json.dumps({ | |
'grant_type': 'refresh_token', | |
'refresh_token': self.refres_token, | |
'scope': 'openid' | |
}), | |
headers=self.auth_header | |
) | |
ret = response.json() | |
self.contact_id = ret['sub'] | |
return ret | |
def get_book_rate(self, from_currency, to_currency, amount): | |
resp = requests.post( | |
f'https://api.ebury.io/quotes?quote_type=quote&client_id={self.client_id}', | |
json={ | |
'trade_type': 'spot', | |
'buy_currency': from_currency, | |
'amount': amount, | |
'operation': 'buy', | |
'sell_currency': to_currency, | |
'value_date': timezone.now().strftime('%Y-%m-%d') | |
}, | |
headers=self.auth_header | |
) | |
r = resp.json() | |
data = { | |
"quote_id": r.get("quote_id"), | |
"rate": r.get("quoted_rate") * (1 - OVERAGE_PERCENTAGE), # Ray's idea | |
"amount": amount | |
} | |
return data | |
def book_rate(self, quote_id, reason="TradeX trade"): | |
resp = requests.post( | |
f'https://api.ebury.io/trades?quote_id={quote_id}&client_id={self.client_id}', | |
json={ | |
'reason': reason, | |
'trade_type': 'spot' | |
}, | |
headers=self.auth_header | |
) | |
ret = resp.json() | |
return { | |
'trade_id': ret.get('trade_id') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment