├── .gitignore ├── MANIFEST.in ├── graphqlclient ├── __init__.py └── client.py ├── example_run.py ├── setup.py ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.egg-info 3 | *.pyc 4 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE 2 | include README.md 3 | -------------------------------------------------------------------------------- /graphqlclient/__init__.py: -------------------------------------------------------------------------------- 1 | from .client import * 2 | -------------------------------------------------------------------------------- /example_run.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from graphqlclient import GraphQLClient 4 | 5 | def main(): 6 | client = GraphQLClient('http://graphql-swapi.parseapp.com/') 7 | 8 | result = client.execute(''' 9 | { 10 | allFilms { 11 | films { 12 | title 13 | } 14 | } 15 | } 16 | ''') 17 | 18 | print(result) 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | with open('README.md', 'r') as fh: 4 | long_description = fh.read() 5 | 6 | setup(name='graphqlclient', 7 | version='0.2.4', 8 | description='Simple GraphQL client for Python 2.7+', 9 | long_description=long_description, 10 | long_description_content_type='text/markdown', 11 | url='https://github.com/prismagraphql/python-graphql-client', 12 | author='prisma.io', 13 | author_email='hello@prisma.io', 14 | license='MIT', 15 | packages=['graphqlclient'], 16 | install_requires=[ 17 | 'six', 18 | ], 19 | zip_safe=False) 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-graphql-client 2 | Simple GraphQL client for Python 2.7+ 3 | 4 | ## Install 5 | 6 | ```sh 7 | pip install graphqlclient 8 | ``` 9 | 10 | ## Usage 11 | 12 | 13 | ```py 14 | from graphqlclient import GraphQLClient 15 | 16 | client = GraphQLClient('http://graphql-swapi.parseapp.com/') 17 | 18 | result = client.execute(''' 19 | { 20 | allFilms { 21 | films { 22 | title 23 | } 24 | } 25 | } 26 | ''') 27 | 28 | print(result) 29 | ``` 30 | 31 | ### Authorization 32 | 33 | Authorization tokens can be added to the request using the client's `inject_token` method: 34 | 35 | ```py 36 | client.inject_token('very-long-and-secure-token') 37 | ``` 38 | 39 | which defaults to http header name `Authorization`. 40 | An alternative http header name for the token can be set by passing in the alternative header name, e.g. for `x-api-key`: 41 | 42 | ```py 43 | client.inject_token('very-long-and-secure-token','x-api-key') 44 | ``` 45 | 46 | ## License 47 | 48 | [MIT License](http://opensource.org/licenses/MIT) 49 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 graph.cool 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /graphqlclient/client.py: -------------------------------------------------------------------------------- 1 | from six.moves import urllib 2 | import json 3 | 4 | class GraphQLClient: 5 | def __init__(self, endpoint): 6 | self.endpoint = endpoint 7 | self.token = None 8 | self.headername = None 9 | 10 | def execute(self, query, variables=None): 11 | return self._send(query, variables) 12 | 13 | def inject_token(self, token, headername='Authorization'): 14 | self.token = token 15 | self.headername = headername 16 | 17 | def _send(self, query, variables): 18 | data = {'query': query, 19 | 'variables': variables} 20 | headers = {'Accept': 'application/json', 21 | 'Content-Type': 'application/json'} 22 | 23 | if self.token is not None: 24 | headers[self.headername] = '{}'.format(self.token) 25 | 26 | req = urllib.request.Request(self.endpoint, json.dumps(data).encode('utf-8'), headers) 27 | 28 | try: 29 | response = urllib.request.urlopen(req) 30 | return response.read().decode('utf-8') 31 | except urllib.error.HTTPError as e: 32 | print((e.read())) 33 | print('') 34 | raise e 35 | --------------------------------------------------------------------------------