def _remove_qs(self, url): ''' Removes a query string from a URL before signing. :param url: The URL to strip. :type url: str ''' scheme, netloc, path, query, fragment = urlsplit(url) return urlunsplit((scheme, netloc, path, '', fragment))
def resolve_remote(self, uri): """ Resolve a remote ``uri``. If called directly, does not check the store first, but after retrieving the document at the specified URI it will be saved in the store if :attr:`cache_remote` is True. .. note:: If the requests_ library is present, ``jsonschema`` will use it to request the remote ``uri``, so that the correct encoding is detected and used. If it isn't, or if the scheme of the ``uri`` is not ``http`` or ``https``, UTF-8 is assumed. Arguments: uri (str): The URI to resolve Returns: The retrieved document .. _requests: http://pypi.python.org/pypi/requests/ """ scheme = urlsplit(uri).scheme if scheme in self.handlers: result = self.handlers[scheme](uri) elif ( scheme in [u"http", u"https"] and requests and getattr(requests.Response, "json", None) is not None ): # Requests has support for detecting the correct encoding of # json over http if callable(requests.Response.json): result = requests.get(uri).json() else: result = requests.get(uri).json else: # Otherwise, pass off to urllib and assume utf-8 result = json.loads(urlopen(uri).read().decode("utf-8")) if self.cache_remote: self.store[uri] = result return result
def sign(url, app_id, app_secret, hash_meth='sha1', **params): ''' A signature method which generates the necessary Ofly parameters. :param app_id: The oFlyAppId, i.e. "application ID". :type app_id: str :param app_secret: The oFlyAppSecret, i.e. "shared secret". :type app_secret: str :param hash_meth: The hash method to use for signing, defaults to "sha1". :type hash_meth: str :param \*\*params: Additional parameters. :type \*\*\params: dict ''' hash_meth_str = hash_meth if hash_meth == 'sha1': hash_meth = sha1 elif hash_meth == 'md5': hash_meth = md5 else: raise TypeError('hash_meth must be one of "sha1", "md5"') now = datetime.utcnow() milliseconds = now.microsecond // 1000 time_format = '%Y-%m-%dT%H:%M:%S.{0}Z'.format(milliseconds) ofly_params = { 'oflyAppId': app_id, 'oflyHashMeth': hash_meth_str.upper(), 'oflyTimestamp': now.strftime(time_format) } url_path = urlsplit(url).path signature_base_string = app_secret + url_path + '?' if len(params): signature_base_string += get_sorted_params(params) + '&' signature_base_string += get_sorted_params(ofly_params) if not isinstance(signature_base_string, bytes): signature_base_string = signature_base_string.encode('utf-8') ofly_params['oflyApiSig'] = \ hash_meth(signature_base_string).hexdigest() all_params = dict(tuple(ofly_params.items()) + tuple(params.items())) return get_sorted_params(all_params)
def resolve_remote(self, uri): """ Resolve a remote ``uri``. Does not check the store first, but stores the retrieved document in the store if :attr:`RefResolver.cache_remote` is True. .. note:: If the requests_ library is present, ``jsonschema`` will use it to request the remote ``uri``, so that the correct encoding is detected and used. If it isn't, or if the scheme of the ``uri`` is not ``http`` or ``https``, UTF-8 is assumed. :argument str uri: the URI to resolve :returns: the retrieved document .. _requests: http://pypi.python.org/pypi/requests/ """ scheme = urlsplit(uri).scheme if scheme in self.handlers: result = self.handlers[scheme](uri) elif ( scheme in ["http", "https"] and requests and getattr(requests.Response, "json", None) is not None ): # Requests has support for detecting the correct encoding of # json over http if callable(requests.Response.json): result = requests.get(uri).json() else: result = requests.get(uri).json else: # Otherwise, pass off to urllib and assume utf-8 result = json.loads(urlopen(uri).read().decode("utf-8")) if self.cache_remote: self.store[uri] = result return result
def url2name(url): from os.path import basename url = url.split('|')[0] return basename(unquote(urlsplit(url)[2]))
def normalize(self, uri): return urlsplit(uri).geturl()