jacebrowning /
coverage-space-cli
| 1 | """API client functions.""" |
||
| 2 | |||
| 3 | 1 | ||
| 4 | 1 | import time |
|
| 5 | |||
| 6 | 1 | import log |
|
| 7 | import requests |
||
| 8 | 1 | ||
| 9 | from .cache import Cache |
||
| 10 | |||
| 11 | 1 | ||
| 12 | 1 | cache = Cache() |
|
| 13 | |||
| 14 | |||
| 15 | 1 | def get(url, data): |
|
| 16 | 1 | log.info("Getting %s: %s", url, data) |
|
| 17 | |||
| 18 | 1 | response = cache.get((url, data)) |
|
| 19 | 1 | if response is None: |
|
| 20 | 1 | for delay in [1, 3, 5]: |
|
| 21 | 1 | response = requests.put(url, data=data) |
|
| 22 | 1 | if response.status_code == 500: |
|
| 23 | 1 | time.sleep(delay) |
|
| 24 | 1 | continue |
|
| 25 | break |
||
| 26 | 1 | cache.set((url, data), response) |
|
| 27 | 1 | ||
| 28 | log.info("Response: %s", response) |
||
| 29 | 1 | ||
| 30 | return response |
||
| 31 | 1 | ||
| 32 | |||
| 33 | def delete(url, data): |
||
| 34 | 1 | log.info("Deleting %s: %s", url, data) |
|
| 35 | 1 | ||
| 36 | for delay in [1, 3, 5]: |
||
| 37 | 1 | response = requests.delete(url, data=data) |
|
| 38 | 1 | if response.status_code == 500: |
|
| 39 | 1 | time.sleep(delay) |
|
| 40 | continue |
||
| 41 | break |
||
| 42 | |||
| 43 | 1 | log.info("Response: %s", response) |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 44 | |||
| 45 | 1 | cache.clear() |
|
| 46 | |||
| 47 | return response |
||
| 48 |