|
| 1 | +try: |
| 2 | + import requests |
| 3 | +except ImportError: |
| 4 | + raise ImportError( |
| 5 | + "requests package is required for Quiver Cloud backend.\n" |
| 6 | + "You can install it using: pip install requests" |
| 7 | + ) |
| 8 | + |
| 9 | +from ..utils.schema_printer import print_schema |
| 10 | + |
| 11 | +from .base import GraphQLBackend |
| 12 | +from .compiled import GraphQLCompiledDocument |
| 13 | + |
| 14 | +from six import urlparse |
| 15 | + |
| 16 | +GRAPHQL_QUERY = """ |
| 17 | +mutation($schemaDsl: String!, $query: String!) { |
| 18 | + generateCode( |
| 19 | + schemaDsl: $schemaDsl |
| 20 | + query: $query, |
| 21 | + language: PYTHON, |
| 22 | + pythonOptions: { |
| 23 | + asyncFramework: PROMISE |
| 24 | + } |
| 25 | + ) { |
| 26 | + code |
| 27 | + compilationTime |
| 28 | + errors { |
| 29 | + type |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | +""" |
| 34 | + |
| 35 | + |
| 36 | +class GraphQLQuiverCloudBackend(GraphQLBackend): |
| 37 | + def __init__(self, dsn, python_options=None, **options): |
| 38 | + super(GraphQLQuiverCloudBackend, self).__init__(**options) |
| 39 | + try: |
| 40 | + url = urlparse(dsn.strip()) |
| 41 | + except Exception: |
| 42 | + raise Exception("Received wrong url {}".format(dsn)) |
| 43 | + |
| 44 | + netloc = url.hostname |
| 45 | + if url.port: |
| 46 | + netloc += ":%s" % url.port |
| 47 | + |
| 48 | + path_bits = url.path.rsplit("/", 1) |
| 49 | + if len(path_bits) > 1: |
| 50 | + path = path_bits[0] |
| 51 | + else: |
| 52 | + path = "" |
| 53 | + |
| 54 | + self.api_url = "%s://%s%s" % (url.scheme.rsplit("+", 1)[-1], netloc, path) |
| 55 | + self.public_key = url.username |
| 56 | + self.secret_key = url.password |
| 57 | + self.extra_namespace = {} |
| 58 | + if python_options is None: |
| 59 | + python_options = {} |
| 60 | + wait_for_promises = python_options.pop("wait_for_promises", None) |
| 61 | + if wait_for_promises: |
| 62 | + assert callable(wait_for_promises), "wait_for_promises must be callable." |
| 63 | + self.extra_namespace["wait_for_promises"] = wait_for_promises |
| 64 | + self.python_options = python_options |
| 65 | + |
| 66 | + def make_post_request(self, url, auth, json_payload): |
| 67 | + """This function executes the request with the provided |
| 68 | + json payload and return the json response""" |
| 69 | + response = requests.post(url, auth=auth, json=json_payload) |
| 70 | + return response.json() |
| 71 | + |
| 72 | + def generate_source(self, schema, query): |
| 73 | + variables = {"schemaDsl": print_schema(schema), "query": query} |
| 74 | + |
| 75 | + json_response = self.make_post_request( |
| 76 | + "{}/graphql".format(self.api_url), |
| 77 | + auth=(self.public_key, self.secret_key), |
| 78 | + json_payload={"query": GRAPHQL_QUERY, "variables": variables}, |
| 79 | + ) |
| 80 | + |
| 81 | + errors = json_response.get('errors') |
| 82 | + if errors: |
| 83 | + raise Exception(errors[0].get('message')) |
| 84 | + data = json_response.get("data", {}) |
| 85 | + code_generation = data.get("generateCode", {}) |
| 86 | + code = code_generation.get("code") |
| 87 | + if not code: |
| 88 | + raise Exception("Cant get the code. Received json from Quiver Cloud") |
| 89 | + code = str(code) |
| 90 | + return code |
| 91 | + |
| 92 | + def document_from_string(self, schema, request_string): |
| 93 | + source = self.generate_source(schema, request_string) |
| 94 | + filename = "<document>" |
| 95 | + code = compile(source, filename, "exec") |
| 96 | + |
| 97 | + def uptodate(): |
| 98 | + return True |
| 99 | + |
| 100 | + document = GraphQLCompiledDocument.from_code( |
| 101 | + schema, code, uptodate, self.extra_namespace |
| 102 | + ) |
| 103 | + return document |
0 commit comments