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
ecba0bec
Commit
ecba0bec
authored
5 years ago
by
Vincent Texier
Browse files
Options
Downloads
Patches
Plain Diff
[fix]
#59
fix format with black
parent
da6dbcac
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
duniterpy/api/client.py
+43
-18
43 additions, 18 deletions
duniterpy/api/client.py
with
43 additions
and
18 deletions
duniterpy/api/client.py
+
43
−
18
View file @
ecba0bec
...
...
@@ -174,12 +174,20 @@ class API:
error_data
=
parse_error
(
await
response
.
text
())
raise
DuniterError
(
error_data
)
except
(
TypeError
,
jsonschema
.
ValidationError
):
raise
ValueError
(
'
status code != 200 => %d (%s)
'
%
(
response
.
status
,
(
await
response
.
text
())))
raise
ValueError
(
"
status code != 200 => %d (%s)
"
%
(
response
.
status
,
(
await
response
.
text
()))
)
return
response
async
def
requests
(
self
,
method
:
str
=
'
GET
'
,
path
:
str
=
''
,
data
:
Optional
[
dict
]
=
None
,
_json
:
Optional
[
dict
]
=
None
)
->
ClientResponse
:
async
def
requests
(
self
,
method
:
str
=
"
GET
"
,
path
:
str
=
""
,
data
:
Optional
[
dict
]
=
None
,
_json
:
Optional
[
dict
]
=
None
,
)
->
ClientResponse
:
"""
Generic requests wrapper on aiohttp
...
...
@@ -196,7 +204,7 @@ class API:
elif
_json
is
not
None
:
logging
.
debug
(
f
"
{
method
}
:
{
url
}
, json=
{
_json
}
"
)
# http header to send json body
self
.
headers
[
'
Content-Type
'
]
=
'
application/json; charset=utf-8
'
self
.
headers
[
"
Content-Type
"
]
=
"
application/json; charset=utf-8
"
else
:
logging
.
debug
(
f
"
{
method
}
:
{
url
}
"
)
...
...
@@ -207,7 +215,7 @@ class API:
json
=
_json
,
headers
=
self
.
headers
,
proxy
=
self
.
connection_handler
.
proxy
,
timeout
=
15
timeout
=
15
,
)
return
response
...
...
@@ -234,8 +242,12 @@ class Client:
Main class to create an API client
"""
def
__init__
(
self
,
_endpoint
:
Union
[
str
,
endpoint
.
Endpoint
],
session
:
Optional
[
ClientSession
]
=
None
,
proxy
:
Optional
[
str
]
=
None
)
->
None
:
def
__init__
(
self
,
_endpoint
:
Union
[
str
,
endpoint
.
Endpoint
],
session
:
Optional
[
ClientSession
]
=
None
,
proxy
:
Optional
[
str
]
=
None
,
)
->
None
:
"""
Init Client instance
...
...
@@ -262,8 +274,13 @@ class Client:
self
.
session
=
session
self
.
proxy
=
proxy
async
def
get
(
self
,
url_path
:
str
,
params
:
Optional
[
dict
]
=
None
,
rtype
:
str
=
RESPONSE_JSON
,
schema
:
Optional
[
dict
]
=
None
)
->
Any
:
async
def
get
(
self
,
url_path
:
str
,
params
:
Optional
[
dict
]
=
None
,
rtype
:
str
=
RESPONSE_JSON
,
schema
:
Optional
[
dict
]
=
None
,
)
->
Any
:
"""
GET request on endpoint host + url_path
...
...
@@ -295,8 +312,13 @@ class Client:
return
result
async
def
post
(
self
,
url_path
:
str
,
params
:
Optional
[
dict
]
=
None
,
rtype
:
str
=
RESPONSE_JSON
,
schema
:
Optional
[
dict
]
=
None
)
->
Any
:
async
def
post
(
self
,
url_path
:
str
,
params
:
Optional
[
dict
]
=
None
,
rtype
:
str
=
RESPONSE_JSON
,
schema
:
Optional
[
dict
]
=
None
,
)
->
Any
:
"""
POST request on endpoint host + url_path
...
...
@@ -328,8 +350,13 @@ class Client:
return
result
async
def
query
(
self
,
query
:
str
,
variables
:
Optional
[
dict
]
=
None
,
rtype
:
str
=
RESPONSE_JSON
,
schema
:
Optional
[
dict
]
=
None
)
->
Any
:
async
def
query
(
self
,
query
:
str
,
variables
:
Optional
[
dict
]
=
None
,
rtype
:
str
=
RESPONSE_JSON
,
schema
:
Optional
[
dict
]
=
None
,
)
->
Any
:
"""
GraphQL query or mutation request on endpoint
...
...
@@ -339,17 +366,15 @@ class Client:
:param schema: Json Schema to validate response (optional, default None)
:return:
"""
payload
=
{
'
query
'
:
query
}
# type: Dict[str, Union[str, dict]]
payload
=
{
"
query
"
:
query
}
# type: Dict[str, Union[str, dict]]
if
variables
is
not
None
:
payload
[
'
variables
'
]
=
variables
payload
[
"
variables
"
]
=
variables
client
=
API
(
self
.
endpoint
.
conn_handler
(
self
.
session
,
self
.
proxy
))
# get aiohttp response
response
=
await
client
.
requests
(
'
POST
'
,
_json
=
payload
)
response
=
await
client
.
requests
(
"
POST
"
,
_json
=
payload
)
# if schema supplied...
if
schema
is
not
None
:
...
...
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