Skip to content

Commit a540627

Browse files
Add helper for using GraphQL API
1 parent f440fba commit a540627

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

main.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,35 @@ def _http_get_json_paginated(self, url: str) -> Iterable[Any]:
659659
f"Got {response.status} from {next_url!r}: {body!r}", response
660660
)
661661

662+
def _http_graphql(self, query: str, variables: dict[str, Any] = {}):
663+
request_body = json.dumps({
664+
"query": query,
665+
"variables": variables
666+
})
667+
668+
self.connection.request(
669+
method="POST",
670+
url="/graphql",
671+
headers={
672+
"Authorization": f"token {self.github_token}",
673+
"Accept": "application/vnd.github.v3+json",
674+
"User-Agent": "Github Access Manager",
675+
},
676+
body=request_body
677+
)
678+
# TODO: see _http_get() regarding unimplemented headers
679+
680+
with self.connection.getresponse() as response:
681+
if 200 <= response.status < 300:
682+
json_response = json.load(response)
683+
if "errors" in json_response:
684+
errors = json_response['errors']
685+
raise Exception("Got GraphQL errors", errors)
686+
return json_response['data']
687+
688+
body = response.read()
689+
raise Exception(f"Got {response.status} from {url!r}: {body!r}", response)
690+
662691
def get_organization(self, org: str) -> Organization:
663692
org_data: Dict[str, Any] = self._http_get_json(f"/orgs/{org}")
664693
default_repo_permission: str = org_data["default_repository_permission"]

0 commit comments

Comments
 (0)