Skip to content

Commit 0363888

Browse files
Merge pull request #3 from PraneethChandraThota/request-timeout
LU-43 Python Http Request Timeout
2 parents c37a70f + e1b9ab7 commit 0363888

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

python-examples/basic/basic_send_with_proxy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626
api_key = os.environ.get('SOCKETLABS_INJECTION_API_KEY')
2727

2828
# create the proxy
29-
proxy = Proxy("127.0.0.1", 8888)
29+
proxy = Proxy("127.0.0.1", 4433)
3030

3131
# create the client
3232
client = SocketLabsClient(server_id, api_key, proxy)
33+
client.request_timeout = 10
3334

3435
# send the message
3536
response = client.send(message)

socketlabs/injectionapi/core/httprequest.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class HttpRequestMethod(Enum):
2828
PUT = 2
2929
DELETE = 3
3030

31-
def __init__(self, method: HttpRequestMethod, endpoint: HttpEndpoint):
31+
def __init__(self, method: HttpRequestMethod, endpoint: HttpEndpoint, timeout: int):
3232
"""
3333
Creates a new instance of the HTTP Request class
3434
:param method: the HTTP request method
@@ -39,6 +39,7 @@ def __init__(self, method: HttpRequestMethod, endpoint: HttpEndpoint):
3939
self._request_method = method
4040
self._endpoint = endpoint
4141
self._http_proxy = None
42+
self._timeout = timeout
4243

4344
@property
4445
def __user_agent(self):
@@ -68,6 +69,15 @@ def __endpoint(self):
6869
"""
6970
return self._endpoint
7071

72+
@property
73+
def __timeout(self):
74+
"""
75+
The SocketLabs Injection API timeout
76+
:return the Http timeout for the HTTP request
77+
:rtype int
78+
"""
79+
return self._timeout
80+
7181
@property
7282
def proxy(self):
7383
"""
@@ -162,9 +172,9 @@ def __get_connection(self):
162172
:rtype HTTPSConnection
163173
"""
164174
if self._http_proxy is not None:
165-
connection = http.client.HTTPSConnection(self._http_proxy.host, self._http_proxy.port)
175+
connection = http.client.HTTPSConnection(self._http_proxy.host, self._http_proxy.port, timeout=self.__timeout)
166176
connection.set_tunnel(self._endpoint.host, 443)
167177
else:
168-
connection = http.client.HTTPSConnection(self._endpoint.host)
178+
connection = http.client.HTTPSConnection(self._endpoint.host, timeout=self.__timeout)
169179

170180
return connection

socketlabs/injectionapi/socketlabsclient.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(self, server_id: int, api_key: str, proxy: Proxy = None):
3131
self._server_id = server_id
3232
self._api_key = api_key
3333
self._http_proxy = proxy
34+
self._request_timeout = 120
3435

3536
@property
3637
def __endpoint(self):
@@ -49,14 +50,32 @@ def __proxy(self):
4950
:rtype Proxy
5051
"""
5152
return self._http_proxy
53+
54+
@property
55+
def request_timeout(self):
56+
"""
57+
The SocketLabs Injection API timeout
58+
:return the Http timeout for the HTTP request
59+
:rtype int
60+
"""
61+
return self._request_timeout
62+
63+
@request_timeout.setter
64+
def request_timeout(self, timeout: int):
65+
"""
66+
Set the request_timeout to use when making the HTTP request
67+
:param timeout: the request_timeout to use for the HTTP request
68+
:type timeout: int
69+
"""
70+
self._request_timeout = timeout
5271

5372
def __build_http_request(self):
5473
"""
5574
Build the HttpRequest. Will add the proxy, if set
5675
:return the HttpRequest object to use for the request
5776
:rtype HttpRequest
5877
"""
59-
req = HttpRequest(HttpRequest.HttpRequestMethod.POST, self.__endpoint)
78+
req = HttpRequest(HttpRequest.HttpRequestMethod.POST, self.__endpoint, self.request_timeout)
6079
if self._http_proxy is not None:
6180
req.proxy = self._http_proxy
6281
return req

0 commit comments

Comments
 (0)