Skip to content

Commit 26a6919

Browse files
feat: HTTP header_mode implement, add example (#8)
1 parent 2096009 commit 26a6919

File tree

2 files changed

+131
-20
lines changed

2 files changed

+131
-20
lines changed

core/modules/http.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,22 @@ def set_server_url(self, url=None ,timeout=5):
251251
return result
252252
return {"response": "Missing arguments : url", "status": Status.ERROR}
253253

254-
def get(self, header_mode=0, timeout=60):
254+
def get(self, data="", header_mode=0, input_timeout=5, timeout=60):
255255
"""
256256
Function for sending HTTP GET request
257257
258258
Parameters
259259
----------
260+
data : str
261+
The full header for the GET request if header_mode is set.
260262
header_mode : int
261263
Customization of HTTP(S) request header)(default=0)
262264
0 --> Disable
263265
1 --> Enable
266+
input_timeout : int
267+
Input timeout (default=5)
268+
timeout : int
269+
Timeout (default=60)
264270
265271
Returns
266272
-------
@@ -276,11 +282,23 @@ def get(self, header_mode=0, timeout=60):
276282
status : int
277283
Status of the command.
278284
"""
279-
if header_mode == 1:
280-
return {"response": "Not implemented yet!", "status": Status.ERROR}
281-
282-
command = f'AT+QHTTPGET={timeout}'
283-
return self.atcom.send_at_comm(command,"OK")
285+
# Set the request header config.
286+
result = self.set_request_header_status(status=header_mode)
287+
if result["status"] == Status.SUCCESS:
288+
if header_mode == 1:
289+
# Send a GET request to the modem.
290+
command = f'AT+QHTTPGET={timeout},{len(data)},{input_timeout}'
291+
result = self.atcom.send_at_comm(command, "CONNECT", timeout=60)
292+
if result["status"] == Status.SUCCESS:
293+
# Send the request header.
294+
return self.atcom.send_at_comm(data, "OK", line_end=False)
295+
else:
296+
# Send a GET request without header.
297+
command = f'AT+QHTTPGET={timeout}'
298+
return self.atcom.send_at_comm(command,"OK")
299+
300+
# Return the result of request header if there is no SUCCESS.
301+
return result
284302

285303
def post(self, data, header_mode=0, input_timeout=5, timeout=60):
286304
"""
@@ -314,14 +332,15 @@ def post(self, data, header_mode=0, input_timeout=5, timeout=60):
314332
status : int
315333
Status of the command.
316334
"""
317-
if header_mode == 1:
318-
return {"response": "Not implemented yet!", "status": Status.ERROR}
319-
320-
command = f'AT+QHTTPPOST={len(data)},{input_timeout},{timeout}'
321-
result = self.atcom.send_at_comm(command,"CONNECT", timeout=timeout)
322-
335+
# Set the request header config.
336+
result = self.set_request_header_status(status=header_mode)
323337
if result["status"] == Status.SUCCESS:
324-
result = self.atcom.send_at_comm(data, "OK", line_end=False) # send data
338+
# Send a POST request to the modem.
339+
command = f'AT+QHTTPPOST={len(data)},{input_timeout},{timeout}'
340+
result = self.atcom.send_at_comm(command,"CONNECT", timeout=timeout)
341+
if result["status"] == Status.SUCCESS:
342+
# Send the request (header and) body.
343+
result = self.atcom.send_at_comm(data, "OK", line_end=False)
325344
return result
326345

327346
def post_from_file(self, file_path, header_mode=0, timeout=60):
@@ -390,14 +409,15 @@ def put(self, data, header_mode=0, input_timeout=5, timeout=60):
390409
status : int
391410
Status of the command.
392411
"""
393-
if header_mode == 1:
394-
return {"response": "Not implemented yet!", "status": Status.ERROR}
395-
396-
command = f'AT+QHTTPPUT={len(data)},{input_timeout},{timeout}'
397-
result = self.atcom.send_at_comm(command,"CONNECT")
398-
412+
# Set the request header config.
413+
result = self.set_request_header_status(status=header_mode)
399414
if result["status"] == Status.SUCCESS:
400-
result = self.atcom.send_at_comm(data, "OK", line_end=False) # send data
415+
# Send a PUT request to the modem.
416+
command = f'AT+QHTTPPUT={len(data)},{input_timeout},{timeout}'
417+
result = self.atcom.send_at_comm(command,"CONNECT")
418+
if result["status"] == Status.SUCCESS:
419+
# Send the request (header and) body.
420+
result = self.atcom.send_at_comm(data, "OK", line_end=False)
401421
return result
402422

403423
def put_from_file(self, file_path, file_type=0, header_mode=0, timeout=60):
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import json
2+
import time
3+
from core.modem import Modem
4+
from core.utils.debug import Debug
5+
from core.utils.status import Status
6+
7+
8+
# Provide your server details to send HTTP
9+
# requests. For testing purposes, you may
10+
# use "webhook.site".
11+
# Example:
12+
# Host: https://webhook.site
13+
# Query: /6112efa6-4f8c-4bdb-8fa3-5787cc02f80a?query_param=DATA
14+
SERVER_DETAILS = {
15+
'HOST': '[YOUR_SERVER_DOMAIN_ADDRESS]',
16+
'QUERY': '[YOUR_REQUEST_QUERY_ADDRESS]'
17+
}
18+
19+
# This messages are arbitrary, you can change
20+
# both attribute and data names.
21+
DATA_TO_POST = {
22+
'messageType': 'picocell_status',
23+
'message': 'Picocell can connect to the server.'
24+
}
25+
26+
DATA_TO_PUT = {
27+
'messageType': 'picocell_battery',
28+
'message': '90%'
29+
}
30+
31+
32+
# Initilaze the instances for the Picocell SDK.
33+
modem = Modem()
34+
debug = Debug()
35+
36+
# Send APN details.
37+
modem.network.set_apn()
38+
39+
# Check the network registration.
40+
result = modem.network.check_network_registration()
41+
if result["status"] != Status.SUCCESS:
42+
debug.error("Could not connected to the cellular network.")
43+
44+
45+
# Set the first HTTP context.
46+
modem.http.set_context_id()
47+
48+
# Activate PDP.
49+
modem.network.deactivate_pdp_context()
50+
modem.network.activate_pdp_context()
51+
52+
# Set server URL.
53+
debug.info("Set Server URL: ", modem.http.set_server_url(SERVER_DETAILS["HOST"]))
54+
55+
# Send a POST request.
56+
data_post_json = json.dumps(DATA_TO_POST)
57+
header = "POST " + SERVER_DETAILS["QUERY"] + " HTTP/1.1\n" + \
58+
"Host: " + SERVER_DETAILS["HOST"][8:] + "\n" + \
59+
"Custom-Header-Name: Custom-Data\n" + \
60+
"Content-Type: application/json\n" + \
61+
"Content-Length: " + str(len(data_post_json) + 1) + "\n" + \
62+
"\n\n"
63+
64+
debug.info("Send POST Request: ", modem.http.post(data=header+data_post_json, header_mode=1))
65+
66+
# Wait for the modem to be ready.
67+
time.sleep(5)
68+
69+
# Send a GET request.
70+
header = "GET " + SERVER_DETAILS["QUERY"] + " HTTP/1.1\n" + \
71+
"Host: " + SERVER_DETAILS["HOST"][8:] + "\n" + \
72+
"Content-Type: text/plain\n" + \
73+
"Content-Length: 0\n" + \
74+
"Custom-Header-Name: Custom-Data\n" + \
75+
"\n\n"
76+
77+
debug.info("Send GET Request: ", modem.http.get(data=header, header_mode=1))
78+
79+
# Wait for the modem to be ready.
80+
time.sleep(5)
81+
82+
# Send a PUT request.
83+
data_put_json = json.dumps(DATA_TO_PUT)
84+
header = "PUT " + SERVER_DETAILS["QUERY"] + " HTTP/1.1\n" + \
85+
"Host: " + SERVER_DETAILS["HOST"][8:] + "\n" + \
86+
"Custom-Header-Name: Custom-Data\n" + \
87+
"Content-Type: application/json\n" + \
88+
"Content-Length: " + str(len(data_put_json) + 1) + "\n" + \
89+
"\n\n"
90+
91+
debug.info("Send PUT Request: ", modem.http.put(data=header+data_put_json, header_mode=1))

0 commit comments

Comments
 (0)