|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2017 Scott Shawcroft for Adafruit Industries |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | +""" |
| 23 | +`adafruit_adabot` |
| 24 | +==================================================== |
| 25 | +
|
| 26 | +TODO(description) |
| 27 | +
|
| 28 | +* Author(s): Scott Shawcroft |
| 29 | +""" |
| 30 | +import os |
| 31 | + |
| 32 | +import requests |
| 33 | + |
| 34 | + |
| 35 | +def _fix_url(url): |
| 36 | + if url.startswith("/"): |
| 37 | + url = "https://api.github.com" + url |
| 38 | + return url |
| 39 | + |
| 40 | +def _fix_kwargs(kwargs): |
| 41 | + api_version = "application/vnd.github.scarlet-witch-preview+json;application/vnd.github.hellcat-preview+json" |
| 42 | + if "headers" in kwargs: |
| 43 | + if "Accept" in kwargs["headers"]: |
| 44 | + kwargs["headers"]["Accept"] += ";" + api_version |
| 45 | + else: |
| 46 | + kwargs["headers"]["Accept"] = api_version |
| 47 | + else: |
| 48 | + kwargs["headers"] = {"Accept": "application/vnd.github.hellcat-preview+json"} |
| 49 | + if "ADABOT_GITHUB_ACCESS_TOKEN" in os.environ and "auth" not in kwargs: |
| 50 | + access_token = os.environ["ADABOT_GITHUB_ACCESS_TOKEN"] |
| 51 | + if "params" in kwargs: |
| 52 | + kwargs["params"]["access_token"] = access_token |
| 53 | + else: |
| 54 | + kwargs["params"] = {"access_token": access_token} |
| 55 | + if "timeout" not in kwargs: |
| 56 | + kwargs["timeout"] = 30 |
| 57 | + return kwargs |
| 58 | + |
| 59 | +def get(url, **kwargs): |
| 60 | + response = requests.get(_fix_url(url), **_fix_kwargs(kwargs)) |
| 61 | + remaining = int(response.headers["X-RateLimit-Remaining"]) |
| 62 | + if remaining % 100 == 0: |
| 63 | + print(remaining, "requests remaining this hour") |
| 64 | + return response |
| 65 | + |
| 66 | +def post(url, **kwargs): |
| 67 | + return requests.post(_fix_url(url), **_fix_kwargs(kwargs)) |
| 68 | + |
| 69 | +def put(url, **kwargs): |
| 70 | + return requests.put(_fix_url(url), **_fix_kwargs(kwargs)) |
| 71 | + |
| 72 | +def patch(url, **kwargs): |
| 73 | + return requests.patch(_fix_url(url), **_fix_kwargs(kwargs)) |
| 74 | + |
| 75 | +def delete(url, **kwargs): |
| 76 | + return requests.delete(_fix_url(url), **_fix_kwargs(kwargs)) |
0 commit comments