forked from Azure/azure-linux-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpclient.py
128 lines (100 loc) · 4.45 KB
/
httpclient.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python2
#
# Copyright (C) Microsoft Corporation, All rights reserved.
"""HttpClient base class."""
import os
import sys
import serializerfactory
class HttpClient:
"""Base class to provide common attributes and functionality to all HttpClient implementation."""
ACCEPT_HEADER_KEY = "Accept"
CONTENT_TYPE_HEADER_KEY = "Content-Type"
CONNECTION_HEADER_KEY = "Connection"
USER_AGENT_HEADER_KEY = "User-Agent"
APP_JSON_HEADER_VALUE = "application/json"
KEEP_ALIVE_HEADER_VALUE = "keep-alive"
GET = "GET"
POST = "POST"
PUT = "PUT"
DELETE = "DELETE"
def __init__(self, cert_path, key_path, insecure=False, proxy_configuration=None):
self.cert_path = cert_path
self.key_path = key_path
self.insecure = insecure
self.proxy_configuration = proxy_configuration
# validate presence of cert/key in case they were removed after process creation
if (cert_path is not None and not os.path.isfile(self.cert_path)) or \
(key_path is not None and not os.path.isfile(self.key_path)):
print(cert_path)
raise Exception("Invalid certificate or key file path.")
self.default_headers = {self.ACCEPT_HEADER_KEY: self.APP_JSON_HEADER_VALUE,
self.CONNECTION_HEADER_KEY: self.KEEP_ALIVE_HEADER_VALUE
}
self.json = serializerfactory.get_serializer(sys.version_info)
@staticmethod
def merge_headers(client_headers, request_headers):
"""Merges client_headers and request_headers into a single dictionary. If a request_header key is also present
in the client_headers, the request_header value will override the client_header one.
Args:
client_headers : dictionary, the default client's headers.
request_headers : dictionary, request specific headers.
Returns:
A dictionary containing a set of both the client_headers and the request_headers
"""
if request_headers is not None:
client_headers.update(request_headers.copy())
else:
request_headers = client_headers.copy()
return request_headers
def get(self, url, headers=None):
"""Issues a GET request to the provided url using the provided headers.
Args:
url : string , the URl.
headers : dictionary, contains the headers key value pair (defaults to None).
Returns:
A RequestResponse
"""
pass
def post(self, url, headers=None, data=None):
"""Issues a POST request to the provided url using the provided headers.
Args:
url : string , the URl.
headers : dictionary, contains the headers key value pair (defaults to None).
data : dictionary, contains the non-serialized request body (defaults to None).
Returns:
A RequestResponse
"""
pass
def put(self, url, headers=None, data=None):
"""Issues a PUT request to the provided url using the provided headers.
Args:
url : string , the URl.
headers : dictionary, contains the headers key value pair (defaults to None).
data : dictionary, contains the non-serialized request body (defaults to None).
Returns:
A RequestResponse
"""
pass
def delete(self, url, headers=None, data=None):
"""Issues a DELETE request to the provided url using the provided headers.
Args:
url : string , the URl.
headers : dictionary, contains the headers key value pair (defaults to None).
data : dictionary, contains the non-serialized request body (defaults to None).
Returns:
A RequestResponse
"""
pass
class RequestResponse:
"""Encapsulates all request response for http clients. Will also deserialize the response when the raw response
data is deserializable.
"""
def __init__(self, status_code, raw_response_data=None):
self.status_code = int(status_code)
self.raw_data = raw_response_data
self.json = serializerfactory.get_serializer(sys.version_info)
if raw_response_data is not None:
try:
self.deserialized_data = self.json.loads(self.raw_data)
except ValueError:
self.deserialized_data = None