Created
May 12, 2021 15:10
-
-
Save Holzhaus/21db17ebba770586ec9969ea3992264a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import logging | |
import requests | |
import urllib.parse | |
import netrc | |
import pprint | |
class BearerAuth(requests.auth.AuthBase): | |
def __init__(self, token): | |
self.token = token | |
def __call__(self, r): | |
r.headers["Authorization"] = "Bearer " + self.token | |
return r | |
class TransifexAPI: | |
def __init__(self, api_key): | |
self.baseurl = "https://rest.api.transifex.com" | |
self.api_key = api_key | |
self.session = requests.Session() | |
self.session.auth = BearerAuth(self.api_key) | |
self.session.headers.update({ | |
"Content-Type": "application/vnd.api+json", | |
"Accept": "application/vnd.api+json", | |
}) | |
def request(self, path, method="GET", params=None): | |
url = urllib.parse.urljoin(self.baseurl, path) | |
return self.session.request(method, url, params=params) | |
def main(argv=None, init_logging=True): | |
if init_logging: | |
logging.basicConfig() | |
logger = logging.getLogger(__name__) | |
netrcobj = netrc.netrc() | |
login, _, password = netrcobj.authenticators("rest.api.transifex.com") | |
if not password or login != "bearer": | |
logger.critical("No API key found in .netrc - please add it with login name 'bearer' for machine rest.api.transifex.com") | |
return 1 | |
tx = TransifexAPI(api_key=password) | |
project_id = "o:mixxx-dj-software:p:mixxxdj-manual" | |
r = tx.request("/resources", params={ | |
"filter[project]": project_id, | |
}) | |
pprint.pprint(r.json()) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment