11import requests
2- from collections import namedtuple
3- from urllib .parse import urlencode , urlunparse
2+ from urllib .parse import urlencode
43
5- from .config import Region
64from .exceptions import *
75
86class ExchangerateClient :
97 """
108 Primary client class
9+ @param api_key: the api key from https://apilayer.com/marketplace/fixer-api#documentation-tab
1110 """
12- def __init__ (self , server_region = Region . AMERICA ):
13- self .server_region = server_region
11+ def __init__ (self , api_key : str ):
12+ self .api_key = api_key
1413 self .session = requests .Session ()
1514
1615 # -------------------------------------------------------------------
@@ -22,7 +21,7 @@ def symbols(self):
2221 """
2322 url = self ._build_url (path = "symbols" )
2423 resp_json = self ._validate_and_get_json (url )
25- return resp_json .get ("rates" )
24+ return resp_json .get ("symbols" ). keys ( )
2625
2726 def latest (self , base_currency = "USD" , symbols = None , amount = 1 ):
2827 """
@@ -43,7 +42,8 @@ def latest(self, base_currency="USD", symbols=None, amount=1):
4342 # Private methods
4443 # -------------------------------------------------------------------
4544 def _validate_and_get_json (self , url ):
46- resp = self .session .get (url )
45+ headers = {"apikey" : self .api_key }
46+ resp = self .session .get (url , headers = headers )
4747 if resp .status_code != 200 :
4848 raise ResponseErrorException ("Status code=%d calling url=%s" % (resp .status_code , url ))
4949
@@ -54,21 +54,11 @@ def _validate_and_get_json(self, url):
5454 return resp_json
5555
5656 def _build_url (self , path = "" , params = None ):
57- Components = namedtuple (
58- typename = 'Components' ,
59- field_names = ['scheme' , 'netloc' , 'url' , 'path' , 'query' , 'fragment' ]
60- )
57+ url = "https://api.apilayer.com/fixer/"
58+ if path :
59+ url += path
6160
62- if not params :
63- params = {}
61+ if params :
62+ url += f"? { urlencode ( params ) } "
6463
65- return urlunparse (
66- Components (
67- scheme = 'https' ,
68- netloc = self .server_region .value ,
69- query = urlencode (params ),
70- path = path ,
71- url = "/" ,
72- fragment = ""
73- )
74- )
64+ return url
0 commit comments