Skip to content

Commit d4a165d

Browse files
handling the API key as bearer token
1 parent 67c102e commit d4a165d

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

socketlabs/injectionapi/core/httprequest.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from ..version import __version__
1010
from ..proxy import Proxy
1111

12+
from .stringextension import StringExtension
1213
from .httpendpoint import HttpEndpoint
1314
from .injectionresponseparser import InjectionResponseParser
1415
from .serialization.injectionrequest import InjectionRequest
@@ -28,7 +29,7 @@ class HttpRequestMethod(Enum):
2829
PUT = 2
2930
DELETE = 3
3031

31-
def __init__(self, method: HttpRequestMethod, endpoint: HttpEndpoint, timeout: int):
32+
def __init__(self, method: HttpRequestMethod, endpoint: HttpEndpoint, timeout: int, authentication: str):
3233
"""
3334
Creates a new instance of the HTTP Request class
3435
:param method: the HTTP request method
@@ -40,6 +41,14 @@ def __init__(self, method: HttpRequestMethod, endpoint: HttpEndpoint, timeout: i
4041
self._endpoint = endpoint
4142
self._http_proxy = None
4243
self._timeout = timeout
44+
self._authentication = authentication
45+
self._headers = {
46+
'User-Agent': self.__user_agent,
47+
'Content-Type': 'application/json; charset=utf-8',
48+
'Accept': 'application/json',
49+
}
50+
if not StringExtension.is_none_or_white_space(authentication):
51+
self._headers["Authorization"] = "Bearer " + authentication
4352

4453
@property
4554
def __user_agent(self):
@@ -108,7 +117,7 @@ def send_async_request(self, request: InjectionRequest, on_success_callback, on_
108117
"""
109118

110119
try:
111-
120+
print (request)
112121
th = threading.Thread(target=self.__queue_request,
113122
kwargs={
114123
"request": request,
@@ -143,17 +152,12 @@ def send_request(self, request: InjectionRequest):
143152
:return the injection response received from the request
144153
:rtype InjectionResponse
145154
"""
146-
headers = {
147-
'User-Agent': self.__user_agent,
148-
'Content-Type': 'application/json; charset=utf-8',
149-
'Accept': 'application/json',
150-
}
151155

152156
json_body = json.dumps(request.to_json())
153157

154158
try:
155159
connection = self.__get_connection()
156-
connection.request("POST", self._endpoint.url, json_body, headers)
160+
connection.request("POST", self._endpoint.url, json_body, self._headers)
157161
response = connection.getresponse()
158162

159163
except Exception as e:

socketlabs/injectionapi/core/injectionresponseparser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def get_injection_response_dto(response: str):
6060

6161
if 'MessageResults' in dct:
6262
resp_dto.message_results = []
63-
for item in dct['MessageResults']:
63+
for item in dct['MessageResults'] or []:
6464
message_dto = MessageResultDto()
6565
if 'Index' in item:
6666
message_dto.index = item['Index']

0 commit comments

Comments
 (0)