Skip to content

Commit 36bd863

Browse files
committed
Attempt to make sync and async versions
1 parent 6b308bb commit 36bd863

File tree

4 files changed

+123
-11
lines changed

4 files changed

+123
-11
lines changed

gqlmod_github/__init__.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import graphql
88

9-
from gqlmod.helpers.urllib import UrllibJsonProvider
109
from gqlmod.helpers.utils import walk_query, walk_variables, unwrap_type
1110

1211

@@ -25,27 +24,27 @@ def find_directive(ast_node, name):
2524
}
2625

2726

28-
class GitHubProvider(UrllibJsonProvider):
27+
class GitHubBaseProvider:
2928
endpoint = 'https://api.github.com/graphql'
3029

3130
def __init__(self, token=None):
3231
self.token = token
3332

34-
def build_request(self, query, variables):
33+
def _build_accept_header(self, variables):
3534
previews = variables.pop('__previews', None)
36-
req = super().build_request(query, variables)
3735
if previews:
3836
# XXX: Can github accept more than one preview at once?
39-
req.add_header('Accept', ', '.join(
37+
return ', '.join(
4038
f"application/vnd.github.{p}+json"
4139
for p in previews
42-
))
43-
return req
40+
)
41+
else:
42+
return "application/json"
4443

45-
def modify_request(self, req, variables):
46-
if self.token:
47-
req.add_header('Authorization', f"Bearer {self.token}")
44+
def _build_authorization_header(self, variables):
45+
return f"Bearer {self.token}"
4846

47+
# This can't be async
4948
def get_schema_str(self):
5049
with urllib.request.urlopen("https://developer.github.com/v4/public_schema/schema.public.graphql") as fobj:
5150
return fobj.read().decode('utf-8')
@@ -68,3 +67,32 @@ def codegen_extra_kwargs(self, gast, schema):
6867
return {
6968
'__previews': previews,
7069
}
70+
71+
72+
try:
73+
from gqlmod.helpers.urllib import UrllibJsonProvider
74+
except ImportError:
75+
pass
76+
else:
77+
class GitHubSyncProvider(GitHubBaseProvider, UrllibJsonProvider):
78+
def build_request(self, query, variables):
79+
req = super().build_request(query, variables)
80+
req.add_header('Accept', self._build_accept_header(variables))
81+
req.add_header('Authorization', self._build_authorization_header(variables))
82+
return req
83+
84+
85+
try:
86+
from gqlmod.helpers.aiohttp import AiohttpProvider
87+
except ImportError:
88+
pass
89+
else:
90+
class GitHubAsyncProvider(GitHubBaseProvider, AiohttpProvider):
91+
use_json = True
92+
93+
def modify_request_args(self, variables, kwargs):
94+
super().modify_request_args(kwargs)
95+
kwargs.setdefault('headers', {}).update({
96+
'Accept': self._build_accept_header(variables),
97+
'Authorization': self._build_authorization_header(variables)
98+
})

setup.cfg

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ tests_require =
3333
app =
3434
jwcrypto
3535
python-dateutil
36+
async =
37+
gqlmod[aiohttp]
38+
sync =
39+
3640

3741
[options.entry_points]
3842
graphql_providers =
39-
github = gqlmod_github:GitHubProvider
43+
github = gqlmod_github:GitHubSyncProvider
44+
github-sync = gqlmod_github:GitHubSyncProvider
45+
github-async = gqlmod_github:GitHubAsyncProvider
4046

4147
[flake8]
4248
select = C,E,F,W,B,B9

tests/queries_async.gql

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#~github-async~
2+
3+
query Login {
4+
viewer {
5+
login
6+
# primarySubjectId
7+
hovercard {
8+
contexts {
9+
message
10+
octicon
11+
}
12+
}
13+
}
14+
}
15+
16+
mutation start_check_run($repo:ID!, $sha:GitObjectID!) {
17+
createCheckRun(input: {
18+
headSha: $sha
19+
name: "gqlmod check"
20+
repositoryId: $repo
21+
status: IN_PROGRESS
22+
}) {
23+
checkRun {
24+
id
25+
}
26+
}
27+
}
28+
29+
mutation append_check_run($repo:ID!, $checkrun:ID!, $annotations:[CheckAnnotationData!]) {
30+
updateCheckRun(input: {
31+
repositoryId: $repo
32+
checkRunId: $checkrun
33+
output: {
34+
title: "gqlmod check"
35+
summary: "Running..."
36+
annotations: $annotations
37+
}
38+
}) {
39+
checkRun {
40+
id
41+
}
42+
}
43+
}
44+
45+
mutation complete_check_run($repo:ID!, $checkrun:ID!, $state:CheckConclusionState) {
46+
updateCheckRun(input: {
47+
repositoryId: $repo
48+
checkRunId: $checkrun
49+
status: COMPLETED
50+
conclusion: $state # Probably want either FAILURE or SUCCESS
51+
output: {
52+
title: "gqlmod check"
53+
summary: "Finished"
54+
}
55+
}) {
56+
checkRun {
57+
id
58+
}
59+
}
60+
}
61+
62+
query get_label($repo: ID!, $name: String!) {
63+
node(id: $repo) {
64+
... on Repository {
65+
label(name: $name) {
66+
id
67+
}
68+
}
69+
}
70+
}

tests/test_github.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@
33

44
def test_get_schema():
55
assert gqlmod.providers.query_for_schema('github')
6+
assert gqlmod.providers.query_for_schema('github-sync')
7+
assert gqlmod.providers.query_for_schema('github-async')
68

79

810
def test_import():
911
gqlmod.enable_gql_import()
1012
import tests.queries # noqa
1113
# TODO: Actually check previews got detected
14+
15+
16+
def test_async_import():
17+
gqlmod.enable_gql_import()
18+
import tests.queries_async # noqa
19+
# TODO: Actually check previews got detected

0 commit comments

Comments
 (0)