Skip to content

Utils

Utils

Bases: object

Helper class for main api classes

Source code in herepy/utils.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
57
58
59
60
61
62
63
64
class Utils(object):
    """Helper class for main api classes"""

    @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)
            )

    @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))

    @staticmethod
    def get_zipped_base64(content):
        content_bytes = content.encode("utf-8")
        content_zipped = zlib.compress(content_bytes)
        return base64.b64encode(content_zipped).decode("utf-8")

build_url(url, extra_params=None) staticmethod

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

Source code in herepy/utils.py
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
57
58
@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. Args: parameters (dict): dictionary of query parameters to be converted. Returns: 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
29
30
@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)
        )