Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
IoT Platform
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IoT
IoT Platform
Commits
92d1e269
Commit
92d1e269
authored
6 months ago
by
Rodrigo Goncalves
Committed by
Guilherme Arthur Gerônimo
4 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Support for logging requests and responses in client
parent
b899546e
No related branches found
Branches containing commit
No related tags found
1 merge request
!20
Resolve "Data ingress for context information for tiki-wiki"
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
sample-clients/python/client.py
+15
-1
15 additions, 1 deletion
sample-clients/python/client.py
sample-clients/python/main.py
+2
-1
2 additions, 1 deletion
sample-clients/python/main.py
with
17 additions
and
2 deletions
sample-clients/python/client.py
+
15
−
1
View file @
92d1e269
...
...
@@ -5,16 +5,27 @@ import requests
import
json
class
SmartDataContextAPIClient
:
def
__init__
(
self
,
cert_file
,
key_file
,
url
=
"
https://iot.lisha.ufsc.br/
"
,
verifyCertificate
=
True
):
def
__init__
(
self
,
cert_file
,
key_file
,
url
=
"
https://iot.lisha.ufsc.br/
"
,
verifyCertificate
=
True
,
logfile
=
None
):
if
not
url
.
endswith
(
'
/
'
):
url
+=
'
/
'
self
.
_url
=
url
self
.
_client
=
requests
.
Session
()
self
.
_client
.
cert
=
(
cert_file
,
key_file
)
self
.
_client
.
verify
=
verifyCertificate
self
.
_logfile
=
logfile
if
not
verifyCertificate
:
requests
.
urllib3
.
disable_warnings
()
def
_log_request
(
self
,
method
,
url
,
headers
,
request_body
,
response_body
):
if
self
.
_logfile
:
with
open
(
self
.
_logfile
,
'
a
'
)
as
log_file
:
log_file
.
write
(
f
"
URL:
{
method
}
{
url
}
\n
"
)
log_file
.
write
(
f
"
Headers:
{
json
.
dumps
(
headers
,
indent
=
2
)
}
\n
"
)
if
request_body
:
log_file
.
write
(
f
"
Request Body:
{
json
.
dumps
(
request_body
,
indent
=
2
)
}
\n
"
)
log_file
.
write
(
f
"
Response Body:
{
response_body
}
\n
"
)
log_file
.
write
(
"
\n
"
+
"
-
"
*
50
+
"
\n
"
)
def
_doJsonPost
(
self
,
json_data
,
return_attribute
=
''
,
endpoint
=
"
api/v1_1/context.php
"
):
headers
=
{
'
Content-Type
'
:
'
application/json
'
...
...
@@ -23,6 +34,7 @@ class SmartDataContextAPIClient:
response
=
self
.
_client
.
post
(
url
,
headers
=
headers
,
json
=
json_data
)
try
:
response_json
=
response
.
json
()
self
.
_log_request
(
"
POST
"
,
url
,
headers
,
json_data
,
response
.
json
())
if
"
errors
"
in
response_json
:
raise
Exception
(
f
"
Error processing request:
{
json
.
dumps
(
response_json
[
'
errors
'
])
}
"
)
else
:
...
...
@@ -157,6 +169,7 @@ class SmartDataContextAPIClient:
response
=
self
.
_client
.
post
(
url
,
headers
=
headers
,
data
=
data
)
try
:
response_json
=
response
.
json
()
self
.
_log_request
(
"
POST
"
,
url
,
headers
,
{
"
data
"
:
"
binary data
"
},
response
.
json
())
if
"
errors
"
in
response_json
:
raise
Exception
(
f
"
Error processing request:
{
json
.
dumps
(
response_json
[
'
errors
'
])
}
"
)
else
:
...
...
@@ -177,6 +190,7 @@ class SmartDataContextAPIClient:
}
response
=
self
.
_client
.
post
(
url
,
headers
=
headers
,
json
=
json_request
)
try
:
self
.
_log_request
(
"
POST
"
,
url
,
dict
(
response
.
headers
),
json_request
,
""
)
response
.
raise_for_status
()
return
response
.
content
except
Exception
as
e
:
...
...
This diff is collapsed.
Click to expand it.
sample-clients/python/main.py
+
2
−
1
View file @
92d1e269
...
...
@@ -16,7 +16,8 @@ def generate_random_file(filename, size_in_mb):
# Create a new SmartDataContextClient, passing a custom URL for the API and the certificates to access the domain
# The parameter verifyCertificate allows using self-signed certificates for development and test environments.
# Should not be used in production ideally
client
=
SmartDataContextAPIClient
(
cert_file
=
CLIENT_CERTIFICATE
,
key_file
=
CLIENT_CERTIFICATE_KEY
,
url
=
API_URL
,
verifyCertificate
=
False
)
client
=
SmartDataContextAPIClient
(
cert_file
=
CLIENT_CERTIFICATE
,
key_file
=
CLIENT_CERTIFICATE_KEY
,
url
=
API_URL
,
verifyCertificate
=
False
,
logfile
=
"
requests.log
"
)
# Creates a new perene SmartDataContext (no start not end time). The minimum required parameters are the content
# and the features, with at least one feature (tags), which indicate tags to be associated with this SmartDataContext
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment