def process_tpf_query_text(query_text, raw_repo_uri, call_name, extraMetadata): """Generates a swagger specification item based on the given TPF query file.""" query_metadata = gquery.get_yaml_decorators(query_text) tags = query_metadata['tags'] if 'tags' in query_metadata else [] glogger.debug("Read query tags: " + ', '.join(tags)) summary = query_metadata['summary'] if 'summary' in query_metadata else "" glogger.debug("Read query summary: " + summary) description = query_metadata['description'] if 'description' in query_metadata else "" glogger.debug("Read query description: " + description) method = query_metadata['method'].lower() if 'method' in query_metadata else "get" if method not in ['get', 'post', 'head', 'put', 'delete', 'options', 'connect']: method = "get" pagination = query_metadata['pagination'] if 'pagination' in query_metadata else "" glogger.debug("Read query pagination: " + str(pagination)) endpoint = query_metadata['endpoint'] if 'endpoint' in query_metadata else "" glogger.debug("Read query endpoint: " + endpoint) # If this query allows pagination, add page number as parameter params = [] if pagination: params.append(pageUtils.getSwaggerPaginationDef(pagination)) item = packItem('/' + call_name, method, tags, summary, description, params, query_metadata, extraMetadata) return item
def process_sparql_query_text(query_text, loader, call_name, extraMetadata): """Generates a swagger specification item based on the given SPARQL query file.""" # We get the endpoint name first, since some query metadata fields (eg enums) require it endpoint, _ = gquery.guess_endpoint_uri(query_text, loader) glogger.debug("Read query endpoint: {}".format(endpoint)) try: query_metadata = gquery.get_metadata(query_text, endpoint) except Exception as e: raise Exception('Could not parse query {}: {}'.format(call_name, str(e))) tags = query_metadata['tags'] if 'tags' in query_metadata else [] summary = query_metadata['summary'] if 'summary' in query_metadata else "" description = query_metadata['description'] if 'description' in query_metadata else "" method = query_metadata['method'].lower() if 'method' in query_metadata else "" if method not in ['get', 'post', 'head', 'put', 'delete', 'options', 'connect']: method = "" pagination = query_metadata['pagination'] if 'pagination' in query_metadata else "" endpoint_in_url = query_metadata['endpoint_in_url'] if 'endpoint_in_url' in query_metadata else True # Processing of the parameters params = [] # PV properties item_properties = {} # If this query allows pagination, add page number as parameter if pagination: params.append(pageUtils.getSwaggerPaginationDef(pagination)) if query_metadata['type'] in ['SelectQuery', 'ConstructQuery', 'InsertData']: # TODO: do something intelligent with the parameters! # As per #3, prefetching IRIs via SPARQL and filling enum parameters = query_metadata['parameters'] for _, p in list(parameters.items()): param = {} param['name'] = p['name'] param['type'] = p['type'] param['required'] = p['required'] param['in'] = "query" param['description'] = "A value of type {} that will substitute {} in the original query".format( p['type'], p['original']) if 'lang' in p: param['description'] = "A value of type {}@{} that will substitute {} in the original query".format( p['type'], p['lang'], p['original']) if 'format' in p: param['format'] = p['format'] param['description'] = "A value of type {} ({}) that will substitute {} in the original query".format( p['type'], p['format'], p['original']) if 'enum' in p: param['enum'] = p['enum'] if 'default' in p: param['default'] = p['default'] params.append(param) if endpoint_in_url: endpoint_param = {} endpoint_param['name'] = "endpoint" endpoint_param['type'] = "string" endpoint_param['in'] = "query" endpoint_param['description'] = "Alternative endpoint for SPARQL query" endpoint_param['default'] = endpoint params.append(endpoint_param) # If this is a URL generated spec we need to force API calls with the specUrl parameter set if type(loader) is URLLoader: specUrl_param = {} specUrl_param['name'] = "specUrl" specUrl_param['type'] = "string" specUrl_param['in'] = "query" specUrl_param['description'] = "URL of the API specification" specUrl_param['default'] = loader.getRawRepoUri() params.append(specUrl_param) if query_metadata['type'] == 'SelectQuery': # Fill in the spec for SELECT if not method: method = 'get' for pv in query_metadata['variables']: item_properties[pv] = { "name": pv, "type": "object", "required": ["type", "value"], "properties": { "type": { "type": "string" }, "value": { "type": "string" }, "xml:lang": { "type": "string" }, "datatype": { "type": "string" } } } elif query_metadata['type'] == 'ConstructQuery': if not method: method = 'get' elif query_metadata['type'] == 'UNKNOWN': glogger.warning("grlc could not parse this query; assuming a plain, non-parametric SELECT in the API spec") if not method: method = 'get' else: # TODO: process all other kinds of queries raise Exception('Could not parse query {}: Query of type {} is currently unsupported'.format(call_name, query_metadata['type'])) # Finally: main structure of the callname spec item = packItem('/' + call_name, method, tags, summary, description, params, query_metadata, extraMetadata) return item
def process_sparql_query_text(query_text, loader, call_name, extraMetadata): # We get the endpoint name first, since some query metadata fields (eg enums) require it endpoint, auth = gquery.guess_endpoint_uri(query_text, loader) glogger.debug("Read query endpoint: {}".format(endpoint)) try: query_metadata = gquery.get_metadata(query_text, endpoint) except Exception: raw_repo_uri = loader.getRawRepoUri() raw_query_uri = raw_repo_uri + ' / ' + call_name glogger.error("Could not parse query at {}".format(raw_query_uri)) glogger.error(traceback.print_exc()) return None tags = query_metadata['tags'] if 'tags' in query_metadata else [] summary = query_metadata['summary'] if 'summary' in query_metadata else "" description = query_metadata['description'] if 'description' in query_metadata else "" method = query_metadata['method'].lower() if 'method' in query_metadata else "" if method not in ['get', 'post', 'head', 'put', 'delete', 'options', 'connect']: method = "" pagination = query_metadata['pagination'] if 'pagination' in query_metadata else "" endpoint_in_url = query_metadata['endpoint_in_url'] if 'endpoint_in_url' in query_metadata else True projection = loader.getProjectionForQueryName(call_name) # Processing of the parameters params = [] # PV properties item_properties = {} # If this query allows pagination, add page number as parameter if pagination: params.append(pageUtils.getSwaggerPaginationDef(pagination)) if query_metadata['type'] == 'SelectQuery' or query_metadata['type'] == 'ConstructQuery' or query_metadata['type'] == 'InsertData': # TODO: do something intelligent with the parameters! # As per #3, prefetching IRIs via SPARQL and filling enum parameters = query_metadata['parameters'] for v, p in list(parameters.items()): param = {} param['name'] = p['name'] param['type'] = p['type'] param['required'] = p['required'] param['in'] = "query" param['description'] = "A value of type {} that will substitute {} in the original query".format(p['type'], p['original']) if 'lang' in p: param['description'] = "A value of type {}@{} that will substitute {} in the original query".format(p['type'], p['lang'], p['original']) if 'format' in p: param['format'] = p['format'] param['description'] = "A value of type {} ({}) that will substitute {} in the original query".format(p['type'], p['format'], p['original']) if 'enum' in p: param['enum'] = p['enum'] if 'default' in p: param['default'] = p['default'] params.append(param) if endpoint_in_url: endpoint_param = {} endpoint_param['name'] = "endpoint" endpoint_param['type'] = "string" endpoint_param['in'] = "query" endpoint_param['description'] = "Alternative endpoint for SPARQL query" endpoint_param['default'] = endpoint params.append(endpoint_param) if query_metadata['type'] == 'SelectQuery': # Fill in the spec for SELECT if not method: method = 'get' for pv in query_metadata['variables']: item_properties[pv] = { "name": pv, "type": "object", "required": ["type", "value"], "properties": { "type": { "type": "string" }, "value": { "type": "string" }, "xml:lang": { "type": "string" }, "datatype": { "type": "string" } } } elif query_metadata['type'] == 'ConstructQuery': if not method: method = 'get' elif query_metadata['type'] == 'UNKNOWN': glogger.warning("grlc could not parse this query; assuming a plain, non-parametric SELECT in the API spec") if not method: method = 'get' else: # TODO: process all other kinds of queries glogger.warning("Query of type {} is currently unsupported! Skipping".format(query_metadata['type'])) # Finally: main structure of the callname spec item = packItem('/' + call_name, method, tags, summary, description, params, query_metadata, extraMetadata, projection) return item