Skip to content

Commit a74c47c

Browse files
committed
Detect reaching quota limit of GraphQL endpoint
1 parent 892d2c9 commit a74c47c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

app.py

+26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
import json
4+
import os
45

56
import requests
67
from flask import (Flask, g, make_response, request, send_from_directory,
@@ -81,6 +82,25 @@ def _get_config(key, fallback=None):
8182
return g.config.get(key, fallback)
8283

8384

85+
def _write_status(key_values):
86+
status = dict()
87+
fp = '/config/status.json'
88+
if os.path.exists(fp):
89+
try:
90+
with open(fp, 'r') as f:
91+
status = json.load(f)
92+
except Exception as e:
93+
app.logger.critical(str(e))
94+
for key, value in key_values.items():
95+
status[key] = value
96+
try:
97+
with open(fp + '~', 'w') as f:
98+
f.write(json.dumps(status, indent=4))
99+
os.replace(fp + '~', fp)
100+
except Exception as e:
101+
app.logger.critical(str(e))
102+
103+
84104
def _build_headers():
85105
return {
86106
'Content-Type': 'application/json',
@@ -276,6 +296,12 @@ def parts_query():
276296
for error in errors:
277297
app.logger.warning("GraphQL Error: " + str(error))
278298

299+
# Handle quota limit.
300+
next_access_time = query_json.get('nextAccessTime')
301+
if (len(data) == 0) and (next_access_time is not None):
302+
app.logger.warning("Quota limit: " + str(next_access_time))
303+
_write_status(dict(next_access_time=next_access_time))
304+
279305
# Convert query response data and return it to the client.
280306
tx = dict(parts=[])
281307
for i in range(len(parts)):

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ services:
99
ports:
1010
- 8000:8000
1111
volumes:
12-
- './config:/config:ro'
12+
- './config:/config'
1313
environment:
1414
FLASK_RUN_DEBUG: 1
1515
FLASK_RUN_HOST: '0.0.0.0'

0 commit comments

Comments
 (0)