def get_data(self, queries: List[ResourceQuery]) -> List[ResourceResult]: """Method to post the request for geo, temporal, property, add filters and make complex query. Args: queries (List[ResourceQuery]): A list of query objects of ResourceQuery class. Returns: rs_results (List[ResourceResult]): returns a list of ResourceResult object. """ url = self.url + "/entityOperations/query" zipped_url = [] for query in queries: zipped_url.append((url, query.get_query(), self.headers)) responses: List[HTTPResponse] = self.pool.starmap( HTTPEntity().post, zipped_url) rs_results = [] for response in responses: rs_result = ResourceResult() if response.get_status_code() == 401: raise RuntimeError("Not Authorized: Invalid Credentials") elif response.get_status_code() == 200: result_data = response.get_json() rs_result.type = result_data["type"] rs_result.title = result_data["title"] rs_result.results = result_data["results"] rs_results.append(rs_result) return rs_results
def get_latest(self, queries: List[ResourceQuery]) -> List[ResourceResult]: """Method to get the request for latest resource data. Args: query (ResourceQuery): A query object of ResourceQuery class. Returns: rs_result (ResourceResult): returns a ResourceResult object. """ base_url = self.url + "/entities" zipped_url = [] for query in queries: url = base_url + query.latest_search() zipped_url.append((url, self.headers)) responses: List[HTTPResponse] = self.pool.starmap( HTTPEntity().get, zipped_url) rs_results = [] for response in responses: rs_result = ResourceResult() if response.get_status_code() == 401: raise RuntimeError("Not Authorized: Invalid Credentials") elif response.get_status_code() == 200: result_data = response.get_json() rs_result.type = result_data["type"] rs_result.title = result_data["title"] rs_result.results = result_data["results"] rs_results.append(rs_result) return rs_results
def get_data_using_get(self, queries: List[ResourceQuery]) -> List[ResourceResult]: """ Get data using HTTP Get Args: queries (List[ResourceQuery]): A list of query objects of ResourceQuery class. Returns: rs_results (List[ResourceResult]): returns a list of ResourceResult object. """ url = self.url + "/entities" rs_results = [] zipped_url = [] offset = None limit = None for query in queries: offset, limit = query.get_offset_limit() new_url = url if (query._is_property_search): url += query.get_query_for_get() if offset is not None and limit is not None: new_url = url + "&offset=" + str(offset) + "&limit=" + str(limit) response = HTTPEntity().get(url=new_url, headers=self.headers) rs_results += self.parse_response([response]) return rs_results
def get_data(self, queries: List[ResourceQuery]) -> List[ResourceResult]: """Method to post the request for geo, temporal, property, add filters and make complex query. Args: queries (List[ResourceQuery]): A list of query objects of ResourceQuery class. Returns: rs_results (List[ResourceResult]): returns a list of ResourceResult object. """ url = self.url + "/temporal/entityOperations/query" rs_results = [] zipped_url = [] offset = None limit = None for query in queries: offset, limit = query.get_offset_limit() new_query = query.get_query() new_url = url if offset is not None and limit is not None: new_url = url + "?offset=" + str(offset) + "&limit=" + str(limit) zipped_url.append((new_url, new_query, self.headers)) responses: List[HTTPResponse] = self.pool.starmap( HTTPEntity().post, zipped_url ) rs_results = self.parse_response(responses) return rs_results
class HTTPEntityTest(unittest.TestCase): """Test different scenarios for the HTTPEntity class. """ def __init__(self, *args, **kwargs): """HTTPEntityTest base class constructor """ super(HTTPEntityTest, self).__init__(*args, **kwargs) self.http_entity = HTTPEntity() def test_get(self): """Function to test the 'get' method for HTTPEntity. """ self.testVector = {} with open("./tests/HTTPEntity/testVector_HTTPEntity.json", "r") as f: self.testVector = json.load(f) for param in self.testVector["params"]: result = self.http_entity.get( url=self.testVector["cat_url"] + "?" + param, headers=self.testVector["headers"]) self.assertEqual(result.get_status_code(), 200) self.assertNotEqual(result.get_status_code(), 500) self.assertNotEqual(result.get_status_code(), 404) self.assertNotEqual(result.get_status_code(), 400) print(result.get_json())
def count_entity(self, query: CatalogueQuery) -> CatalogueResult: """Method to get the count response for entities, based on a query. Args: query (CatalogueQuery): A query object of CatalogueQuery class. Returns: cat_result (CatalogueResult): returns a CatalogueResult object. """ url = self.url + "/count" url = url + "?" + query.get_query() http_entity = HTTPEntity() response: HTTPResponse = http_entity.get(url, self.headers) result_data = response.get_json() cat_result = CatalogueResult() if response.get_status_code() == 200: cat_result.documents = result_data["results"] cat_result.total_hits = result_data["totalHits"] cat_result.status = result_data["type"] return cat_result
def get_item(self, iid: str) -> CatalogueResult: """Pydoc heading. Args: argument (argument-type): argument-description Returns: returned-varaible (returned-varaible-type): return-variable-description """ url = self.url + "/item" url = url + "?" + "id=" + iid http_entity = HTTPEntity() response: HTTPResponse = http_entity.get(url, self.headers) result_data = response.get_json() cat_result = CatalogueResult() if response.get_status_code() == 200: cat_result.documents = result_data["results"] cat_result.total_hits = result_data["totalHits"] cat_result.status = result_data["type"] return cat_result
def list_entity(self, entity_type: str) -> CatalogueResult: """Method to get the list response for entities, based on an entity type. Args: entity_type (String): type must be either resource, resourceGroup, resourceServer. Returns: cat_result (CatalogueResult): returns a CatalogueResult object. """ url = self.url + "/list" url = url + "/" + entity_type http_entity = HTTPEntity() response: HTTPResponse = http_entity.get(url, self.headers) result_data = response.get_json() cat_result = CatalogueResult() if response.get_status_code() == 200: cat_result.documents = result_data["results"] cat_result.total_hits = result_data["totalHits"] cat_result.status = result_data["type"] return cat_result
def request_token(self) -> str: """ Method to request a token for the private resources Returns: access_token (String): Token to access the private resources """ if self.item is None: self.set_item("rs.iudx.org.in", "resource_server", "consumer") http_entity = HTTPEntity() response: HTTPResponse = http_entity.post(self.auth_url, json.dumps(self.item), self.headers) result_data = response.get_json() if response.get_status_code() == 200: self.credentials = result_data["results"] access_token = self.credentials["accessToken"] return access_token else: raise RuntimeError(result_data)
def get_related_entity(self, iid: str, rel: str) -> CatalogueResult: """Method to get the relationship response for entities, based on their id and relation. Args: iid (String): Id of the entity. rel (String): Relationship attribute of the entity whose id is provided. Returns: cat_result (CatalogueResult): returns a CatalogueResult object. """ url = self.url + "/relationship" url = url + "?" + "id=" + iid + "&" + "rel=" + rel http_entity = HTTPEntity() response: HTTPResponse = http_entity.get(url, self.headers) result_data = response.get_json() cat_result = CatalogueResult() if response.get_status_code() == 200: cat_result.documents = result_data["results"] cat_result.total_hits = result_data["totalHits"] cat_result.status = result_data["type"] return cat_result
def __init__(self, *args, **kwargs): """HTTPEntityTest base class constructor """ super(HTTPEntityTest, self).__init__(*args, **kwargs) self.http_entity = HTTPEntity()