Skip to content

Commit 545c7de

Browse files
authored
Merge pull request #221 from TotallyNotRobots/handle-cmc-no-data
Handle missing data from CMC api
2 parents 1f3320f + 5e1155b commit 545c7de

File tree

2 files changed

+51
-53
lines changed

2 files changed

+51
-53
lines changed

plugins/cryptocurrency.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def __init__(self, status: ResponseStatus):
135135

136136

137137
class UntypedResponse(APIRequestResponse):
138-
def __init__(self, data: Any, status: ResponseStatus):
138+
def __init__(self, status: ResponseStatus, data: Any = None):
139139
super().__init__(status)
140140
self.data = data
141141

@@ -370,9 +370,9 @@ def read_data(data: Dict, schema_cls: Type[T]) -> T:
370370
field_names.append(name)
371371
try:
372372
value = data[name]
373-
except KeyError:
373+
except KeyError as e:
374374
if schema_field.default is schema_field.empty:
375-
raise MissingSchemaField(name)
375+
raise MissingSchemaField(name) from e
376376

377377
value = schema_field.default
378378

tests/plugin_tests/test_cryptocurrency.py

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -108,61 +108,59 @@ def init_response(
108108
)
109109

110110
if quote:
111+
response_data = {
112+
"1": {
113+
"id": 1,
114+
"name": "Bitcoin",
115+
"slug": "bitcoin",
116+
"symbol": "BTC",
117+
"circulating_supply": 100,
118+
"total_supply": 1000,
119+
"date_added": (now - timedelta(days=5)).strftime(iso_fmt),
120+
"num_market_pairs": 1,
121+
"cmc_rank": 1,
122+
"last_updated": (now - timedelta(hours=1)).strftime(iso_fmt),
123+
"tags": [],
124+
"quote": {
125+
"BTC": {
126+
"price": 2,
127+
"volume_24h": 5,
128+
"market_cap": 97,
129+
"percent_change_1h": 12.5,
130+
"percent_change_24h": 17.4,
131+
"percent_change_7d": 54.1,
132+
"last_updated": (now - timedelta(minutes=6)).strftime(iso_fmt),
133+
},
134+
"USD": {
135+
"price": 50000000000,
136+
"volume_24h": 20,
137+
"market_cap": 92,
138+
"percent_change_1h": 14.5,
139+
"percent_change_24h": pct_change,
140+
"percent_change_7d": 24.5,
141+
"last_updated": (now - timedelta(minutes=3)).strftime(iso_fmt),
142+
},
143+
},
144+
},
145+
}
146+
data = {
147+
"status": {
148+
"timestamp": now.strftime(iso_fmt),
149+
"error_code": 400 if error_msg else 200,
150+
"error_message": error_msg,
151+
"elapsed": 1,
152+
"credit_count": 1,
153+
},
154+
}
155+
if not error_msg:
156+
data["data"] = response_data
157+
111158
mock_requests.add(
112159
MatchAPIKey(
113160
"GET",
114161
"https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=BTC&convert=USD%2CBTC",
115162
api_key="APIKEY" if check_api_key else None,
116-
json={
117-
"status": {
118-
"timestamp": now.strftime(iso_fmt),
119-
"error_code": 400 if error_msg else 200,
120-
"error_message": error_msg,
121-
"elapsed": 1,
122-
"credit_count": 1,
123-
},
124-
"data": {
125-
"1": {
126-
"id": 1,
127-
"name": "Bitcoin",
128-
"slug": "bitcoin",
129-
"symbol": "BTC",
130-
"circulating_supply": 100,
131-
"total_supply": 1000,
132-
"date_added": (now - timedelta(days=5)).strftime(iso_fmt),
133-
"num_market_pairs": 1,
134-
"cmc_rank": 1,
135-
"last_updated": (now - timedelta(hours=1)).strftime(
136-
iso_fmt
137-
),
138-
"tags": [],
139-
"quote": {
140-
"BTC": {
141-
"price": 2,
142-
"volume_24h": 5,
143-
"market_cap": 97,
144-
"percent_change_1h": 12.5,
145-
"percent_change_24h": 17.4,
146-
"percent_change_7d": 54.1,
147-
"last_updated": (
148-
now - timedelta(minutes=6)
149-
).strftime(iso_fmt),
150-
},
151-
"USD": {
152-
"price": 50000000000,
153-
"volume_24h": 20,
154-
"market_cap": 92,
155-
"percent_change_1h": 14.5,
156-
"percent_change_24h": pct_change,
157-
"percent_change_7d": 24.5,
158-
"last_updated": (
159-
now - timedelta(minutes=3)
160-
).strftime(iso_fmt),
161-
},
162-
},
163-
},
164-
},
165-
},
163+
json=data,
166164
)
167165
)
168166

0 commit comments

Comments
 (0)