# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. from __future__ import annotations import httpx from ..types import ( telephony_credential_list_params, telephony_credential_create_params, telephony_credential_update_params, ) from .._types import Body, Omit, Query, Headers, NotGiven, omit, not_given from .._utils import path_template, maybe_transform, strip_not_given, async_maybe_transform from .._compat import cached_property from .._resource import SyncAPIResource, AsyncAPIResource from .._response import ( to_raw_response_wrapper, to_streamed_response_wrapper, async_to_raw_response_wrapper, async_to_streamed_response_wrapper, ) from ..pagination import SyncDefaultFlatPagination, AsyncDefaultFlatPagination from .._base_client import AsyncPaginator, make_request_options from ..types.telephony_credential import TelephonyCredential from ..types.telephony_credential_create_response import TelephonyCredentialCreateResponse from ..types.telephony_credential_delete_response import TelephonyCredentialDeleteResponse from ..types.telephony_credential_update_response import TelephonyCredentialUpdateResponse from ..types.telephony_credential_retrieve_response import TelephonyCredentialRetrieveResponse from ..types.telephony_credential_retrieve_token_material_response import ( TelephonyCredentialRetrieveTokenMaterialResponse, ) __all__ = ["TelephonyCredentialsResource", "AsyncTelephonyCredentialsResource"] class TelephonyCredentialsResource(SyncAPIResource): @cached_property def with_raw_response(self) -> TelephonyCredentialsResourceWithRawResponse: """ This property can be used as a prefix for any HTTP method call to return the raw response object instead of the parsed content. For more information, see https://www.github.com/team-telnyx/telnyx-python#accessing-raw-response-data-eg-headers """ return TelephonyCredentialsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> TelephonyCredentialsResourceWithStreamingResponse: """ An alternative to `.with_raw_response` that doesn't eagerly read the response body. For more information, see https://www.github.com/team-telnyx/telnyx-python#with_streaming_response """ return TelephonyCredentialsResourceWithStreamingResponse(self) def create( self, *, connection_id: str, expires_at: str | Omit = omit, name: str | Omit = omit, tag: str | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> TelephonyCredentialCreateResponse: """Creates a new on-demand telephony credential for the specified connection. The credential can then be used to generate access tokens for SIP or WebRTC clients. Args: connection_id: Identifies the Credential Connection this credential is associated with. expires_at: ISO-8601 formatted date indicating when the credential will expire. tag: Tags a credential. A single tag can hold at maximum 1000 credentials. extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ return self._post( "/telephony_credentials", body=maybe_transform( { "connection_id": connection_id, "expires_at": expires_at, "name": name, "tag": tag, }, telephony_credential_create_params.TelephonyCredentialCreateParams, ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=TelephonyCredentialCreateResponse, ) def retrieve( self, id: str, *, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> TelephonyCredentialRetrieveResponse: """ Get the details of an existing On-demand Credential. Args: extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return self._get( path_template("/telephony_credentials/{id}", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=TelephonyCredentialRetrieveResponse, ) def update( self, id: str, *, connection_id: str | Omit = omit, expires_at: str | Omit = omit, name: str | Omit = omit, tag: str | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> TelephonyCredentialUpdateResponse: """ Updates the specified telephony credential and returns the updated credential. Args: connection_id: Identifies the Credential Connection this credential is associated with. expires_at: ISO-8601 formatted date indicating when the credential will expire. tag: Tags a credential. A single tag can hold at maximum 1000 credentials. extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return self._patch( path_template("/telephony_credentials/{id}", id=id), body=maybe_transform( { "connection_id": connection_id, "expires_at": expires_at, "name": name, "tag": tag, }, telephony_credential_update_params.TelephonyCredentialUpdateParams, ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=TelephonyCredentialUpdateResponse, ) def list( self, *, filter: telephony_credential_list_params.Filter | Omit = omit, page_number: int | Omit = omit, page_size: int | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> SyncDefaultFlatPagination[TelephonyCredential]: """ Returns a paginated list of the on-demand telephony credentials on your account, with support for filtering. Args: filter: Consolidated filter parameter (deepObject style). Originally: filter[tag], filter[name], filter[status], filter[resource_id], filter[sip_username] extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ return self._get_api_list( "/telephony_credentials", page=SyncDefaultFlatPagination[TelephonyCredential], options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout, query=maybe_transform( { "filter": filter, "page_number": page_number, "page_size": page_size, }, telephony_credential_list_params.TelephonyCredentialListParams, ), ), model=TelephonyCredential, ) def delete( self, id: str, *, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> TelephonyCredentialDeleteResponse: """ Permanently deletes the specified telephony credential, revoking any access it provided. Args: extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return self._delete( path_template("/telephony_credentials/{id}", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=TelephonyCredentialDeleteResponse, ) def create_token( self, id: str, *, idempotency_key: str | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> str: """ Create an Access Token (JWT) for the credential. Args: extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") extra_headers = {"Accept": "text/plain", **(extra_headers or {})} extra_headers = {**strip_not_given({"Idempotency-Key": idempotency_key}), **(extra_headers or {})} return self._post( path_template("/telephony_credentials/{id}/token", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=str, ) def retrieve_token_material( self, id: str, *, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> TelephonyCredentialRetrieveTokenMaterialResponse: """ Returns the material a server needs to mint login tokens for the credential locally, with no request per token: `kid`, `tel_token` and `signing_secret`. The same material is in the response of `POST /telephony_credentials`. Treat it like the SIP password. Args: extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return self._get( path_template("/telephony_credentials/{id}/token_material", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=TelephonyCredentialRetrieveTokenMaterialResponse, ) class AsyncTelephonyCredentialsResource(AsyncAPIResource): @cached_property def with_raw_response(self) -> AsyncTelephonyCredentialsResourceWithRawResponse: """ This property can be used as a prefix for any HTTP method call to return the raw response object instead of the parsed content. For more information, see https://www.github.com/team-telnyx/telnyx-python#accessing-raw-response-data-eg-headers """ return AsyncTelephonyCredentialsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncTelephonyCredentialsResourceWithStreamingResponse: """ An alternative to `.with_raw_response` that doesn't eagerly read the response body. For more information, see https://www.github.com/team-telnyx/telnyx-python#with_streaming_response """ return AsyncTelephonyCredentialsResourceWithStreamingResponse(self) async def create( self, *, connection_id: str, expires_at: str | Omit = omit, name: str | Omit = omit, tag: str | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> TelephonyCredentialCreateResponse: """Creates a new on-demand telephony credential for the specified connection. The credential can then be used to generate access tokens for SIP or WebRTC clients. Args: connection_id: Identifies the Credential Connection this credential is associated with. expires_at: ISO-8601 formatted date indicating when the credential will expire. tag: Tags a credential. A single tag can hold at maximum 1000 credentials. extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ return await self._post( "/telephony_credentials", body=await async_maybe_transform( { "connection_id": connection_id, "expires_at": expires_at, "name": name, "tag": tag, }, telephony_credential_create_params.TelephonyCredentialCreateParams, ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=TelephonyCredentialCreateResponse, ) async def retrieve( self, id: str, *, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> TelephonyCredentialRetrieveResponse: """ Get the details of an existing On-demand Credential. Args: extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return await self._get( path_template("/telephony_credentials/{id}", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=TelephonyCredentialRetrieveResponse, ) async def update( self, id: str, *, connection_id: str | Omit = omit, expires_at: str | Omit = omit, name: str | Omit = omit, tag: str | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> TelephonyCredentialUpdateResponse: """ Updates the specified telephony credential and returns the updated credential. Args: connection_id: Identifies the Credential Connection this credential is associated with. expires_at: ISO-8601 formatted date indicating when the credential will expire. tag: Tags a credential. A single tag can hold at maximum 1000 credentials. extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return await self._patch( path_template("/telephony_credentials/{id}", id=id), body=await async_maybe_transform( { "connection_id": connection_id, "expires_at": expires_at, "name": name, "tag": tag, }, telephony_credential_update_params.TelephonyCredentialUpdateParams, ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=TelephonyCredentialUpdateResponse, ) def list( self, *, filter: telephony_credential_list_params.Filter | Omit = omit, page_number: int | Omit = omit, page_size: int | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> AsyncPaginator[TelephonyCredential, AsyncDefaultFlatPagination[TelephonyCredential]]: """ Returns a paginated list of the on-demand telephony credentials on your account, with support for filtering. Args: filter: Consolidated filter parameter (deepObject style). Originally: filter[tag], filter[name], filter[status], filter[resource_id], filter[sip_username] extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ return self._get_api_list( "/telephony_credentials", page=AsyncDefaultFlatPagination[TelephonyCredential], options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout, query=maybe_transform( { "filter": filter, "page_number": page_number, "page_size": page_size, }, telephony_credential_list_params.TelephonyCredentialListParams, ), ), model=TelephonyCredential, ) async def delete( self, id: str, *, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> TelephonyCredentialDeleteResponse: """ Permanently deletes the specified telephony credential, revoking any access it provided. Args: extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return await self._delete( path_template("/telephony_credentials/{id}", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=TelephonyCredentialDeleteResponse, ) async def create_token( self, id: str, *, idempotency_key: str | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> str: """ Create an Access Token (JWT) for the credential. Args: extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") extra_headers = {"Accept": "text/plain", **(extra_headers or {})} extra_headers = {**strip_not_given({"Idempotency-Key": idempotency_key}), **(extra_headers or {})} return await self._post( path_template("/telephony_credentials/{id}/token", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=str, ) async def retrieve_token_material( self, id: str, *, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> TelephonyCredentialRetrieveTokenMaterialResponse: """ Returns the material a server needs to mint login tokens for the credential locally, with no request per token: `kid`, `tel_token` and `signing_secret`. The same material is in the response of `POST /telephony_credentials`. Treat it like the SIP password. Args: extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return await self._get( path_template("/telephony_credentials/{id}/token_material", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=TelephonyCredentialRetrieveTokenMaterialResponse, ) class TelephonyCredentialsResourceWithRawResponse: def __init__(self, telephony_credentials: TelephonyCredentialsResource) -> None: self._telephony_credentials = telephony_credentials self.create = to_raw_response_wrapper( telephony_credentials.create, ) self.retrieve = to_raw_response_wrapper( telephony_credentials.retrieve, ) self.update = to_raw_response_wrapper( telephony_credentials.update, ) self.list = to_raw_response_wrapper( telephony_credentials.list, ) self.delete = to_raw_response_wrapper( telephony_credentials.delete, ) self.create_token = to_raw_response_wrapper( telephony_credentials.create_token, ) self.retrieve_token_material = to_raw_response_wrapper( telephony_credentials.retrieve_token_material, ) class AsyncTelephonyCredentialsResourceWithRawResponse: def __init__(self, telephony_credentials: AsyncTelephonyCredentialsResource) -> None: self._telephony_credentials = telephony_credentials self.create = async_to_raw_response_wrapper( telephony_credentials.create, ) self.retrieve = async_to_raw_response_wrapper( telephony_credentials.retrieve, ) self.update = async_to_raw_response_wrapper( telephony_credentials.update, ) self.list = async_to_raw_response_wrapper( telephony_credentials.list, ) self.delete = async_to_raw_response_wrapper( telephony_credentials.delete, ) self.create_token = async_to_raw_response_wrapper( telephony_credentials.create_token, ) self.retrieve_token_material = async_to_raw_response_wrapper( telephony_credentials.retrieve_token_material, ) class TelephonyCredentialsResourceWithStreamingResponse: def __init__(self, telephony_credentials: TelephonyCredentialsResource) -> None: self._telephony_credentials = telephony_credentials self.create = to_streamed_response_wrapper( telephony_credentials.create, ) self.retrieve = to_streamed_response_wrapper( telephony_credentials.retrieve, ) self.update = to_streamed_response_wrapper( telephony_credentials.update, ) self.list = to_streamed_response_wrapper( telephony_credentials.list, ) self.delete = to_streamed_response_wrapper( telephony_credentials.delete, ) self.create_token = to_streamed_response_wrapper( telephony_credentials.create_token, ) self.retrieve_token_material = to_streamed_response_wrapper( telephony_credentials.retrieve_token_material, ) class AsyncTelephonyCredentialsResourceWithStreamingResponse: def __init__(self, telephony_credentials: AsyncTelephonyCredentialsResource) -> None: self._telephony_credentials = telephony_credentials self.create = async_to_streamed_response_wrapper( telephony_credentials.create, ) self.retrieve = async_to_streamed_response_wrapper( telephony_credentials.retrieve, ) self.update = async_to_streamed_response_wrapper( telephony_credentials.update, ) self.list = async_to_streamed_response_wrapper( telephony_credentials.list, ) self.delete = async_to_streamed_response_wrapper( telephony_credentials.delete, ) self.create_token = async_to_streamed_response_wrapper( telephony_credentials.create_token, ) self.retrieve_token_material = async_to_streamed_response_wrapper( telephony_credentials.retrieve_token_material, )