9
9
import requests
10
10
import time
11
11
from .about import get_client_user_agent
12
+ import urllib3
13
+ urllib3 .disable_warnings (urllib3 .exceptions .InsecureRequestWarning )
12
14
13
15
class AuthError (Exception ):
14
16
def __init__ (self , message ):
@@ -30,30 +32,14 @@ class RestApiConnection:
30
32
# Don't let users set these headers
31
33
FORBIDDEN_HEADERS = {"Authorization" , "Content-Type" , "Accept" }
32
34
33
- def __init__ (self , use_http = False , host = "api.ultradns.com" , access_token : str = "" , refresh_token : str = "" , custom_headers = None ):
35
+ def __init__ (self , use_http = False , host = "api.ultradns.com" , access_token : str = "" , refresh_token : str = "" , custom_headers = None , proxy = None , verify_https = True ):
34
36
self .use_http = use_http
35
37
self .host = host
36
38
self .access_token = access_token
37
39
self .refresh_token = refresh_token
38
40
self .custom_headers = custom_headers or {}
39
-
40
- def _validate_custom_headers (self , headers ):
41
- """Ensure no forbidden headers are being set by the user."""
42
- for header in headers .keys ():
43
- if header in self .FORBIDDEN_HEADERS :
44
- raise ValueError (f"Custom headers cannot include '{ header } '." )
45
-
46
- def set_custom_headers (self , headers ):
47
- """Update custom headers after instantiation."""
48
- self ._validate_custom_headers (headers )
49
- self .custom_headers .update (headers )
50
-
51
- def _get_connection (self ):
52
- if self .host .startswith ("https://" ) or self .host .startswith ("http://" ):
53
- return self .host
54
- else :
55
- protocol = "http://" if self .use_http else "https://"
56
- return protocol + self .host
41
+ self .proxy = proxy
42
+ self .verify_https = verify_https
57
43
58
44
# Authentication
59
45
# We need the ability to take in a username and password and get
@@ -69,7 +55,12 @@ def auth(self, username, password):
69
55
"username" :username ,
70
56
"password" :password
71
57
}
72
- response = requests .post (f"{ host } /v1/authorization/token" , data = payload )
58
+ response = requests .post (
59
+ f"{ host } /v1/authorization/token" ,
60
+ data = payload ,
61
+ proxies = self .proxy ,
62
+ verify = self .verify_https
63
+ )
73
64
if response .status_code == requests .codes .OK :
74
65
json_body = response .json ()
75
66
self .access_token = json_body .get ('accessToken' )
@@ -83,14 +74,27 @@ def _refresh(self):
83
74
"grant_type" :"refresh_token" ,
84
75
"refresh_token" :self .refresh_token
85
76
}
86
- response = requests .post (f"{ host } /v1/authorization/token" , data = payload )
77
+ response = requests .post (
78
+ f"{ host } /v1/authorization/token" ,
79
+ data = payload ,
80
+ proxies = self .proxy ,
81
+ verify = self .verify_https
82
+ )
87
83
if response .status_code == requests .codes .OK :
88
84
json_body = response .json ()
89
85
self .access_token = json_body .get ('accessToken' )
90
86
self .refresh_token = json_body .get ('refreshToken' )
91
87
else :
92
88
raise AuthError (response .json ())
93
89
90
+ # Private Utility Methods
91
+
92
+ def _validate_custom_headers (self , headers ):
93
+ """Ensure no forbidden headers are being set by the user."""
94
+ for header in headers .keys ():
95
+ if header in self .FORBIDDEN_HEADERS :
96
+ raise ValueError (f"Custom headers cannot include '{ header } '." )
97
+
94
98
def _build_headers (self , content_type ):
95
99
"""Construct headers by merging default, custom, and per-request headers."""
96
100
headers = {
@@ -105,25 +109,14 @@ def _build_headers(self, content_type):
105
109
106
110
return headers
107
111
108
- def get (self , uri , params = None ):
109
- params = params or {}
110
- return self ._do_call (uri , "GET" , params = params )
111
-
112
- def post_multi_part (self , uri , files ):
113
- #use empty string for content type so we don't set it
114
- return self ._do_call (uri , "POST" , files = files , content_type = None )
115
-
116
- def post (self , uri , json = None ):
117
- return self ._do_call (uri , "POST" , body = json ) if json is not None else self ._do_call (uri , "POST" )
118
-
119
- def put (self , uri , json ):
120
- return self ._do_call (uri , "PUT" , body = json )
112
+ def _get_connection (self ):
113
+ if self .host .startswith ("https://" ) or self .host .startswith ("http://" ):
114
+ return self .host
115
+ else :
116
+ protocol = "http://" if self .use_http else "https://"
117
+ return protocol + self .host
121
118
122
- def patch (self , uri , json ):
123
- return self ._do_call (uri , "PATCH" , body = json )
124
-
125
- def delete (self , uri ):
126
- return self ._do_call (uri , "DELETE" )
119
+ # Main Request Method
127
120
128
121
def _do_call (self , uri , method , params = None , body = None , retry = True , files = None , content_type = "application/json" ):
129
122
host = self ._get_connection ()
@@ -133,7 +126,9 @@ def _do_call(self, uri, method, params=None, body=None, retry=True, files=None,
133
126
params = params ,
134
127
data = body ,
135
128
headers = self ._build_headers (content_type ),
136
- files = files
129
+ files = files ,
130
+ proxies = self .proxy ,
131
+ verify = self .verify_https
137
132
)
138
133
if response .status_code == requests .codes .NO_CONTENT :
139
134
return {}
@@ -171,3 +166,36 @@ def _do_call(self, uri, method, params=None, body=None, retry=True, files=None,
171
166
return self ._do_call (uri , method , params , body , False )
172
167
173
168
return json_body
169
+
170
+ # Public HTTP Methods
171
+
172
+ def get (self , uri , params = None ):
173
+ params = params or {}
174
+ return self ._do_call (uri , "GET" , params = params )
175
+
176
+ def post_multi_part (self , uri , files ):
177
+ #use empty string for content type so we don't set it
178
+ return self ._do_call (uri , "POST" , files = files , content_type = None )
179
+
180
+ def post (self , uri , json = None ):
181
+ return self ._do_call (uri , "POST" , body = json ) if json is not None else self ._do_call (uri , "POST" )
182
+
183
+ def put (self , uri , json ):
184
+ return self ._do_call (uri , "PUT" , body = json )
185
+
186
+ def patch (self , uri , json ):
187
+ return self ._do_call (uri , "PATCH" , body = json )
188
+
189
+ def delete (self , uri ):
190
+ return self ._do_call (uri , "DELETE" )
191
+
192
+ # Public Utility Methods
193
+
194
+ def set_custom_headers (self , headers ):
195
+ """Update custom headers after instantiation."""
196
+ self ._validate_custom_headers (headers )
197
+ self .custom_headers .update (headers )
198
+
199
+ def set_proxy (self , proxy ):
200
+ """Update the proxy configuration."""
201
+ self .proxy = proxy
0 commit comments