Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""LICENSE
2Copyright 2019 Hermann Krumrey <hermann@krumreyh.com>
4This file is part of puffotter.
6puffotter is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
11puffotter is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
16You should have received a copy of the GNU General Public License
17along with puffotter. If not, see <http://www.gnu.org/licenses/>.
18LICENSE"""
20import json
21import requests
22from typing import Dict, Any, Optional
25class GraphQlClient:
26 """
27 A simple API wrapper for GraphQL APIs
28 """
30 def __init__(self, api_url: str):
31 """
32 Initializes the GraphQL API wrapper
33 :param api_url: The API endpoint URL
34 """
35 self.api_url = api_url
37 def query(
38 self,
39 query_string: str,
40 variables: Optional[Dict[str, Any]]
41 ) -> Optional[Dict[str, Any]]:
42 """
43 Executes a GraphQL query
44 :param query_string: The query string to use
45 :param variables: The variables to send
46 :return: The response JSON, or None if an error occurred.
47 """
48 if variables is None:
49 variables = {}
51 resp = requests.post(self.api_url, json={
52 "query": query_string,
53 "variables": variables
54 })
55 if not resp.status_code < 300:
56 return None
57 else:
58 return json.loads(resp.text)