def _make_search_request(base: str, query: dict, page_state: dict, config: dict): """ Do the first half of the "search_by_page" function, by making the call to CMR. Build a request and issue it, returning a json object Parameters: base (string): the CMR end point, the base of the URL before params query (dictionary): CMR parameters and their values page_state (dictionary): the current page to download config (dictionary): configurations settings responds to: * accept - the format for the return defaults to UMM-JSON Returns: JSON object with either data from CMR, or on error you get the error response """ # Build headers headers = _standard_headers_from_config(config) if 'Echo-Token' in headers: logger.info('Using a CMR-Token') if 'Authorization' in headers: logger.info('Using an Authorization token') if 'CMR-Scroll-Id' in page_state: logger.debug('Setting scroll id to %s.', page_state['CMR-Scroll-Id']) headers = common.conj(headers, {'CMR-Scroll-Id': page_state['CMR-Scroll-Id']}) accept = config.get('accept', 'application/vnd.nasa.cmr.umm_results+json') headers = common.conj(headers, {'Accept': accept}) # Build URL and make POST url = _cmr_query_url(base, None, page_state, config = config) logger.info(' - %s: %s', 'POST', url) obj_json = net.post(url, query, headers=headers) return obj_json
def test_conj(self): """Test the conj function""" self.assertEqual([3, 4], com.conj(None, [3, 4]), 'src was None') self.assertEqual([1, 2, 3, 4], com.conj([1, 2], [3, 4]), 'good src, lists') self.assertEqual((4, 3, 1, 2), com.conj((1, 2), (3, 4)), 'good src, tuples') self.assertEqual({ 'a': 'A', 'b': 'B' }, com.conj({'a': 'A'}, {'b': 'B'}), 'good src, dict')
def _cmr_query_url(base: str, query: dict, page_state: dict, config: dict = None): """ build a collection or granule search URL to CMR Parameters: base: CMR endpoint query: dictionary url parameters config: configurations, responds to: * env - sit, uat, ops, prod, production, or blank for production """ if query is None: query = {} if int(page_state['limit'])>2000: query = common.conj(query, {'scroll': 'true'}) query = common.conj(query, {'page_size': page_state['page_size']}) return _cmr_basic_url(base, query, config)
def clear_scroll(scroll_id, config: dict = None): """ This action is called to clear a scroll ID from CMR allowing CMR to free up memory associated with the current search. This call is the same as calling the following CURL command: curl -i -XPOST -H "Content-Type: application/json" \ https://cmr.earthdata.nasa.gov/search/clear-scroll \ -d '{ "scroll_id" : "xxxx"}' This API call must send " and not ' API call returns HTTP status code 204 when successful. Parameters: scroll_id(string/number): CMR Scroll ID config(dictionary) - used to make configurations changes Returns: error dictionary if there was a problem, otherwise a JSON object of response headers """ config = common.always(config) # Build headers headers = _standard_headers_from_config(config) headers = common.conj(headers, {'Content-Type': 'application/json'}) url = _cmr_basic_url('clear-scroll', None, config) data = '{"scroll_id": "' + str(scroll_id) + '"}' logger.info(" - %s: %s", 'POST', url) obj_json = net.post(url, data, headers=headers) if 'errors' in obj_json: errors = obj_json['errors'] for err in errors: logger.warning(" Error while clearing scroll: %s", err) return obj_json