Skip to content

Utils

Utils

Helper class for main api classes

build_url(url, extra_params=None) staticmethod

Builds a url with given parameters which will be used in requests.

Parameters:

Name Type Description Default
url str

base url.

required
extra_params dict

dictionary of query parameters.

None

Returns:

Type Description

A encoded url ready for the request

Source code in herepy/utils.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@staticmethod
def build_url(url, extra_params=None):
    """Builds a url with given parameters which will
    be used in requests.
    Args:
      url (str):
        base url.
      extra_params (dict):
        dictionary of query parameters.
    Returns:
      A encoded url ready for the request"""

    # Break url into constituent parts
    (scheme, netloc, path, params, query, fragment) = urlparse(url)

    # Add any additional query parameters to the query string
    params_length = len(extra_params)
    if extra_params and params_length > 0:
        extra_query = Utils.encode_parameters(extra_params)
        # Add it to the existing query
        if query:
            query += '&' + extra_query
        else:
            query = extra_query

    # Return the rebuilt URL
    return urlunparse((scheme, netloc, path, params, query, fragment))

encode_parameters(parameters) staticmethod

Return a string in key=value&key=value form. Values of None are not included in the output string.

Parameters:

Name Type Description Default
parameters dict

dictionary of query parameters to be converted.

required

Returns:

Type Description

A URL-encoded string in "key=value&key=value" form

Source code in herepy/utils.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@staticmethod
def encode_parameters(parameters):
    """Return a string in key=value&key=value form.
    Values of None are not included in the output string.
    Args:
      parameters (dict):
        dictionary of query parameters to be converted.
    Returns:
      A URL-encoded string in "key=value&key=value" form
    """
    if parameters is None:
        return None
    if not isinstance(parameters, dict):
        raise HEREError("`parameters` must be a dict.")
    else:
        return urlencode(dict((k, v) for k, v in parameters.items() if v is not None))