Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
DuniterPy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
clients
python
DuniterPy
Commits
bd3c686c
Commit
bd3c686c
authored
6 years ago
by
Vincent Texier
Browse files
Options
Downloads
Patches
Plain Diff
issue
#56
WIP - Can choose the response type for Client.get()
parent
be9c65b7
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
duniterpy/api/client.py
+19
-3
19 additions, 3 deletions
duniterpy/api/client.py
examples/request_data.py
+28
-5
28 additions, 5 deletions
examples/request_data.py
with
47 additions
and
8 deletions
duniterpy/api/client.py
+
19
−
3
View file @
bd3c686c
...
...
@@ -11,6 +11,12 @@ import duniterpy.api.endpoint as endpoint
logger
=
logging
.
getLogger
(
"
duniter
"
)
# Response type constants
RESPONSE_JSON
=
'
json
'
RESPONSE_TEXT
=
'
text
'
RESPONSE_AIOHTTP
=
'
aiohttp
'
# jsonschema validator
ERROR_SCHEMA
=
{
"
type
"
:
"
object
"
,
"
properties
"
:
{
...
...
@@ -190,12 +196,13 @@ class Client:
self
.
session
=
session
self
.
proxy
=
proxy
async
def
get
(
self
,
url_path
:
str
,
params
:
dict
=
None
,
schema
:
dict
=
None
)
->
any
:
async
def
get
(
self
,
url_path
:
str
,
params
:
dict
=
None
,
rtype
:
str
=
RESPONSE_JSON
,
schema
:
dict
=
None
)
->
any
:
"""
Get request on self.endpoint + url_path
:param url_path: Url encoded path following the endpoint
:param params: Url query string parameters dictionary
:param rtype: Response type
:param schema: Json Schema to validate response (optional, default None)
:return:
"""
...
...
@@ -204,11 +211,20 @@ class Client:
client
=
API
(
self
.
endpoint
.
conn_handler
(
self
.
session
,
self
.
proxy
),
''
)
# get aiohttp response
response
=
await
client
.
requests_get
(
url_path
,
**
params
)
# if schema supplied...
if
schema
is
not
None
:
return
await
parse_response
(
response
,
schema
)
else
:
# validate response
await
parse_response
(
response
,
schema
)
# return the chosen type
if
rtype
==
RESPONSE_AIOHTTP
:
return
response
elif
rtype
==
RESPONSE_TEXT
:
return
await
response
.
text
()
elif
rtype
==
RESPONSE_JSON
:
return
await
response
.
json
()
async
def
close
(
self
):
...
...
This diff is collapsed.
Click to expand it.
examples/request_data.py
+
28
−
5
View file @
bd3c686c
import
asyncio
from
duniterpy.api.client
import
Client
from
duniterpy.api.client
import
Client
,
RESPONSE_AIOHTTP
from
duniterpy.api
import
bma
# CONFIG #######################################
...
...
@@ -19,10 +19,6 @@ async def main():
# Create Client from endpoint string in Duniter format
client
=
Client
(
BMAS_ENDPOINT
)
# Get the node summary infos (direct REST GET request)
response
=
await
client
.
get
(
'
node/summary
'
)
print
(
response
)
# Get the node summary infos by dedicated method (with json schema validation)
response
=
await
client
(
bma
.
node
.
summary
)
print
(
response
)
...
...
@@ -39,6 +35,33 @@ async def main():
response
=
await
client
(
bma
.
blockchain
.
block
,
10
)
print
(
response
)
# jsonschema validator
summary_schema
=
{
"
type
"
:
"
object
"
,
"
properties
"
:
{
"
duniter
"
:
{
"
type
"
:
"
object
"
,
"
properties
"
:
{
"
software
"
:
{
"
type
"
:
"
string
"
},
"
version
"
:
{
"
type
"
:
"
string
"
,
},
"
forkWindowSize
"
:
{
"
type
"
:
"
number
"
}
},
"
required
"
:
[
"
software
"
,
"
version
"
]
},
},
"
required
"
:
[
"
duniter
"
]
}
# Get the node summary infos (direct REST GET request)
response
=
await
client
.
get
(
'
node/summary
'
,
rtype
=
RESPONSE_AIOHTTP
,
schema
=
summary_schema
)
print
(
response
)
# Close client aiohttp session
await
client
.
close
()
...
...
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