-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
29 lines (24 loc) · 1.06 KB
/
Copy pathhandler.py
File metadata and controls
29 lines (24 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import requests
class CryptoAPIError(Exception):
pass
class CryptoHandler:
BASE_URL = 'https://api.example.com/crypto'
def fetch_data(self, endpoint):
try:
response = requests.get(f'{self.BASE_URL}/{endpoint}')
response.raise_for_status()
return response.json()
except requests.exceptions.HTTPError as http_err:
raise CryptoAPIError(f'HTTP error occurred: {http_err}') from http_err
except requests.exceptions.ConnectionError:
raise CryptoAPIError('Network error, please check your connection.')
except requests.exceptions.Timeout:
raise CryptoAPIError('Request timed out.')
except requests.exceptions.RequestException as req_err:
raise CryptoAPIError(f'An error occurred: {req_err}')
except ValueError:
raise CryptoAPIError('Invalid JSON response.')
def get_price(self, crypto):
return self.fetch_data(f'price/{crypto}')
def get_market_cap(self, crypto):
return self.fetch_data(f'market_cap/{crypto}')