sgqlc.endpoint module¶
Access GraphQL endpoints using Python¶
This package provide the following modules:
sgqlc.endpoint.base
: with abstract classsgqlc.endpoint.base.BaseEndpoint
and helpful logging utilities to transform errors into JSON objects.
sgqlc.endpoint.http
: concretesgqlc.endpoint.http.HTTPEndpoint
usingurllib.request.urlopen()
.
sgqlc.endpoint.requests
: concretesgqlc.endpoint.requests.RequestsEndpoint
usingrequests
.
sgqlc.endpoint.websocket
: concretesgqlc.endpoint.websocket.WebSocketEndpoint
usingwebsocket._core.create_connection()
.
- Unless otherwise stated the endpoints follow a pattern:
construct the endpoint giving constants such as address, timeout…
call the endpoint given an operation and variables
The given variables
must be a plain JSON-serializeable object
(dict with string keys and values being one of dict, list, tuple, str, int,
float, bool, None… – json.dumps()
is used)
and the keys must match exactly the variable names (no name conversion
is done, no dollar-sign prefix $
should be used).
Example using sgqlc.endpoint.http.HTTPEndpoint
:
#!/usr/bin/env python3
import sys
import json
from sgqlc.endpoint.http import HTTPEndpoint
try:
token, repo = sys.argv[1:]
except ValueError:
raise SystemExit('Usage: <token> <team/repo>')
query = '''
query GitHubRepoIssues($repoOwner: String!, $repoName: String!) {
repository(owner: $repoOwner, name: $repoName) {
issues(first: 100) {
nodes {
number
title
}
}
}
}
'''
owner, name = repo.split('/', 1)
variables = {
'repoOwner': owner,
'repoName': name,
}
url = 'https://api.github.com/graphql'
headers = {
'Authorization': 'bearer ' + token,
}
endpoint = HTTPEndpoint(url, headers)
data = endpoint(query, variables)
json.dump(data, sys.stdout, sort_keys=True, indent=2, default=str)
See more examples.
- license:
ISC