|
| 1 | +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing_extensions import Literal |
| 6 | + |
| 7 | +import httpx |
| 8 | + |
| 9 | +from ..types import translation_translate_params |
| 10 | +from .._types import NOT_GIVEN, Body, Query, Headers, NotGiven |
| 11 | +from .._utils import ( |
| 12 | + maybe_transform, |
| 13 | + async_maybe_transform, |
| 14 | +) |
| 15 | +from .._compat import cached_property |
| 16 | +from .._resource import SyncAPIResource, AsyncAPIResource |
| 17 | +from .._response import ( |
| 18 | + to_raw_response_wrapper, |
| 19 | + to_streamed_response_wrapper, |
| 20 | + async_to_raw_response_wrapper, |
| 21 | + async_to_streamed_response_wrapper, |
| 22 | +) |
| 23 | +from .._base_client import make_request_options |
| 24 | +from ..types.translation_response import TranslationResponse |
| 25 | + |
| 26 | +__all__ = ["TranslationResource", "AsyncTranslationResource"] |
| 27 | + |
| 28 | + |
| 29 | +class TranslationResource(SyncAPIResource): |
| 30 | + @cached_property |
| 31 | + def with_raw_response(self) -> TranslationResourceWithRawResponse: |
| 32 | + """ |
| 33 | + This property can be used as a prefix for any HTTP method call to return |
| 34 | + the raw response object instead of the parsed content. |
| 35 | +
|
| 36 | + For more information, see https://www.github.com/writer/writer-python#accessing-raw-response-data-eg-headers |
| 37 | + """ |
| 38 | + return TranslationResourceWithRawResponse(self) |
| 39 | + |
| 40 | + @cached_property |
| 41 | + def with_streaming_response(self) -> TranslationResourceWithStreamingResponse: |
| 42 | + """ |
| 43 | + An alternative to `.with_raw_response` that doesn't eagerly read the response body. |
| 44 | +
|
| 45 | + For more information, see https://www.github.com/writer/writer-python#with_streaming_response |
| 46 | + """ |
| 47 | + return TranslationResourceWithStreamingResponse(self) |
| 48 | + |
| 49 | + def translate( |
| 50 | + self, |
| 51 | + *, |
| 52 | + formality: bool, |
| 53 | + length_control: bool, |
| 54 | + mask_profanity: bool, |
| 55 | + model: Literal["palmyra-translate"], |
| 56 | + source_language_code: str, |
| 57 | + target_language_code: str, |
| 58 | + text: str, |
| 59 | + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 60 | + # The extra values given here take precedence over values defined on the client or passed to this method. |
| 61 | + extra_headers: Headers | None = None, |
| 62 | + extra_query: Query | None = None, |
| 63 | + extra_body: Body | None = None, |
| 64 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 65 | + ) -> TranslationResponse: |
| 66 | + """ |
| 67 | + Translate text from one language to another. |
| 68 | +
|
| 69 | + Args: |
| 70 | + formality: Whether to use formal or informal language in the translation. See the |
| 71 | + [list of languages that support formality](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#formality). |
| 72 | + If the language does not support formality, this parameter is ignored. |
| 73 | +
|
| 74 | + length_control: Whether to control the length of the translated text. See the |
| 75 | + [list of languages that support length control](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#length-control). |
| 76 | + If the language does not support length control, this parameter is ignored. |
| 77 | +
|
| 78 | + mask_profanity: Whether to mask profane words in the translated text. See the |
| 79 | + [list of languages that do not support profanity masking](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#profanity-masking). |
| 80 | + If the language does not support profanity masking, this parameter is ignored. |
| 81 | +
|
| 82 | + model: The model to use for translation. |
| 83 | +
|
| 84 | + source_language_code: The [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes) |
| 85 | + language code of the original text to translate. For example, `en` for English, |
| 86 | + `zh` for Chinese, `fr` for French, `es` for Spanish. If the language has a |
| 87 | + variant, the code appends the two-digit |
| 88 | + [ISO-3166 country code](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes). |
| 89 | + For example, Mexican Spanish is `es-MX`. See the |
| 90 | + [list of supported languages and language codes](https://dev.writer.com/api-guides/api-reference/translation-api/language-support). |
| 91 | +
|
| 92 | + target_language_code: The [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes) |
| 93 | + language code of the target language for the translation. For example, `en` for |
| 94 | + English, `zh` for Chinese, `fr` for French, `es` for Spanish. If the language |
| 95 | + has a variant, the code appends the two-digit |
| 96 | + [ISO-3166 country code](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes). |
| 97 | + For example, Mexican Spanish is `es-MX`. See the |
| 98 | + [list of supported languages and language codes](https://dev.writer.com/api-guides/api-reference/translation-api/language-support). |
| 99 | +
|
| 100 | + text: The text to translate. Maximum of 100,000 words. |
| 101 | +
|
| 102 | + extra_headers: Send extra headers |
| 103 | +
|
| 104 | + extra_query: Add additional query parameters to the request |
| 105 | +
|
| 106 | + extra_body: Add additional JSON properties to the request |
| 107 | +
|
| 108 | + timeout: Override the client-level default timeout for this request, in seconds |
| 109 | + """ |
| 110 | + return self._post( |
| 111 | + "/v1/translation", |
| 112 | + body=maybe_transform( |
| 113 | + { |
| 114 | + "formality": formality, |
| 115 | + "length_control": length_control, |
| 116 | + "mask_profanity": mask_profanity, |
| 117 | + "model": model, |
| 118 | + "source_language_code": source_language_code, |
| 119 | + "target_language_code": target_language_code, |
| 120 | + "text": text, |
| 121 | + }, |
| 122 | + translation_translate_params.TranslationTranslateParams, |
| 123 | + ), |
| 124 | + options=make_request_options( |
| 125 | + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 126 | + ), |
| 127 | + cast_to=TranslationResponse, |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | +class AsyncTranslationResource(AsyncAPIResource): |
| 132 | + @cached_property |
| 133 | + def with_raw_response(self) -> AsyncTranslationResourceWithRawResponse: |
| 134 | + """ |
| 135 | + This property can be used as a prefix for any HTTP method call to return |
| 136 | + the raw response object instead of the parsed content. |
| 137 | +
|
| 138 | + For more information, see https://www.github.com/writer/writer-python#accessing-raw-response-data-eg-headers |
| 139 | + """ |
| 140 | + return AsyncTranslationResourceWithRawResponse(self) |
| 141 | + |
| 142 | + @cached_property |
| 143 | + def with_streaming_response(self) -> AsyncTranslationResourceWithStreamingResponse: |
| 144 | + """ |
| 145 | + An alternative to `.with_raw_response` that doesn't eagerly read the response body. |
| 146 | +
|
| 147 | + For more information, see https://www.github.com/writer/writer-python#with_streaming_response |
| 148 | + """ |
| 149 | + return AsyncTranslationResourceWithStreamingResponse(self) |
| 150 | + |
| 151 | + async def translate( |
| 152 | + self, |
| 153 | + *, |
| 154 | + formality: bool, |
| 155 | + length_control: bool, |
| 156 | + mask_profanity: bool, |
| 157 | + model: Literal["palmyra-translate"], |
| 158 | + source_language_code: str, |
| 159 | + target_language_code: str, |
| 160 | + text: str, |
| 161 | + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 162 | + # The extra values given here take precedence over values defined on the client or passed to this method. |
| 163 | + extra_headers: Headers | None = None, |
| 164 | + extra_query: Query | None = None, |
| 165 | + extra_body: Body | None = None, |
| 166 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 167 | + ) -> TranslationResponse: |
| 168 | + """ |
| 169 | + Translate text from one language to another. |
| 170 | +
|
| 171 | + Args: |
| 172 | + formality: Whether to use formal or informal language in the translation. See the |
| 173 | + [list of languages that support formality](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#formality). |
| 174 | + If the language does not support formality, this parameter is ignored. |
| 175 | +
|
| 176 | + length_control: Whether to control the length of the translated text. See the |
| 177 | + [list of languages that support length control](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#length-control). |
| 178 | + If the language does not support length control, this parameter is ignored. |
| 179 | +
|
| 180 | + mask_profanity: Whether to mask profane words in the translated text. See the |
| 181 | + [list of languages that do not support profanity masking](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#profanity-masking). |
| 182 | + If the language does not support profanity masking, this parameter is ignored. |
| 183 | +
|
| 184 | + model: The model to use for translation. |
| 185 | +
|
| 186 | + source_language_code: The [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes) |
| 187 | + language code of the original text to translate. For example, `en` for English, |
| 188 | + `zh` for Chinese, `fr` for French, `es` for Spanish. If the language has a |
| 189 | + variant, the code appends the two-digit |
| 190 | + [ISO-3166 country code](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes). |
| 191 | + For example, Mexican Spanish is `es-MX`. See the |
| 192 | + [list of supported languages and language codes](https://dev.writer.com/api-guides/api-reference/translation-api/language-support). |
| 193 | +
|
| 194 | + target_language_code: The [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes) |
| 195 | + language code of the target language for the translation. For example, `en` for |
| 196 | + English, `zh` for Chinese, `fr` for French, `es` for Spanish. If the language |
| 197 | + has a variant, the code appends the two-digit |
| 198 | + [ISO-3166 country code](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes). |
| 199 | + For example, Mexican Spanish is `es-MX`. See the |
| 200 | + [list of supported languages and language codes](https://dev.writer.com/api-guides/api-reference/translation-api/language-support). |
| 201 | +
|
| 202 | + text: The text to translate. Maximum of 100,000 words. |
| 203 | +
|
| 204 | + extra_headers: Send extra headers |
| 205 | +
|
| 206 | + extra_query: Add additional query parameters to the request |
| 207 | +
|
| 208 | + extra_body: Add additional JSON properties to the request |
| 209 | +
|
| 210 | + timeout: Override the client-level default timeout for this request, in seconds |
| 211 | + """ |
| 212 | + return await self._post( |
| 213 | + "/v1/translation", |
| 214 | + body=await async_maybe_transform( |
| 215 | + { |
| 216 | + "formality": formality, |
| 217 | + "length_control": length_control, |
| 218 | + "mask_profanity": mask_profanity, |
| 219 | + "model": model, |
| 220 | + "source_language_code": source_language_code, |
| 221 | + "target_language_code": target_language_code, |
| 222 | + "text": text, |
| 223 | + }, |
| 224 | + translation_translate_params.TranslationTranslateParams, |
| 225 | + ), |
| 226 | + options=make_request_options( |
| 227 | + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 228 | + ), |
| 229 | + cast_to=TranslationResponse, |
| 230 | + ) |
| 231 | + |
| 232 | + |
| 233 | +class TranslationResourceWithRawResponse: |
| 234 | + def __init__(self, translation: TranslationResource) -> None: |
| 235 | + self._translation = translation |
| 236 | + |
| 237 | + self.translate = to_raw_response_wrapper( |
| 238 | + translation.translate, |
| 239 | + ) |
| 240 | + |
| 241 | + |
| 242 | +class AsyncTranslationResourceWithRawResponse: |
| 243 | + def __init__(self, translation: AsyncTranslationResource) -> None: |
| 244 | + self._translation = translation |
| 245 | + |
| 246 | + self.translate = async_to_raw_response_wrapper( |
| 247 | + translation.translate, |
| 248 | + ) |
| 249 | + |
| 250 | + |
| 251 | +class TranslationResourceWithStreamingResponse: |
| 252 | + def __init__(self, translation: TranslationResource) -> None: |
| 253 | + self._translation = translation |
| 254 | + |
| 255 | + self.translate = to_streamed_response_wrapper( |
| 256 | + translation.translate, |
| 257 | + ) |
| 258 | + |
| 259 | + |
| 260 | +class AsyncTranslationResourceWithStreamingResponse: |
| 261 | + def __init__(self, translation: AsyncTranslationResource) -> None: |
| 262 | + self._translation = translation |
| 263 | + |
| 264 | + self.translate = async_to_streamed_response_wrapper( |
| 265 | + translation.translate, |
| 266 | + ) |
0 commit comments