def __call__(self, request): """Perform the actual authentication""" if self.where == "qs": parts = urlparse(request.url) qs = parse_qs(parts.query) qs[self.key] = self.token request.url = urlunparse(( parts.scheme, parts.netloc, parts.path, parts.params, urlencode(qs), parts.fragment, )) elif self.where == "header": request.headers["Authorization"] = "Bearer {}".format(self.token) return request
def build_query_string(self, *args, **kwargs): """Build The query string using the search parameters""" logger.debug("Building the query string that will be used for search") if "raise_errors" in kwargs.keys(): del kwargs["raise_errors"] # . not allowed in eodag_search_key, replaced with %2E kwargs = {k.replace(".", "%2E"): v for k, v in kwargs.items()} queryables = self.get_queryables(kwargs) query_params = {} # Get all the search parameters that are recognised as queryables by the # provider (they appear in the queryables dictionary) for eodag_search_key, provider_search_key in queryables.items(): user_input = kwargs[eodag_search_key] if self.COMPLEX_QS_REGEX.match(provider_search_key): parts = provider_search_key.split("=") if len(parts) == 1: formatted_query_param = format_metadata( provider_search_key, *args, **kwargs ) if "{{" in provider_search_key: # json query string (for POST request) update_nested_dict( query_params, json.loads(formatted_query_param) ) else: query_params[eodag_search_key] = formatted_query_param else: provider_search_key, provider_value = parts query_params.setdefault(provider_search_key, []).append( format_metadata(provider_value, *args, **kwargs) ) else: query_params[provider_search_key] = user_input # Now get all the literal search params (i.e params to be passed "as is" # in the search request) # ignore additional_params if it isn't a dictionary literal_search_params = getattr(self.config, "literal_search_params", {}) if not isinstance(literal_search_params, dict): literal_search_params = {} # Now add formatted free text search parameters (this is for cases where a # complex query through a free text search parameter is available for the # provider and needed for the consumer) literal_search_params.update(self.format_free_text_search(**kwargs)) for provider_search_key, provider_value in literal_search_params.items(): if isinstance(provider_value, list): query_params.setdefault(provider_search_key, []).extend(provider_value) else: query_params.setdefault(provider_search_key, []).append(provider_value) # Build the final query string, in one go without quoting it # (some providers do not operate well with urlencoded and quoted query strings) return ( query_params, urlencode( query_params, doseq=True, quote_via=lambda x, *_args, **_kwargs: x ), )