| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# File generated from our OpenAPI spec |
| 3 |
from stripe._createable_api_resource import CreateableAPIResource |
| 4 |
from stripe._list_object import ListObject |
| 5 |
from stripe._listable_api_resource import ListableAPIResource |
| 6 |
from stripe._nested_resource_class_methods import nested_resource_class_methods |
| 7 |
from stripe._stripe_object import StripeObject |
| 8 |
from stripe._updateable_api_resource import UpdateableAPIResource |
| 9 |
from stripe._util import class_method_variant, sanitize_id |
| 10 |
from typing import ClassVar, Optional, cast, overload |
| 11 |
from typing_extensions import Literal, Unpack, TYPE_CHECKING |
| 12 |
|
| 13 |
if TYPE_CHECKING: |
| 14 |
from stripe.billing._meter_event_summary import MeterEventSummary |
| 15 |
from stripe.params.billing._meter_create_params import MeterCreateParams |
| 16 |
from stripe.params.billing._meter_deactivate_params import ( |
| 17 |
MeterDeactivateParams, |
| 18 |
) |
| 19 |
from stripe.params.billing._meter_list_event_summaries_params import ( |
| 20 |
MeterListEventSummariesParams, |
| 21 |
) |
| 22 |
from stripe.params.billing._meter_list_params import MeterListParams |
| 23 |
from stripe.params.billing._meter_modify_params import MeterModifyParams |
| 24 |
from stripe.params.billing._meter_reactivate_params import ( |
| 25 |
MeterReactivateParams, |
| 26 |
) |
| 27 |
from stripe.params.billing._meter_retrieve_params import ( |
| 28 |
MeterRetrieveParams, |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
@nested_resource_class_methods("event_summary") |
| 33 |
class Meter( |
| 34 |
CreateableAPIResource["Meter"], |
| 35 |
ListableAPIResource["Meter"], |
| 36 |
UpdateableAPIResource["Meter"], |
| 37 |
): |
| 38 |
""" |
| 39 |
Meters specify how to aggregate meter events over a billing period. Meter events represent the actions that customers take in your system. Meters attach to prices and form the basis of the bill. |
| 40 |
|
| 41 |
Related guide: [Usage based billing](https://docs.stripe.com/billing/subscriptions/usage-based) |
| 42 |
""" |
| 43 |
|
| 44 |
OBJECT_NAME: ClassVar[Literal["billing.meter"]] = "billing.meter" |
| 45 |
|
| 46 |
class CustomerMapping(StripeObject): |
| 47 |
event_payload_key: str |
| 48 |
""" |
| 49 |
The key in the meter event payload to use for mapping the event to a customer. |
| 50 |
""" |
| 51 |
type: Literal["by_id"] |
| 52 |
""" |
| 53 |
The method for mapping a meter event to a customer. |
| 54 |
""" |
| 55 |
|
| 56 |
class DefaultAggregation(StripeObject): |
| 57 |
formula: Literal["count", "last", "sum"] |
| 58 |
""" |
| 59 |
Specifies how events are aggregated. |
| 60 |
""" |
| 61 |
|
| 62 |
class StatusTransitions(StripeObject): |
| 63 |
deactivated_at: Optional[int] |
| 64 |
""" |
| 65 |
The time the meter was deactivated, if any. Measured in seconds since Unix epoch. |
| 66 |
""" |
| 67 |
|
| 68 |
class ValueSettings(StripeObject): |
| 69 |
event_payload_key: str |
| 70 |
""" |
| 71 |
The key in the meter event payload to use as the value for this meter. |
| 72 |
""" |
| 73 |
|
| 74 |
created: int |
| 75 |
""" |
| 76 |
Time at which the object was created. Measured in seconds since the Unix epoch. |
| 77 |
""" |
| 78 |
customer_mapping: CustomerMapping |
| 79 |
default_aggregation: DefaultAggregation |
| 80 |
display_name: str |
| 81 |
""" |
| 82 |
The meter's name. |
| 83 |
""" |
| 84 |
event_name: str |
| 85 |
""" |
| 86 |
The name of the meter event to record usage for. Corresponds with the `event_name` field on meter events. |
| 87 |
""" |
| 88 |
event_time_window: Optional[Literal["day", "hour"]] |
| 89 |
""" |
| 90 |
The time window which meter events have been pre-aggregated for, if any. |
| 91 |
""" |
| 92 |
id: str |
| 93 |
""" |
| 94 |
Unique identifier for the object. |
| 95 |
""" |
| 96 |
livemode: bool |
| 97 |
""" |
| 98 |
Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode. |
| 99 |
""" |
| 100 |
object: Literal["billing.meter"] |
| 101 |
""" |
| 102 |
String representing the object's type. Objects of the same type share the same value. |
| 103 |
""" |
| 104 |
status: Literal["active", "inactive"] |
| 105 |
""" |
| 106 |
The meter's status. |
| 107 |
""" |
| 108 |
status_transitions: StatusTransitions |
| 109 |
updated: int |
| 110 |
""" |
| 111 |
Time at which the object was last updated. Measured in seconds since the Unix epoch. |
| 112 |
""" |
| 113 |
value_settings: ValueSettings |
| 114 |
|
| 115 |
@classmethod |
| 116 |
def create(cls, **params: Unpack["MeterCreateParams"]) -> "Meter": |
| 117 |
""" |
| 118 |
Creates a billing meter. |
| 119 |
""" |
| 120 |
return cast( |
| 121 |
"Meter", |
| 122 |
cls._static_request( |
| 123 |
"post", |
| 124 |
cls.class_url(), |
| 125 |
params=params, |
| 126 |
), |
| 127 |
) |
| 128 |
|
| 129 |
@classmethod |
| 130 |
async def create_async( |
| 131 |
cls, **params: Unpack["MeterCreateParams"] |
| 132 |
) -> "Meter": |
| 133 |
""" |
| 134 |
Creates a billing meter. |
| 135 |
""" |
| 136 |
return cast( |
| 137 |
"Meter", |
| 138 |
await cls._static_request_async( |
| 139 |
"post", |
| 140 |
cls.class_url(), |
| 141 |
params=params, |
| 142 |
), |
| 143 |
) |
| 144 |
|
| 145 |
@classmethod |
| 146 |
def _cls_deactivate( |
| 147 |
cls, id: str, **params: Unpack["MeterDeactivateParams"] |
| 148 |
) -> "Meter": |
| 149 |
""" |
| 150 |
When a meter is deactivated, no more meter events will be accepted for this meter. You can't attach a deactivated meter to a price. |
| 151 |
""" |
| 152 |
return cast( |
| 153 |
"Meter", |
| 154 |
cls._static_request( |
| 155 |
"post", |
| 156 |
"/v1/billing/meters/{id}/deactivate".format( |
| 157 |
id=sanitize_id(id) |
| 158 |
), |
| 159 |
params=params, |
| 160 |
), |
| 161 |
) |
| 162 |
|
| 163 |
@overload |
| 164 |
@staticmethod |
| 165 |
def deactivate( |
| 166 |
id: str, **params: Unpack["MeterDeactivateParams"] |
| 167 |
) -> "Meter": |
| 168 |
""" |
| 169 |
When a meter is deactivated, no more meter events will be accepted for this meter. You can't attach a deactivated meter to a price. |
| 170 |
""" |
| 171 |
... |
| 172 |
|
| 173 |
@overload |
| 174 |
def deactivate(self, **params: Unpack["MeterDeactivateParams"]) -> "Meter": |
| 175 |
""" |
| 176 |
When a meter is deactivated, no more meter events will be accepted for this meter. You can't attach a deactivated meter to a price. |
| 177 |
""" |
| 178 |
... |
| 179 |
|
| 180 |
@class_method_variant("_cls_deactivate") |
| 181 |
def deactivate( # pyright: ignore[reportGeneralTypeIssues] |
| 182 |
self, **params: Unpack["MeterDeactivateParams"] |
| 183 |
) -> "Meter": |
| 184 |
""" |
| 185 |
When a meter is deactivated, no more meter events will be accepted for this meter. You can't attach a deactivated meter to a price. |
| 186 |
""" |
| 187 |
return cast( |
| 188 |
"Meter", |
| 189 |
self._request( |
| 190 |
"post", |
| 191 |
"/v1/billing/meters/{id}/deactivate".format( |
| 192 |
id=sanitize_id(self.get("id")) |
| 193 |
), |
| 194 |
params=params, |
| 195 |
), |
| 196 |
) |
| 197 |
|
| 198 |
@classmethod |
| 199 |
async def _cls_deactivate_async( |
| 200 |
cls, id: str, **params: Unpack["MeterDeactivateParams"] |
| 201 |
) -> "Meter": |
| 202 |
""" |
| 203 |
When a meter is deactivated, no more meter events will be accepted for this meter. You can't attach a deactivated meter to a price. |
| 204 |
""" |
| 205 |
return cast( |
| 206 |
"Meter", |
| 207 |
await cls._static_request_async( |
| 208 |
"post", |
| 209 |
"/v1/billing/meters/{id}/deactivate".format( |
| 210 |
id=sanitize_id(id) |
| 211 |
), |
| 212 |
params=params, |
| 213 |
), |
| 214 |
) |
| 215 |
|
| 216 |
@overload |
| 217 |
@staticmethod |
| 218 |
async def deactivate_async( |
| 219 |
id: str, **params: Unpack["MeterDeactivateParams"] |
| 220 |
) -> "Meter": |
| 221 |
""" |
| 222 |
When a meter is deactivated, no more meter events will be accepted for this meter. You can't attach a deactivated meter to a price. |
| 223 |
""" |
| 224 |
... |
| 225 |
|
| 226 |
@overload |
| 227 |
async def deactivate_async( |
| 228 |
self, **params: Unpack["MeterDeactivateParams"] |
| 229 |
) -> "Meter": |
| 230 |
""" |
| 231 |
When a meter is deactivated, no more meter events will be accepted for this meter. You can't attach a deactivated meter to a price. |
| 232 |
""" |
| 233 |
... |
| 234 |
|
| 235 |
@class_method_variant("_cls_deactivate_async") |
| 236 |
async def deactivate_async( # pyright: ignore[reportGeneralTypeIssues] |
| 237 |
self, **params: Unpack["MeterDeactivateParams"] |
| 238 |
) -> "Meter": |
| 239 |
""" |
| 240 |
When a meter is deactivated, no more meter events will be accepted for this meter. You can't attach a deactivated meter to a price. |
| 241 |
""" |
| 242 |
return cast( |
| 243 |
"Meter", |
| 244 |
await self._request_async( |
| 245 |
"post", |
| 246 |
"/v1/billing/meters/{id}/deactivate".format( |
| 247 |
id=sanitize_id(self.get("id")) |
| 248 |
), |
| 249 |
params=params, |
| 250 |
), |
| 251 |
) |
| 252 |
|
| 253 |
@classmethod |
| 254 |
def list(cls, **params: Unpack["MeterListParams"]) -> ListObject["Meter"]: |
| 255 |
""" |
| 256 |
Retrieve a list of billing meters. |
| 257 |
""" |
| 258 |
result = cls._static_request( |
| 259 |
"get", |
| 260 |
cls.class_url(), |
| 261 |
params=params, |
| 262 |
) |
| 263 |
if not isinstance(result, ListObject): |
| 264 |
raise TypeError( |
| 265 |
"Expected list object from API, got %s" |
| 266 |
% (type(result).__name__) |
| 267 |
) |
| 268 |
|
| 269 |
return result |
| 270 |
|
| 271 |
@classmethod |
| 272 |
async def list_async( |
| 273 |
cls, **params: Unpack["MeterListParams"] |
| 274 |
) -> ListObject["Meter"]: |
| 275 |
""" |
| 276 |
Retrieve a list of billing meters. |
| 277 |
""" |
| 278 |
result = await cls._static_request_async( |
| 279 |
"get", |
| 280 |
cls.class_url(), |
| 281 |
params=params, |
| 282 |
) |
| 283 |
if not isinstance(result, ListObject): |
| 284 |
raise TypeError( |
| 285 |
"Expected list object from API, got %s" |
| 286 |
% (type(result).__name__) |
| 287 |
) |
| 288 |
|
| 289 |
return result |
| 290 |
|
| 291 |
@classmethod |
| 292 |
def modify(cls, id: str, **params: Unpack["MeterModifyParams"]) -> "Meter": |
| 293 |
""" |
| 294 |
Updates a billing meter. |
| 295 |
""" |
| 296 |
url = "%s/%s" % (cls.class_url(), sanitize_id(id)) |
| 297 |
return cast( |
| 298 |
"Meter", |
| 299 |
cls._static_request( |
| 300 |
"post", |
| 301 |
url, |
| 302 |
params=params, |
| 303 |
), |
| 304 |
) |
| 305 |
|
| 306 |
@classmethod |
| 307 |
async def modify_async( |
| 308 |
cls, id: str, **params: Unpack["MeterModifyParams"] |
| 309 |
) -> "Meter": |
| 310 |
""" |
| 311 |
Updates a billing meter. |
| 312 |
""" |
| 313 |
url = "%s/%s" % (cls.class_url(), sanitize_id(id)) |
| 314 |
return cast( |
| 315 |
"Meter", |
| 316 |
await cls._static_request_async( |
| 317 |
"post", |
| 318 |
url, |
| 319 |
params=params, |
| 320 |
), |
| 321 |
) |
| 322 |
|
| 323 |
@classmethod |
| 324 |
def _cls_reactivate( |
| 325 |
cls, id: str, **params: Unpack["MeterReactivateParams"] |
| 326 |
) -> "Meter": |
| 327 |
""" |
| 328 |
When a meter is reactivated, events for this meter can be accepted and you can attach the meter to a price. |
| 329 |
""" |
| 330 |
return cast( |
| 331 |
"Meter", |
| 332 |
cls._static_request( |
| 333 |
"post", |
| 334 |
"/v1/billing/meters/{id}/reactivate".format( |
| 335 |
id=sanitize_id(id) |
| 336 |
), |
| 337 |
params=params, |
| 338 |
), |
| 339 |
) |
| 340 |
|
| 341 |
@overload |
| 342 |
@staticmethod |
| 343 |
def reactivate( |
| 344 |
id: str, **params: Unpack["MeterReactivateParams"] |
| 345 |
) -> "Meter": |
| 346 |
""" |
| 347 |
When a meter is reactivated, events for this meter can be accepted and you can attach the meter to a price. |
| 348 |
""" |
| 349 |
... |
| 350 |
|
| 351 |
@overload |
| 352 |
def reactivate(self, **params: Unpack["MeterReactivateParams"]) -> "Meter": |
| 353 |
""" |
| 354 |
When a meter is reactivated, events for this meter can be accepted and you can attach the meter to a price. |
| 355 |
""" |
| 356 |
... |
| 357 |
|
| 358 |
@class_method_variant("_cls_reactivate") |
| 359 |
def reactivate( # pyright: ignore[reportGeneralTypeIssues] |
| 360 |
self, **params: Unpack["MeterReactivateParams"] |
| 361 |
) -> "Meter": |
| 362 |
""" |
| 363 |
When a meter is reactivated, events for this meter can be accepted and you can attach the meter to a price. |
| 364 |
""" |
| 365 |
return cast( |
| 366 |
"Meter", |
| 367 |
self._request( |
| 368 |
"post", |
| 369 |
"/v1/billing/meters/{id}/reactivate".format( |
| 370 |
id=sanitize_id(self.get("id")) |
| 371 |
), |
| 372 |
params=params, |
| 373 |
), |
| 374 |
) |
| 375 |
|
| 376 |
@classmethod |
| 377 |
async def _cls_reactivate_async( |
| 378 |
cls, id: str, **params: Unpack["MeterReactivateParams"] |
| 379 |
) -> "Meter": |
| 380 |
""" |
| 381 |
When a meter is reactivated, events for this meter can be accepted and you can attach the meter to a price. |
| 382 |
""" |
| 383 |
return cast( |
| 384 |
"Meter", |
| 385 |
await cls._static_request_async( |
| 386 |
"post", |
| 387 |
"/v1/billing/meters/{id}/reactivate".format( |
| 388 |
id=sanitize_id(id) |
| 389 |
), |
| 390 |
params=params, |
| 391 |
), |
| 392 |
) |
| 393 |
|
| 394 |
@overload |
| 395 |
@staticmethod |
| 396 |
async def reactivate_async( |
| 397 |
id: str, **params: Unpack["MeterReactivateParams"] |
| 398 |
) -> "Meter": |
| 399 |
""" |
| 400 |
When a meter is reactivated, events for this meter can be accepted and you can attach the meter to a price. |
| 401 |
""" |
| 402 |
... |
| 403 |
|
| 404 |
@overload |
| 405 |
async def reactivate_async( |
| 406 |
self, **params: Unpack["MeterReactivateParams"] |
| 407 |
) -> "Meter": |
| 408 |
""" |
| 409 |
When a meter is reactivated, events for this meter can be accepted and you can attach the meter to a price. |
| 410 |
""" |
| 411 |
... |
| 412 |
|
| 413 |
@class_method_variant("_cls_reactivate_async") |
| 414 |
async def reactivate_async( # pyright: ignore[reportGeneralTypeIssues] |
| 415 |
self, **params: Unpack["MeterReactivateParams"] |
| 416 |
) -> "Meter": |
| 417 |
""" |
| 418 |
When a meter is reactivated, events for this meter can be accepted and you can attach the meter to a price. |
| 419 |
""" |
| 420 |
return cast( |
| 421 |
"Meter", |
| 422 |
await self._request_async( |
| 423 |
"post", |
| 424 |
"/v1/billing/meters/{id}/reactivate".format( |
| 425 |
id=sanitize_id(self.get("id")) |
| 426 |
), |
| 427 |
params=params, |
| 428 |
), |
| 429 |
) |
| 430 |
|
| 431 |
@classmethod |
| 432 |
def retrieve( |
| 433 |
cls, id: str, **params: Unpack["MeterRetrieveParams"] |
| 434 |
) -> "Meter": |
| 435 |
""" |
| 436 |
Retrieves a billing meter given an ID. |
| 437 |
""" |
| 438 |
instance = cls(id, **params) |
| 439 |
instance.refresh() |
| 440 |
return instance |
| 441 |
|
| 442 |
@classmethod |
| 443 |
async def retrieve_async( |
| 444 |
cls, id: str, **params: Unpack["MeterRetrieveParams"] |
| 445 |
) -> "Meter": |
| 446 |
""" |
| 447 |
Retrieves a billing meter given an ID. |
| 448 |
""" |
| 449 |
instance = cls(id, **params) |
| 450 |
await instance.refresh_async() |
| 451 |
return instance |
| 452 |
|
| 453 |
@classmethod |
| 454 |
def list_event_summaries( |
| 455 |
cls, id: str, **params: Unpack["MeterListEventSummariesParams"] |
| 456 |
) -> ListObject["MeterEventSummary"]: |
| 457 |
""" |
| 458 |
Retrieve a list of billing meter event summaries. |
| 459 |
""" |
| 460 |
return cast( |
| 461 |
ListObject["MeterEventSummary"], |
| 462 |
cls._static_request( |
| 463 |
"get", |
| 464 |
"/v1/billing/meters/{id}/event_summaries".format( |
| 465 |
id=sanitize_id(id) |
| 466 |
), |
| 467 |
params=params, |
| 468 |
), |
| 469 |
) |
| 470 |
|
| 471 |
@classmethod |
| 472 |
async def list_event_summaries_async( |
| 473 |
cls, id: str, **params: Unpack["MeterListEventSummariesParams"] |
| 474 |
) -> ListObject["MeterEventSummary"]: |
| 475 |
""" |
| 476 |
Retrieve a list of billing meter event summaries. |
| 477 |
""" |
| 478 |
return cast( |
| 479 |
ListObject["MeterEventSummary"], |
| 480 |
await cls._static_request_async( |
| 481 |
"get", |
| 482 |
"/v1/billing/meters/{id}/event_summaries".format( |
| 483 |
id=sanitize_id(id) |
| 484 |
), |
| 485 |
params=params, |
| 486 |
), |
| 487 |
) |
| 488 |
|
| 489 |
_inner_class_types = { |
| 490 |
"customer_mapping": CustomerMapping, |
| 491 |
"default_aggregation": DefaultAggregation, |
| 492 |
"status_transitions": StatusTransitions, |
| 493 |
"value_settings": ValueSettings, |
| 494 |
} |