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
3eb1154f
Commit
3eb1154f
authored
6 years ago
by
Vincent Texier
Browse files
Options
Downloads
Patches
Plain Diff
[enh]
#59
new Client.query method to request GraphQL API
parent
b9fa32c6
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
+106
-17
106 additions, 17 deletions
duniterpy/api/client.py
examples/request_graphql.py
+38
-0
38 additions, 0 deletions
examples/request_graphql.py
with
144 additions
and
17 deletions
duniterpy/api/client.py
+
106
−
17
View file @
3eb1154f
...
@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
...
@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import
json
import
json
import
logging
import
logging
from
typing
import
Callable
,
Union
,
Any
,
Optional
from
typing
import
Callable
,
Union
,
Any
,
Optional
,
Dict
import
jsonschema
import
jsonschema
from
aiohttp
import
ClientResponse
,
ClientSession
,
ClientWebSocketResponse
from
aiohttp
import
ClientResponse
,
ClientSession
,
ClientWebSocketResponse
...
@@ -60,7 +60,7 @@ def parse_text(text: str, schema: dict) -> Any:
...
@@ -60,7 +60,7 @@ def parse_text(text: str, schema: dict) -> Any:
return
data
return
data
def
parse_error
(
text
:
str
)
->
Any
:
def
parse_error
(
text
:
str
)
->
dict
:
"""
"""
Validate and parse the BMA answer from websocket
Validate and parse the BMA answer from websocket
...
@@ -237,7 +237,7 @@ class API:
...
@@ -237,7 +237,7 @@ class API:
return
url
+
path
return
url
+
path
async
def
requests_get
(
self
,
path
:
str
,
**
kwargs
)
->
ClientResponse
:
async
def
requests_get
(
self
,
path
:
str
,
**
kwargs
:
Any
)
->
ClientResponse
:
"""
"""
Requests GET wrapper in order to use API parameters.
Requests GET wrapper in order to use API parameters.
...
@@ -267,7 +267,7 @@ class API:
...
@@ -267,7 +267,7 @@ class API:
return
response
return
response
async
def
requests_post
(
self
,
path
:
str
,
**
kwargs
)
->
ClientResponse
:
async
def
requests_post
(
self
,
path
:
str
,
**
kwargs
:
Any
)
->
ClientResponse
:
"""
"""
Requests POST wrapper in order to use API parameters.
Requests POST wrapper in order to use API parameters.
...
@@ -285,6 +285,55 @@ class API:
...
@@ -285,6 +285,55 @@ class API:
proxy
=
self
.
connection_handler
.
proxy
,
proxy
=
self
.
connection_handler
.
proxy
,
timeout
=
15
,
timeout
=
15
,
)
)
if
response
.
status
!=
200
:
try
:
error_data
=
parse_error
(
await
response
.
text
())
raise
DuniterError
(
error_data
)
except
(
TypeError
,
jsonschema
.
ValidationError
)
as
e
:
raise
ValueError
(
"
status code != 200 => %d (%s)
"
%
(
response
.
status
,
(
await
response
.
text
()))
)
from
e
return
response
async
def
requests
(
self
,
method
:
str
=
"
GET
"
,
path
:
str
=
""
,
data
:
Optional
[
dict
]
=
None
,
_json
:
Optional
[
dict
]
=
None
,
)
->
ClientResponse
:
"""
Generic requests wrapper on aiohttp
:param method: the request http method
:param path: the path added to endpoint
:param data: data for form POST request
:param _json: json for json POST request
:rtype: aiohttp.ClientResponse
"""
url
=
self
.
reverse_url
(
self
.
connection_handler
.
http_scheme
,
path
)
if
data
is
not
None
:
logging
.
debug
(
"
%s : %s, data=%s
"
,
method
,
url
,
data
)
elif
_json
is
not
None
:
logging
.
debug
(
"
%s : %s, json=%s
"
,
method
,
url
,
_json
)
# http header to send json body
self
.
headers
[
"
Content-Type
"
]
=
"
application/json; charset=utf-8
"
else
:
logging
.
debug
(
"
%s : %s
"
,
method
,
url
)
response
=
await
self
.
connection_handler
.
session
.
request
(
method
,
url
,
data
=
data
,
json
=
_json
,
headers
=
self
.
headers
,
proxy
=
self
.
connection_handler
.
proxy
,
timeout
=
15
,
)
return
response
return
response
async
def
connect_ws
(
self
,
path
:
str
)
->
WSConnection
:
async
def
connect_ws
(
self
,
path
:
str
)
->
WSConnection
:
...
@@ -321,15 +370,15 @@ class Client:
...
@@ -321,15 +370,15 @@ class Client:
def
__init__
(
def
__init__
(
self
,
self
,
_endpoint
:
Union
[
str
,
endpoint
.
Endpoint
],
_endpoint
:
Union
[
str
,
endpoint
.
Endpoint
],
session
:
ClientSession
=
None
,
session
:
Optional
[
ClientSession
]
=
None
,
proxy
:
str
=
None
,
proxy
:
Optional
[
str
]
=
None
,
)
->
None
:
)
->
None
:
"""
"""
Init Client instance
Init Client instance
:param _endpoint: Endpoint string in duniter format
:param _endpoint: Endpoint string in duniter format
:param session: Aiohttp client session (optional, default None)
:param session: Aiohttp client session (optional, default None)
:param proxy: Proxy server as hostname:port
:param proxy: Proxy server as hostname:port
(optional, default None)
"""
"""
if
isinstance
(
_endpoint
,
str
):
if
isinstance
(
_endpoint
,
str
):
# Endpoint Protocol detection
# Endpoint Protocol detection
...
@@ -353,16 +402,16 @@ class Client:
...
@@ -353,16 +402,16 @@ class Client:
async
def
get
(
async
def
get
(
self
,
self
,
url_path
:
str
,
url_path
:
str
,
params
:
dict
=
None
,
params
:
Optional
[
dict
]
=
None
,
rtype
:
str
=
RESPONSE_JSON
,
rtype
:
str
=
RESPONSE_JSON
,
schema
:
dict
=
None
,
schema
:
Optional
[
dict
]
=
None
,
)
->
Any
:
)
->
Any
:
"""
"""
GET request on
self.
endpoint + url_path
GET request on endpoint
host
+ url_path
:param url_path: Url encoded path following the endpoint
:param url_path: Url encoded path following the endpoint
:param params: Url query string parameters dictionary
:param params: Url query string parameters dictionary
(optional, default None)
:param rtype: Response type
:param rtype: Response type
(optional, default RESPONSE_JSON)
:param schema: Json Schema to validate response (optional, default None)
:param schema: Json Schema to validate response (optional, default None)
:return:
:return:
"""
"""
...
@@ -391,16 +440,16 @@ class Client:
...
@@ -391,16 +440,16 @@ class Client:
async
def
post
(
async
def
post
(
self
,
self
,
url_path
:
str
,
url_path
:
str
,
params
:
dict
=
None
,
params
:
Optional
[
dict
]
=
None
,
rtype
:
str
=
RESPONSE_JSON
,
rtype
:
str
=
RESPONSE_JSON
,
schema
:
dict
=
None
,
schema
:
Optional
[
dict
]
=
None
,
)
->
Any
:
)
->
Any
:
"""
"""
POST request on
self.
endpoint + url_path
POST request on endpoint
host
+ url_path
:param url_path: Url encoded path following the endpoint
:param url_path: Url encoded path following the endpoint
:param params: Url query string parameters dictionary
:param params: Url query string parameters dictionary
(optional, default None)
:param rtype: Response type
:param rtype: Response type
(optional, default RESPONSE_JSON)
:param schema: Json Schema to validate response (optional, default None)
:param schema: Json Schema to validate response (optional, default None)
:return:
:return:
"""
"""
...
@@ -426,6 +475,46 @@ class Client:
...
@@ -426,6 +475,46 @@ class Client:
return
result
return
result
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
:param query: GraphQL query string
:param variables: Variables for the query (optional, default None)
:param rtype: Response type (optional, default RESPONSE_JSON)
:param schema: Json Schema to validate response (optional, default None)
:return:
"""
payload
=
{
"
query
"
:
query
}
# type: Dict[str, Union[str, dict]]
if
variables
is
not
None
:
payload
[
"
variables
"
]
=
variables
client
=
API
(
self
.
endpoint
.
conn_handler
(
self
.
session
,
self
.
proxy
))
# get aiohttp response
response
=
await
client
.
requests
(
"
POST
"
,
_json
=
payload
)
# if schema supplied...
if
schema
is
not
None
:
# validate response
await
parse_response
(
response
,
schema
)
# return the chosen type
result
=
response
# type: Any
if
rtype
==
RESPONSE_TEXT
:
result
=
await
response
.
text
()
elif
rtype
==
RESPONSE_JSON
:
result
=
await
response
.
json
()
return
result
async
def
connect_ws
(
self
,
path
:
str
=
""
)
->
WSConnection
:
async
def
connect_ws
(
self
,
path
:
str
=
""
)
->
WSConnection
:
"""
"""
Connect to a websocket in order to use API parameters
Connect to a websocket in order to use API parameters
...
...
This diff is collapsed.
Click to expand it.
examples/request_graphql.py
0 → 100644
+
38
−
0
View file @
3eb1154f
import
asyncio
from
duniterpy.api.client
import
Client
# CONFIG #######################################
# You can either use a complete defined endpoint : [NAME_OF_THE_API] [DOMAIN] [IPv4] [IPv6] [PORT]
# or the simple definition : [NAME_OF_THE_API] [DOMAIN] [PORT]
# Here we use the secure BASIC_MERKLED_API (BMAS)
SWAPI_ENDPOINT
=
"
BMAS swapi.graph.cool 443
"
################################################
async
def
main
():
client
=
Client
(
SWAPI_ENDPOINT
)
query
=
"""
query {
allFilms {
title,
characters {
name
}
}
}
"""
response
=
await
client
.
query
(
query
)
print
(
response
)
# Close client aiohttp session
await
client
.
close
()
# Latest duniter-python-api is asynchronous and you have to use asyncio, an asyncio loop and a "as" on the data.
# ( https://docs.python.org/3/library/asyncio.html )
asyncio
.
get_event_loop
().
run_until_complete
(
main
())
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