-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathquota.py
More file actions
153 lines (140 loc) · 5.72 KB
/
quota.py
File metadata and controls
153 lines (140 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Copyright 2025 Planet Labs PBC.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import logging
from typing import AsyncIterator, Optional
from planet.clients.base import _BaseClient
from planet.constants import PLANET_BASE_URL
from planet.http import Session
from planet.models import Paged
BASE_URL = f'{PLANET_BASE_URL}/quota/v1'
LOGGER = logging.getLogger(__name__)
class Reservations(Paged):
"""Asynchronous iterator over reservations from a paged response."""
NEXT_KEY = '_next'
ITEMS_KEY = 'reservations'
class QuotaClient(_BaseClient):
"""High-level asynchronous access to Planet's quota API.
The Planet Quota Reservations API allows you to create, estimate, and view
existing quota reservations on the Planet platform for compatible products
including Planetary Variables, Analysis-Ready PlanetScope (ARPS), and select
PlanetScope imagery products.
Example:
```python
>>> import asyncio
>>> from planet import Session
>>>
>>> async def main():
... async with Session() as sess:
... cl = sess.client('quota')
... # use client here
...
>>> asyncio.run(main())
```
"""
def __init__(self, session: Session, base_url: Optional[str] = None):
"""
Parameters:
session: Open session connected to server.
base_url: The base URL to use. Defaults to production quota API
base url.
"""
super().__init__(session, base_url or BASE_URL)
def _reservations_url(self):
return f'{self._base_url}/reservations'
async def create_reservation(self, request: dict) -> dict:
"""Create a new quota reservation.
Parameters:
request: Quota reservation request specification.
Returns:
Description of the created reservation.
Raises:
planet.exceptions.APIError: On API error.
"""
url = self._reservations_url()
response = await self._session.request(method='POST',
url=url,
json=request)
return response.json()
async def estimate_quota(self, request: dict) -> dict:
"""Estimate quota requirements for a potential reservation.
Parameters:
request: Quota estimation request specification.
Returns:
Quota estimation details including projected costs and usage.
Raises:
planet.exceptions.APIError: On API error.
"""
url = f'{self._base_url}/estimate'
response = await self._session.request(method='POST',
url=url,
json=request)
return response.json()
async def list_reservations(self,
status: Optional[str] = None,
limit: int = 100) -> AsyncIterator[dict]:
"""Iterate through list of quota reservations.
Parameters:
status: Filter reservations by status (e.g., 'active', 'completed', 'cancelled').
limit: Maximum number of results to return. When set to 0, no
maximum is applied.
Yields:
Description of a quota reservation.
Raises:
planet.exceptions.APIError: On API error.
"""
url = self._reservations_url()
params = {}
if status:
params['status'] = status
response = await self._session.request(method='GET',
url=url,
params=params)
async for reservation in Reservations(response,
self._session.request,
limit=limit):
yield reservation
async def get_reservation(self, reservation_id: str) -> dict:
"""Get a quota reservation by ID.
Parameters:
reservation_id: Quota reservation identifier.
Returns:
Quota reservation details.
Raises:
planet.exceptions.APIError: On API error.
"""
url = f'{self._reservations_url()}/{reservation_id}'
response = await self._session.request(method='GET', url=url)
return response.json()
async def cancel_reservation(self, reservation_id: str) -> dict:
"""Cancel an existing quota reservation.
Parameters:
reservation_id: Quota reservation identifier.
Returns:
Updated reservation details.
Raises:
planet.exceptions.APIError: On API error.
"""
url = f'{self._reservations_url()}/{reservation_id}/cancel'
response = await self._session.request(method='POST', url=url)
return response.json()
async def get_quota_usage(self) -> dict:
"""Get current quota usage and limits.
Returns:
Current quota usage statistics and limits.
Raises:
planet.exceptions.APIError: On API error.
"""
url = f'{self._base_url}/usage'
response = await self._session.request(method='GET', url=url)
return response.json()