|
| 1 | +# pylint: disable=too-many-lines,too-many-statements |
| 2 | +# coding=utf-8 |
| 3 | +# -------------------------------------------------------------------------- |
| 4 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 6 | +# Code generated by Microsoft (R) AutoRest Code Generator. |
| 7 | +# Changes may cause incorrect behavior and will be lost if the code is regenerated. |
| 8 | +# -------------------------------------------------------------------------- |
| 9 | +import sys |
| 10 | +from typing import Any, AsyncIterable, Callable, Dict, Optional, Type, TypeVar |
| 11 | +import urllib.parse |
| 12 | + |
| 13 | +from azure.core.async_paging import AsyncItemPaged, AsyncList |
| 14 | +from azure.core.exceptions import ( |
| 15 | + ClientAuthenticationError, |
| 16 | + HttpResponseError, |
| 17 | + ResourceExistsError, |
| 18 | + ResourceNotFoundError, |
| 19 | + ResourceNotModifiedError, |
| 20 | + map_error, |
| 21 | +) |
| 22 | +from azure.core.pipeline import PipelineResponse |
| 23 | +from azure.core.rest import AsyncHttpResponse, HttpRequest |
| 24 | +from azure.core.tracing.decorator import distributed_trace |
| 25 | +from azure.core.utils import case_insensitive_dict |
| 26 | +from azure.mgmt.core.exceptions import ARMErrorFormat |
| 27 | + |
| 28 | +from ... import models as _models |
| 29 | +from ...operations._quota_usages_operations import build_list_request |
| 30 | + |
| 31 | +if sys.version_info >= (3, 9): |
| 32 | + from collections.abc import MutableMapping |
| 33 | +else: |
| 34 | + from typing import MutableMapping # type: ignore # pylint: disable=ungrouped-imports |
| 35 | +T = TypeVar("T") |
| 36 | +ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]] |
| 37 | + |
| 38 | + |
| 39 | +class QuotaUsagesOperations: |
| 40 | + """ |
| 41 | + .. warning:: |
| 42 | + **DO NOT** instantiate this class directly. |
| 43 | +
|
| 44 | + Instead, you should access the following operations through |
| 45 | + :class:`~azure.mgmt.postgresqlflexibleservers.aio.PostgreSQLManagementClient`'s |
| 46 | + :attr:`quota_usages` attribute. |
| 47 | + """ |
| 48 | + |
| 49 | + models = _models |
| 50 | + |
| 51 | + def __init__(self, *args, **kwargs) -> None: |
| 52 | + input_args = list(args) |
| 53 | + self._client = input_args.pop(0) if input_args else kwargs.pop("client") |
| 54 | + self._config = input_args.pop(0) if input_args else kwargs.pop("config") |
| 55 | + self._serialize = input_args.pop(0) if input_args else kwargs.pop("serializer") |
| 56 | + self._deserialize = input_args.pop(0) if input_args else kwargs.pop("deserializer") |
| 57 | + |
| 58 | + @distributed_trace |
| 59 | + def list(self, location_name: str, **kwargs: Any) -> AsyncIterable["_models.QuotaUsage"]: |
| 60 | + """Get quota usages at specified location in a given subscription. |
| 61 | +
|
| 62 | + :param location_name: The name of the location. Required. |
| 63 | + :type location_name: str |
| 64 | + :return: An iterator like instance of either QuotaUsage or the result of cls(response) |
| 65 | + :rtype: |
| 66 | + ~azure.core.async_paging.AsyncItemPaged[~azure.mgmt.postgresqlflexibleservers.models.QuotaUsage] |
| 67 | + :raises ~azure.core.exceptions.HttpResponseError: |
| 68 | + """ |
| 69 | + _headers = kwargs.pop("headers", {}) or {} |
| 70 | + _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) |
| 71 | + |
| 72 | + api_version: str = kwargs.pop("api_version", _params.pop("api-version", self._config.api_version)) |
| 73 | + cls: ClsType[_models.QuotaUsagesListResult] = kwargs.pop("cls", None) |
| 74 | + |
| 75 | + error_map: MutableMapping[int, Type[HttpResponseError]] = { |
| 76 | + 401: ClientAuthenticationError, |
| 77 | + 404: ResourceNotFoundError, |
| 78 | + 409: ResourceExistsError, |
| 79 | + 304: ResourceNotModifiedError, |
| 80 | + } |
| 81 | + error_map.update(kwargs.pop("error_map", {}) or {}) |
| 82 | + |
| 83 | + def prepare_request(next_link=None): |
| 84 | + if not next_link: |
| 85 | + |
| 86 | + _request = build_list_request( |
| 87 | + location_name=location_name, |
| 88 | + subscription_id=self._config.subscription_id, |
| 89 | + api_version=api_version, |
| 90 | + headers=_headers, |
| 91 | + params=_params, |
| 92 | + ) |
| 93 | + _request.url = self._client.format_url(_request.url) |
| 94 | + |
| 95 | + else: |
| 96 | + # make call to next link with the client's api-version |
| 97 | + _parsed_next_link = urllib.parse.urlparse(next_link) |
| 98 | + _next_request_params = case_insensitive_dict( |
| 99 | + { |
| 100 | + key: [urllib.parse.quote(v) for v in value] |
| 101 | + for key, value in urllib.parse.parse_qs(_parsed_next_link.query).items() |
| 102 | + } |
| 103 | + ) |
| 104 | + _next_request_params["api-version"] = self._config.api_version |
| 105 | + _request = HttpRequest( |
| 106 | + "GET", urllib.parse.urljoin(next_link, _parsed_next_link.path), params=_next_request_params |
| 107 | + ) |
| 108 | + _request.url = self._client.format_url(_request.url) |
| 109 | + _request.method = "GET" |
| 110 | + return _request |
| 111 | + |
| 112 | + async def extract_data(pipeline_response): |
| 113 | + deserialized = self._deserialize("QuotaUsagesListResult", pipeline_response) |
| 114 | + list_of_elem = deserialized.value |
| 115 | + if cls: |
| 116 | + list_of_elem = cls(list_of_elem) # type: ignore |
| 117 | + return deserialized.next_link or None, AsyncList(list_of_elem) |
| 118 | + |
| 119 | + async def get_next(next_link=None): |
| 120 | + _request = prepare_request(next_link) |
| 121 | + |
| 122 | + _stream = False |
| 123 | + pipeline_response: PipelineResponse = await self._client._pipeline.run( # pylint: disable=protected-access |
| 124 | + _request, stream=_stream, **kwargs |
| 125 | + ) |
| 126 | + response = pipeline_response.http_response |
| 127 | + |
| 128 | + if response.status_code not in [200]: |
| 129 | + map_error(status_code=response.status_code, response=response, error_map=error_map) |
| 130 | + error = self._deserialize.failsafe_deserialize(_models.ErrorResponse, pipeline_response) |
| 131 | + raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat) |
| 132 | + |
| 133 | + return pipeline_response |
| 134 | + |
| 135 | + return AsyncItemPaged(get_next, extract_data) |
0 commit comments