|
| 1 | +# File generated from our OpenAPI spec by Stainless. |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import List, Union, Optional |
| 6 | +from datetime import datetime |
| 7 | + |
| 8 | +import httpx |
| 9 | + |
| 10 | +from ..._types import NOT_GIVEN, Body, Query, Headers, NotGiven |
| 11 | +from ..._utils import maybe_transform |
| 12 | +from ..._compat import cached_property |
| 13 | +from ..._resource import SyncAPIResource, AsyncAPIResource |
| 14 | +from ..._response import to_raw_response_wrapper, async_to_raw_response_wrapper |
| 15 | +from ...types.beta import PriceEvaluateResponse, price_evaluate_params |
| 16 | +from ..._base_client import ( |
| 17 | + make_request_options, |
| 18 | +) |
| 19 | + |
| 20 | +__all__ = ["Price", "AsyncPrice"] |
| 21 | + |
| 22 | + |
| 23 | +class Price(SyncAPIResource): |
| 24 | + @cached_property |
| 25 | + def with_raw_response(self) -> PriceWithRawResponse: |
| 26 | + return PriceWithRawResponse(self) |
| 27 | + |
| 28 | + def evaluate( |
| 29 | + self, |
| 30 | + price_id: str, |
| 31 | + *, |
| 32 | + timeframe_end: Union[str, datetime], |
| 33 | + timeframe_start: Union[str, datetime], |
| 34 | + customer_id: Optional[str] | NotGiven = NOT_GIVEN, |
| 35 | + external_customer_id: Optional[str] | NotGiven = NOT_GIVEN, |
| 36 | + filter: Optional[str] | NotGiven = NOT_GIVEN, |
| 37 | + grouping_keys: List[str] | NotGiven = NOT_GIVEN, |
| 38 | + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 39 | + # The extra values given here take precedence over values defined on the client or passed to this method. |
| 40 | + extra_headers: Headers | None = None, |
| 41 | + extra_query: Query | None = None, |
| 42 | + extra_body: Body | None = None, |
| 43 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 44 | + idempotency_key: str | None = None, |
| 45 | + ) -> PriceEvaluateResponse: |
| 46 | + """ |
| 47 | + This endpoint is used to evaluate the output of a price for a given customer and |
| 48 | + time range. It enables filtering and grouping the output using |
| 49 | + [computed properties](../guides/extensibility/advanced-metrics#computed-properties), |
| 50 | + supporting the following workflows: |
| 51 | +
|
| 52 | + 1. Showing detailed usage and costs to the end customer. |
| 53 | + 2. Auditing subtotals on invoice line items. |
| 54 | +
|
| 55 | + For these workflows, the expressiveness of computed properties in both the |
| 56 | + filters and grouping is critical. For example, if you'd like to show your |
| 57 | + customer their usage grouped by hour and another property, you can do so with |
| 58 | + the following `grouping_keys`: |
| 59 | + `["hour_floor_timestamp_millis(timestamp_millis)", "my_property"]`. If you'd |
| 60 | + like to examine a customer's usage for a specific property value, you can do so |
| 61 | + with the following `filter`: |
| 62 | + `my_property = 'foo' AND my_other_property = 'bar'`. |
| 63 | +
|
| 64 | + By default, the start of the time range must be no more than 100 days ago and |
| 65 | + the length of the results must be no greater than 1000. Note that this is a POST |
| 66 | + endpoint rather than a GET endpoint because it employs a JSON body rather than |
| 67 | + query parameters. |
| 68 | +
|
| 69 | + Args: |
| 70 | + timeframe_end: The exclusive upper bound for event timestamps |
| 71 | +
|
| 72 | + timeframe_start: The inclusive lower bound for event timestamps |
| 73 | +
|
| 74 | + customer_id: The ID of the customer to which this evaluation is scoped. |
| 75 | +
|
| 76 | + external_customer_id: The external customer ID of the customer to which this evaluation is scoped. |
| 77 | +
|
| 78 | + filter: A boolean |
| 79 | + [computed property](../guides/extensibility/advanced-metrics#computed-properties) |
| 80 | + used to filter the underlying billable metric |
| 81 | +
|
| 82 | + grouping_keys: Properties (or |
| 83 | + [computed properties](../guides/extensibility/advanced-metrics#computed-properties)) |
| 84 | + used to group the underlying billable metric |
| 85 | +
|
| 86 | + extra_headers: Send extra headers |
| 87 | +
|
| 88 | + extra_query: Add additional query parameters to the request |
| 89 | +
|
| 90 | + extra_body: Add additional JSON properties to the request |
| 91 | +
|
| 92 | + timeout: Override the client-level default timeout for this request, in seconds |
| 93 | +
|
| 94 | + idempotency_key: Specify a custom idempotency key for this request |
| 95 | + """ |
| 96 | + return self._post( |
| 97 | + f"/prices/{price_id}/evaluate", |
| 98 | + body=maybe_transform( |
| 99 | + { |
| 100 | + "timeframe_end": timeframe_end, |
| 101 | + "timeframe_start": timeframe_start, |
| 102 | + "customer_id": customer_id, |
| 103 | + "external_customer_id": external_customer_id, |
| 104 | + "filter": filter, |
| 105 | + "grouping_keys": grouping_keys, |
| 106 | + }, |
| 107 | + price_evaluate_params.PriceEvaluateParams, |
| 108 | + ), |
| 109 | + options=make_request_options( |
| 110 | + extra_headers=extra_headers, |
| 111 | + extra_query=extra_query, |
| 112 | + extra_body=extra_body, |
| 113 | + timeout=timeout, |
| 114 | + idempotency_key=idempotency_key, |
| 115 | + ), |
| 116 | + cast_to=PriceEvaluateResponse, |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +class AsyncPrice(AsyncAPIResource): |
| 121 | + @cached_property |
| 122 | + def with_raw_response(self) -> AsyncPriceWithRawResponse: |
| 123 | + return AsyncPriceWithRawResponse(self) |
| 124 | + |
| 125 | + async def evaluate( |
| 126 | + self, |
| 127 | + price_id: str, |
| 128 | + *, |
| 129 | + timeframe_end: Union[str, datetime], |
| 130 | + timeframe_start: Union[str, datetime], |
| 131 | + customer_id: Optional[str] | NotGiven = NOT_GIVEN, |
| 132 | + external_customer_id: Optional[str] | NotGiven = NOT_GIVEN, |
| 133 | + filter: Optional[str] | NotGiven = NOT_GIVEN, |
| 134 | + grouping_keys: List[str] | NotGiven = NOT_GIVEN, |
| 135 | + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 136 | + # The extra values given here take precedence over values defined on the client or passed to this method. |
| 137 | + extra_headers: Headers | None = None, |
| 138 | + extra_query: Query | None = None, |
| 139 | + extra_body: Body | None = None, |
| 140 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 141 | + idempotency_key: str | None = None, |
| 142 | + ) -> PriceEvaluateResponse: |
| 143 | + """ |
| 144 | + This endpoint is used to evaluate the output of a price for a given customer and |
| 145 | + time range. It enables filtering and grouping the output using |
| 146 | + [computed properties](../guides/extensibility/advanced-metrics#computed-properties), |
| 147 | + supporting the following workflows: |
| 148 | +
|
| 149 | + 1. Showing detailed usage and costs to the end customer. |
| 150 | + 2. Auditing subtotals on invoice line items. |
| 151 | +
|
| 152 | + For these workflows, the expressiveness of computed properties in both the |
| 153 | + filters and grouping is critical. For example, if you'd like to show your |
| 154 | + customer their usage grouped by hour and another property, you can do so with |
| 155 | + the following `grouping_keys`: |
| 156 | + `["hour_floor_timestamp_millis(timestamp_millis)", "my_property"]`. If you'd |
| 157 | + like to examine a customer's usage for a specific property value, you can do so |
| 158 | + with the following `filter`: |
| 159 | + `my_property = 'foo' AND my_other_property = 'bar'`. |
| 160 | +
|
| 161 | + By default, the start of the time range must be no more than 100 days ago and |
| 162 | + the length of the results must be no greater than 1000. Note that this is a POST |
| 163 | + endpoint rather than a GET endpoint because it employs a JSON body rather than |
| 164 | + query parameters. |
| 165 | +
|
| 166 | + Args: |
| 167 | + timeframe_end: The exclusive upper bound for event timestamps |
| 168 | +
|
| 169 | + timeframe_start: The inclusive lower bound for event timestamps |
| 170 | +
|
| 171 | + customer_id: The ID of the customer to which this evaluation is scoped. |
| 172 | +
|
| 173 | + external_customer_id: The external customer ID of the customer to which this evaluation is scoped. |
| 174 | +
|
| 175 | + filter: A boolean |
| 176 | + [computed property](../guides/extensibility/advanced-metrics#computed-properties) |
| 177 | + used to filter the underlying billable metric |
| 178 | +
|
| 179 | + grouping_keys: Properties (or |
| 180 | + [computed properties](../guides/extensibility/advanced-metrics#computed-properties)) |
| 181 | + used to group the underlying billable metric |
| 182 | +
|
| 183 | + extra_headers: Send extra headers |
| 184 | +
|
| 185 | + extra_query: Add additional query parameters to the request |
| 186 | +
|
| 187 | + extra_body: Add additional JSON properties to the request |
| 188 | +
|
| 189 | + timeout: Override the client-level default timeout for this request, in seconds |
| 190 | +
|
| 191 | + idempotency_key: Specify a custom idempotency key for this request |
| 192 | + """ |
| 193 | + return await self._post( |
| 194 | + f"/prices/{price_id}/evaluate", |
| 195 | + body=maybe_transform( |
| 196 | + { |
| 197 | + "timeframe_end": timeframe_end, |
| 198 | + "timeframe_start": timeframe_start, |
| 199 | + "customer_id": customer_id, |
| 200 | + "external_customer_id": external_customer_id, |
| 201 | + "filter": filter, |
| 202 | + "grouping_keys": grouping_keys, |
| 203 | + }, |
| 204 | + price_evaluate_params.PriceEvaluateParams, |
| 205 | + ), |
| 206 | + options=make_request_options( |
| 207 | + extra_headers=extra_headers, |
| 208 | + extra_query=extra_query, |
| 209 | + extra_body=extra_body, |
| 210 | + timeout=timeout, |
| 211 | + idempotency_key=idempotency_key, |
| 212 | + ), |
| 213 | + cast_to=PriceEvaluateResponse, |
| 214 | + ) |
| 215 | + |
| 216 | + |
| 217 | +class PriceWithRawResponse: |
| 218 | + def __init__(self, price: Price) -> None: |
| 219 | + self.evaluate = to_raw_response_wrapper( |
| 220 | + price.evaluate, |
| 221 | + ) |
| 222 | + |
| 223 | + |
| 224 | +class AsyncPriceWithRawResponse: |
| 225 | + def __init__(self, price: AsyncPrice) -> None: |
| 226 | + self.evaluate = async_to_raw_response_wrapper( |
| 227 | + price.evaluate, |
| 228 | + ) |
0 commit comments