-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
client.py
75 lines (59 loc) · 1.95 KB
/
client.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
"""Simple client to access our API with Slumber credentials."""
from __future__ import (
absolute_import,
division,
print_function,
unicode_literals,
)
import logging
import requests
from django.conf import settings
from requests_toolbelt.adapters import host_header_ssl
from rest_framework.renderers import JSONRenderer
from slumber import API, serialize
log = logging.getLogger(__name__)
PRODUCTION_DOMAIN = getattr(settings, 'PRODUCTION_DOMAIN', 'readthedocs.org')
API_HOST = getattr(settings, 'SLUMBER_API_HOST', 'https://readthedocs.org')
USER = getattr(settings, 'SLUMBER_USERNAME', None)
PASS = getattr(settings, 'SLUMBER_PASSWORD', None)
class DrfJsonSerializer(serialize.JsonSerializer):
"""Additional serialization help from the DRF renderer"""
key = 'json-drf'
def dumps(self, data):
"""Used to be able to render datetime objects."""
return JSONRenderer().render(data)
def setup_api():
session = requests.Session()
if API_HOST.startswith('https'):
# Only use the HostHeaderSSLAdapter for HTTPS connections
adapter_class = host_header_ssl.HostHeaderSSLAdapter
else:
adapter_class = requests.adapters.HTTPAdapter
session.mount(
API_HOST,
adapter_class(max_retries=3),
)
session.headers.update({'Host': PRODUCTION_DOMAIN})
api_config = {
'base_url': '%s/api/v2/' % API_HOST,
'serializer': serialize.Serializer(
default='json-drf',
serializers=[
serialize.JsonSerializer(),
DrfJsonSerializer(),
],
),
'session': session,
}
if USER and PASS:
log.debug(
'Using slumber v2 with user %s, pointed at %s',
USER,
API_HOST,
)
session.auth = (USER, PASS)
else:
log.warning('SLUMBER_USERNAME/PASSWORD settings are not set')
return API(**api_config)
api = setup_api()