This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
/
api.py
60 lines (49 loc) · 2.35 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# TODO: extend API to handle release blobs
import logging
import requests
import os
CA_BUNDLE = os.path.join(os.path.dirname(__file__),
'../../../../misc/mozilla-root.crt')
class API(object):
url_template = \
'%(api_root)s/releases/%(name)s/builds/%(build_target)s/%(locale)s'
verify = False
auth = None
def __init__(self, api_root='https://balrog.build.mozilla.org',
auth=None, ca_certs=CA_BUNDLE, timeout=60, raise_exceptions=True):
""" Creates an API object which wraps REST API of Balrog server.
api_root: API root URL of balrog server
auth : a tuple of (username, password) or None
ca_certs: CA bundle. It follows python-requests `verify' usage.
If set to False, no SSL verification is done.
If set to True, it tries to load a CA bundle from certifi
module.
If set to string, puthon-requests uses it as a pth to path to
CA bundle.
timeout : request timeout
raise_exceptions: Sets danger_mode parameter of python-requests config
which controls excpetion raising.
"""
self.api_root = api_root.rstrip('/')
self.verify = ca_certs
assert isinstance(auth, tuple) or auth == None, \
"auth should be set to tuple or None"
self.auth = auth
self.timeout = timeout
self.config = dict(danger_mode=raise_exceptions)
def request(self, url, data=None, method='GET'):
logging.debug('Balrog request to %s' % url)
logging.debug('Data sent: %s' % data)
return requests.request(method=method, url=url, data=data,
config=self.config, timeout=self.timeout,
verify=self.verify, auth=self.auth)
def update_build(self, name, product, version, build_target, locale,
details, copy_to=None):
url_template_vars = dict(api_root=self.api_root, name=name,
locale=locale, build_target=build_target)
url = self.url_template % url_template_vars
data = dict(product=product, version=version,
details=details)
if copy_to:
data['copy_to'] = copy_to
return self.request(method='PUT', url=url, data=data)