1# coding=utf-8
2# --------------------------------------------------------------------------
3# Copyright (c) Microsoft Corporation. All rights reserved.
4# Licensed under the MIT License. See License.txt in the project root for license information.
5# Code generated by Microsoft (R) AutoRest Code Generator.
6# Changes may cause incorrect behavior and will be lost if the code is regenerated.
7# --------------------------------------------------------------------------
8from typing import TYPE_CHECKING
9import warnings
10
11from azure.core.exceptions import ClientAuthenticationError, HttpResponseError, ResourceExistsError, ResourceNotFoundError, map_error
12from azure.core.paging import ItemPaged
13from azure.core.pipeline import PipelineResponse
14from azure.core.pipeline.transport import HttpRequest, HttpResponse
15from azure.mgmt.core.exceptions import ARMErrorFormat
16
17from .. import models as _models
18
19if TYPE_CHECKING:
20    # pylint: disable=unused-import,ungrouped-imports
21    from typing import Any, Callable, Dict, Generic, Iterable, Optional, TypeVar
22
23    T = TypeVar('T')
24    ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]
25
26class UsagesOperations(object):
27    """UsagesOperations operations.
28
29    You should not instantiate this class directly. Instead, you should create a Client instance that
30    instantiates it for you and attaches it as an attribute.
31
32    :ivar models: Alias to model classes used in this operation group.
33    :type models: ~azure.mgmt.network.v2019_11_01.models
34    :param client: Client for service requests.
35    :param config: Configuration of service client.
36    :param serializer: An object model serializer.
37    :param deserializer: An object model deserializer.
38    """
39
40    models = _models
41
42    def __init__(self, client, config, serializer, deserializer):
43        self._client = client
44        self._serialize = serializer
45        self._deserialize = deserializer
46        self._config = config
47
48    def list(
49        self,
50        location,  # type: str
51        **kwargs  # type: Any
52    ):
53        # type: (...) -> Iterable["_models.UsagesListResult"]
54        """List network usages for a subscription.
55
56        :param location: The location where resource usage is queried.
57        :type location: str
58        :keyword callable cls: A custom type or function that will be passed the direct response
59        :return: An iterator like instance of either UsagesListResult or the result of cls(response)
60        :rtype: ~azure.core.paging.ItemPaged[~azure.mgmt.network.v2019_11_01.models.UsagesListResult]
61        :raises: ~azure.core.exceptions.HttpResponseError
62        """
63        cls = kwargs.pop('cls', None)  # type: ClsType["_models.UsagesListResult"]
64        error_map = {
65            401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError
66        }
67        error_map.update(kwargs.pop('error_map', {}))
68        api_version = "2019-11-01"
69        accept = "application/json"
70
71        def prepare_request(next_link=None):
72            # Construct headers
73            header_parameters = {}  # type: Dict[str, Any]
74            header_parameters['Accept'] = self._serialize.header("accept", accept, 'str')
75
76            if not next_link:
77                # Construct URL
78                url = self.list.metadata['url']  # type: ignore
79                path_format_arguments = {
80                    'location': self._serialize.url("location", location, 'str', pattern=r'^[-\w\._ ]+$'),
81                    'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'),
82                }
83                url = self._client.format_url(url, **path_format_arguments)
84                # Construct parameters
85                query_parameters = {}  # type: Dict[str, Any]
86                query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str')
87
88                request = self._client.get(url, query_parameters, header_parameters)
89            else:
90                url = next_link
91                query_parameters = {}  # type: Dict[str, Any]
92                request = self._client.get(url, query_parameters, header_parameters)
93            return request
94
95        def extract_data(pipeline_response):
96            deserialized = self._deserialize('UsagesListResult', pipeline_response)
97            list_of_elem = deserialized.value
98            if cls:
99                list_of_elem = cls(list_of_elem)
100            return deserialized.next_link or None, iter(list_of_elem)
101
102        def get_next(next_link=None):
103            request = prepare_request(next_link)
104
105            pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs)
106            response = pipeline_response.http_response
107
108            if response.status_code not in [200]:
109                map_error(status_code=response.status_code, response=response, error_map=error_map)
110                raise HttpResponseError(response=response, error_format=ARMErrorFormat)
111
112            return pipeline_response
113
114        return ItemPaged(
115            get_next, extract_data
116        )
117    list.metadata = {'url': '/subscriptions/{subscriptionId}/providers/Microsoft.Network/locations/{location}/usages'}  # type: ignore
118