Skip to content

Commit 0f0fad7

Browse files
committed
v1.3.0 — add hourly endpoint, EU server support, math/date_type params
1 parent 0cdfcfb commit 0f0fad7

File tree

4 files changed

+101
-26
lines changed

4 files changed

+101
-26
lines changed

README.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,28 @@ from forexrateapi.client import Client
1616

1717
api_key = 'SET_YOUR_API_KEY_HERE'
1818
client = Client(api_key)
19+
20+
# Or use EU server:
21+
# client = Client(api_key, server='eu')
22+
```
23+
---
24+
## Server Regions
25+
26+
ForexRateAPI provides two regional endpoints. Choose the one closest to your servers for optimal performance.
27+
28+
| Region | Base URL |
29+
|--------|----------|
30+
| United States (default) | `https://api.forexrateapi.com/v1` |
31+
| Europe | `https://api-eu.forexrateapi.com/v1` |
32+
33+
```python
34+
# Default (US)
35+
client = Client('SET_YOUR_API_KEY_HERE')
36+
37+
# Europe
38+
client = Client('SET_YOUR_API_KEY_HERE', server='eu')
1939
```
40+
2041
---
2142
## Documentation
2243

@@ -28,10 +49,20 @@ client.fetchSymbols()
2849
[Link](https://forexrateapi.com/documentation#api_symbol)
2950

3051
---
31-
#### fetchLive(base, currencies)
52+
#### setServer(server)
53+
54+
- `server` <[string]> Pass `'eu'` to use the EU server (`api-eu.forexrateapi.com`), or `'us'` for the US server. Defaults to US if not specified.
55+
56+
```python
57+
client.setServer('eu')
58+
```
59+
60+
---
61+
#### fetchLive(base, currencies, math)
3262

3363
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
3464
- `currencies` <[List]<[string]>> Optional. Pass in an list of currencies to return values for.
65+
- `math` <[string]> Optional. Pass in a math expression to apply to the rates.
3566

3667
```python
3768
client.fetchLive(base='USD', currencies=['AUD', 'CAD', 'GBP', 'JPY'])
@@ -52,6 +83,22 @@ client.fetchHistorical(date='2024-02-05', base='USD', currencies=['AUD', 'CAD',
5283

5384
[Link](https://forexrateapi.com/documentation#api_historical)
5485

86+
---
87+
#### hourly(base, currency, start_date, end_date, math, date_type)
88+
89+
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
90+
- `currency` <[string]> Required. Specify currency you would like to get hourly rates for.
91+
- `start_date` <[string]> Required. Specify the start date using the format `YYYY-MM-DD`.
92+
- `end_date` <[string]> Required. Specify the end date using the format `YYYY-MM-DD`.
93+
- `math` <[string]> Optional. Pass in a math expression to apply to the rates.
94+
- `date_type` <[string]> Optional. Pass in a date type, overrides date parameters if passed in.
95+
96+
```python
97+
client.hourly(base='USD', currency='EUR', start_date='2024-02-05', end_date='2024-02-05')
98+
```
99+
100+
[Link](https://forexrateapi.com/documentation#api_hourly)
101+
55102
---
56103
#### ohlc(base, currency, date, date_type)
57104

@@ -95,12 +142,13 @@ client.timeframe(start_date='2024-02-05', end_date='2024-02-06', base='USD', cur
95142
[Link](https://forexrateapi.com/documentation#api_timeframe)
96143

97144
---
98-
#### change(start_date, end_date, base, currencies)
145+
#### change(start_date, end_date, base, currencies, date_type)
99146

100147
- `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
101148
- `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
102149
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
103150
- `currencies` <[List]<[string]>> Optional. Pass in an list of currencies to return values for.
151+
- `date_type` <[string]> Optional. Pass in a date type, overrides date parameters if passed in.
104152

105153
```python
106154
client.change(start_date='2024-02-05', end_date='2024-02-06', base='USD', currencies=['AUD', 'CAD', 'GBP', 'JPY'])
@@ -139,4 +187,4 @@ For support, get in touch using [this form](https://forexrateapi.com/contact).
139187

140188
[List]: https://www.w3schools.com/python/python_datatypes.asp 'List'
141189
[number]: https://www.w3schools.com/python/python_datatypes.asp 'Number'
142-
[string]: https://www.w3schools.com/python/python_datatypes.asp 'String'
190+
[string]: https://www.w3schools.com/python/python_datatypes.asp 'String'

example.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,31 @@
33
api_key = 'REPLACE_ME'
44
client = Client(api_key)
55

6+
# Or use EU server:
7+
# client = Client(api_key, server='eu')
8+
69
result = client.fetchSymbols()
710
print(result)
811

912
result = client.fetchLive(base='USD', currencies=['AUD', 'CAD', 'GBP', 'JPY'])
1013
print(result)
1114

12-
result = client.fetchHistorical(date='2021-04-05', base='USD', currencies=['AUD', 'CAD', 'GBP', 'JPY'])
15+
result = client.fetchHistorical(date='2024-02-05', base='USD', currencies=['AUD', 'CAD', 'GBP', 'JPY'])
16+
print(result)
17+
18+
result = client.hourly(base='USD', currency='EUR', start_date='2024-02-05', end_date='2024-02-05')
1319
print(result)
1420

15-
result = client.ohlc(base='USD', currency='EUR', date='2021-04-05', date_type=None)
21+
result = client.ohlc(base='USD', currency='EUR', date='2024-02-05', date_type=None)
1622
print(result)
1723

18-
result = client.convert(from_currency='USD', to_currency='EUR', amount=100, date='2021-04-05')
24+
result = client.convert(from_currency='USD', to_currency='EUR', amount=100, date='2024-02-05')
1925
print(result)
2026

21-
result = client.timeframe(start_date='2021-04-05', end_date='2021-04-06', base='USD', currencies=['AUD', 'CAD', 'GBP', 'JPY'])
27+
result = client.timeframe(start_date='2024-02-05', end_date='2024-02-06', base='USD', currencies=['AUD', 'CAD', 'GBP', 'JPY'])
2228
print(result)
2329

24-
result = client.change(start_date='2021-04-05', end_date='2021-04-06', base='USD', currencies=['AUD', 'CAD', 'GBP', 'JPY'])
30+
result = client.change(start_date='2024-02-05', end_date='2024-02-06', base='USD', currencies=['AUD', 'CAD', 'GBP', 'JPY'])
2531
print(result)
2632

2733
result = client.usage()

forexrateapi/client.py

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,96 @@
11
import requests
22

33
class Client(object):
4-
base_url = 'https://api.forexrateapi.com/v1'
4+
SERVERS = {
5+
'us': 'https://api.forexrateapi.com/v1',
6+
'eu': 'https://api-eu.forexrateapi.com/v1'
7+
}
58

69
def _removeEmpty(self, params):
7-
return {key:value for key,value in params.items() if value != ''}
10+
return {key: value for key, value in params.items() if value is not None and value != ''}
811

9-
def __init__(self, api_key):
12+
def __init__(self, api_key, server='us'):
13+
self.base_url = self.SERVERS.get(server, self.SERVERS['us'])
1014
self.client = requests.Session()
1115
self.client.params.update({'api_key': api_key})
1216

17+
def setServer(self, server):
18+
self.base_url = self.SERVERS.get(server, self.SERVERS['us'])
19+
1320
def fetchSymbols(self):
1421
response = self.client.get(f'{self.base_url}/symbols')
1522
return response.json()
1623

17-
def fetchLive(self, base='', currencies=[]):
24+
def fetchLive(self, base='', currencies=None, math=''):
1825
params = self._removeEmpty({
1926
'base': base,
20-
'currencies': ','.join(currencies)
27+
'currencies': ','.join(currencies or []),
28+
'math': math,
2129
})
2230
response = self.client.get(f'{self.base_url}/latest', params=params)
2331
return response.json()
2432

25-
def fetchHistorical(self, date, base='', currencies=[]):
33+
def fetchHistorical(self, date, base='', currencies=None):
2634
params = self._removeEmpty({
2735
'base': base,
28-
'currencies': ','.join(currencies)
36+
'currencies': ','.join(currencies or []),
2937
})
3038
response = self.client.get(f'{self.base_url}/{date}', params=params)
3139
return response.json()
3240

33-
def ohlc(self, base, currency, date, date_type):
41+
def hourly(self, base='', currency='', start_date='', end_date='', math='', date_type=''):
3442
params = self._removeEmpty({
3543
'base': base,
3644
'currency': currency,
37-
'date': date,
45+
'start_date': start_date,
46+
'end_date': end_date,
47+
'math': math,
3848
'date_type': date_type
3949
})
50+
response = self.client.get(f'{self.base_url}/hourly', params=params)
51+
return response.json()
52+
53+
def ohlc(self, base='', currency='', date='', date_type=''):
54+
params = self._removeEmpty({
55+
'base': base,
56+
'currency': currency,
57+
'date': date,
58+
'date_type': date_type,
59+
})
4060
response = self.client.get(f'{self.base_url}/ohlc', params=params)
4161
return response.json()
4262

43-
def convert(self, to_currency, amount, from_currency='', date=''):
63+
def convert(self, from_currency='', to_currency='', amount='', date=''):
4464
params = self._removeEmpty({
4565
'from': from_currency,
4666
'to': to_currency,
4767
'amount': amount,
48-
'date': date
68+
'date': date,
4969
})
5070
response = self.client.get(f'{self.base_url}/convert', params=params)
5171
return response.json()
5272

53-
def timeframe(self, start_date, end_date, base='', currencies=[]):
73+
def timeframe(self, start_date, end_date, base='', currencies=None):
5474
params = self._removeEmpty({
5575
'start_date': start_date,
5676
'end_date': end_date,
5777
'base': base,
58-
'currencies': ','.join(currencies)
78+
'currencies': ','.join(currencies or []),
5979
})
6080
response = self.client.get(f'{self.base_url}/timeframe', params=params)
6181
return response.json()
6282

63-
def change(self, start_date, end_date, base='', currencies=[]):
83+
def change(self, start_date, end_date, base='', currencies=None, date_type=''):
6484
params = self._removeEmpty({
6585
'start_date': start_date,
6686
'end_date': end_date,
6787
'base': base,
68-
'currencies': ','.join(currencies)
88+
'currencies': ','.join(currencies or []),
89+
'date_type': date_type,
6990
})
7091
response = self.client.get(f'{self.base_url}/change', params=params)
7192
return response.json()
7293

7394
def usage(self):
7495
response = self.client.get(f'{self.base_url}/usage')
75-
return response.json()
96+
return response.json()

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
setuptools.setup(
44
name="forexrateapi",
5-
version="1.1.2",
5+
version="1.3.0",
66
url="https://github.com/forexrateapi/forexrateapi-python",
77

8-
license_files=('LICENSE'),
8+
license_files=('LICENSE',),
99

1010
author="ForexRateAPI",
1111
author_email="contact@forexrateapi.com",

0 commit comments

Comments
 (0)