oapi.client
SSLContext
SSLContext(check_hostname: bool = True)
Bases: ssl.SSLContext
This class is a wrapper for ssl.SSLContext which makes it possible to
connect to hosts which have an unverified SSL certificate.
Source code in src/oapi/client.py
952 953 954 955 956 957 958 959 960 961 | |
__reduce__
__reduce__() -> tuple
A pickled instance of this class will just be an entirely new instance.
Source code in src/oapi/client.py
963 964 965 966 967 968 | |
Client
Client(
url: str | None = None,
*,
user: str | None = None,
password: str | None = None,
bearer_token: str | None = None,
api_key: str | None = None,
api_key_in: typing.Literal[
"header", "query", "cookie"
] = "header",
api_key_name: str = "X-API-KEY",
oauth2_client_id: str | None = None,
oauth2_client_secret: str | None = None,
oauth2_username: str | None = None,
oauth2_password: str | None = None,
oauth2_authorization_url: str | None = None,
oauth2_token_url: str | None = None,
oauth2_scope: str | tuple[str, ...] | None = None,
oauth2_refresh_url: str | None = None,
oauth2_flows: (
tuple[
typing.Literal[
"authorizationCode",
"implicit",
"password",
"clientCredentials",
"accessCode",
"application",
],
...,
]
| None
) = None,
open_id_connect_url: str = ".well-known/openid-configuration",
headers: (
collections.abc.Mapping[str, str]
| collections.abc.Sequence[tuple[str, str]]
) = (
("Accept", "application/json"),
("Content-type", "application/json"),
),
timeout: int = 0,
retry_number_of_attempts: int = 1,
retry_for_errors: tuple[
type[Exception], ...
] = oapi.client.DEFAULT_RETRY_FOR_ERRORS,
retry_hook: typing.Callable[
[Exception], bool
] = oapi.client.default_retry_hook,
verify_ssl_certificate: bool = True,
logger: logging.Logger | None = None,
echo: bool = False
)
A base class for OpenAPI clients.
Parameters:
-
url(str | None, default:None) –The base URL for API requests.
-
user(str | None, default:None) –A user name for use with HTTP basic authentication.
-
password(str | None, default:None) –A password for use with HTTP basic authentication.
-
bearer_token(str | None, default:None) –A token for use with HTTP bearer authentication.
-
api_key(str | None, default:None) –An API key with which to authenticate requests.
-
api_key_in(typing.Literal['header', 'query', 'cookie'], default:'header') –Where the API key should be conveyed: "header", "query" or "cookie".
-
api_key_name(str, default:'X-API-KEY') –The name of the header, query parameter, or cookie parameter in which to convey the API key.
-
oauth2_client_id(str | None, default:None) –An OAuth2 client ID.
-
oauth2_client_secret(str | None, default:None) –An OAuth2 client secret.
-
oauth2_username(str | None, default:None) –A username for the "password" OAuth2 grant type.
-
oauth2_password(str | None, default:None) –A password for the "password" OAuth2 grant type.
-
oauth2_authorization_url(str | None, default:None) –The authorization URL to use for an OAuth2 flow. Can be relative to
url. -
oauth2_token_url(str | None, default:None) –The token URL to use for OAuth2 authentication. Can be relative to
url. -
oauth2_scope(str | tuple[str, ...] | None, default:None) –One or more OAuth2 scopes
-
oauth2_refresh_url(str | None, default:None) –The URL to be used for obtaining refresh tokens for OAuth2 authentication.
-
oauth2_flows(tuple[typing.Literal['authorizationCode', 'implicit', 'password', 'clientCredentials', 'accessCode', 'application'], ...] | None, default:None) –A tuple containing one or more of the following: "authorizationCode", "implicit", "password" and/or "clientCredentials".
-
open_id_connect_url(str, default:'.well-known/openid-configuration') –The URL where an OpenID Provider's configuration can be obtained.
-
headers(collections.abc.Mapping[str, str] | collections.abc.Sequence[tuple[str, str]], default:(('Accept', 'application/json'), ('Content-type', 'application/json'))) –Default headers to include with all requests. Method-specific header arguments will override or modify these, where applicable, as will dynamically modified headers such as content-length, authorization, cookie, etc.
-
timeout(int, default:0) –The number of seconds before a request will timeout and throw an error. If this is 0 (the default), the system default timeout will be used.
-
retry_number_of_attempts(int, default:1) –The number of times to retry a request which results in an error.
-
retry_for_errors(tuple[type[Exception], ...], default:oapi.client.DEFAULT_RETRY_FOR_ERRORS) –A tuple of one or more exception types on which to retry a request. To retry for all errors, pass
(Exception,)for this argument. -
retry_hook(typing.Callable[[Exception], bool], default:oapi.client.default_retry_hook) –A function, accepting one argument (an Exception), and returning a boolean value indicating whether to retry the request (if retries have not been exhausted). This hook applies only for exceptions which are a sub-class of an exception included in
retry_for_errors. -
verify_ssl_certificate(bool, default:True) –If
True, SSL certificates are verified, per usual. IfFalse, SSL certificates are not verified. -
logger(logging.Logger | None, default:None) –A
logging.Loggerto which requests should be logged. -
echo(bool, default:False) –If
True, requests/responses are printed as they occur.
Source code in src/oapi/client.py
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 | |
request
request(
path: str,
method: str,
*,
json: str | bytes | sob.abc.Model | None = None,
data: (
collections.abc.Mapping[
str, sob.abc.MarshallableTypes
]
| collections.abc.Sequence[
tuple[str, sob.abc.MarshallableTypes]
]
) = (),
query: (
collections.abc.Mapping[
str, sob.abc.MarshallableTypes
]
| collections.abc.Sequence[
tuple[str, sob.abc.MarshallableTypes]
]
| str
) = (),
headers: (
collections.abc.Mapping[
str, sob.abc.MarshallableTypes
]
| collections.abc.Sequence[
tuple[str, sob.abc.MarshallableTypes]
]
) = (),
multipart: bool = False,
multipart_data_headers: (
collections.abc.Mapping[
str, collections.abc.MutableMapping[str, str]
]
| collections.abc.Sequence[
tuple[
str,
collections.abc.MutableMapping[str, str],
]
]
) = (),
timeout: int = 0
) -> sob.abc.Readable
Construct and submit an HTTP request and return the response
(an instance of http.client.HTTPResponse).
Parameters:
-
path(str) –This is the path of the request, relative to the server base URL
-
query(collections.abc.Mapping[str, sob.abc.MarshallableTypes] | collections.abc.Sequence[tuple[str, sob.abc.MarshallableTypes]] | str, default:()) – -
json(str | bytes | sob.abc.Model | None, default:None) –JSON data to be conveyed in the body of the request
-
data(collections.abc.Mapping[str, sob.abc.MarshallableTypes] | collections.abc.Sequence[tuple[str, sob.abc.MarshallableTypes]], default:()) –Form data to be conveyed in the body of the request
-
multipart(bool, default:False) –If
True,datashould be conveyed as a multipart request. -
query(collections.abc.Mapping[str, sob.abc.MarshallableTypes] | collections.abc.Sequence[tuple[str, sob.abc.MarshallableTypes]] | str, default:()) –A dictionary from which to assemble the query string.
-
headers(collections.abc.Mapping[str, sob.abc.MarshallableTypes] | collections.abc.Sequence[tuple[str, sob.abc.MarshallableTypes]], default:()) – -
multipart_data_headers(collections.abc.Mapping[str, collections.abc.MutableMapping[str, str]] | collections.abc.Sequence[tuple[str, collections.abc.MutableMapping[str, str]]], default:()) – -
timeout(int, default:0) –
Source code in src/oapi/client.py
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 | |
ClientModule
ClientModule(
open_api: (
str | sob.abc.Readable | oapi.oas.model.OpenAPI
),
model_path: str | pathlib.Path,
*,
class_name: str = "Client",
base_class: type[
oapi.client.Client
] = oapi.client.Client,
imports: str | tuple[str, ...] = (),
init_decorator: str = "",
include_init_parameters: str | tuple[str, ...] = (),
add_init_parameters: str | tuple[str, ...] = (),
add_init_parameter_docs: str | tuple[str, ...] = (),
init_parameter_defaults: (
collections.abc.Mapping[str, typing.Any]
| collections.abc.Sequence[tuple[str, typing.Any]]
) = (),
init_parameter_defaults_source: (
collections.abc.Mapping[str, typing.Any]
| collections.abc.Sequence[tuple[str, typing.Any]]
) = (),
get_method_name_from_path_method_operation: typing.Callable[
[str, str, str | None], str
] = oapi.client.get_default_method_name_from_path_method_operation,
use_operation_id: bool = False,
module_docstring: str | None = None,
class_docstring: str | None = None
)
This class parses an Open API document and outputs a module defining a client class for interfacing with the API described by an OpenAPI document.
Parameters:
-
open_api(str | sob.abc.Readable | oapi.oas.model.OpenAPI) –An OpenAPI document. This can be a URL, file-path, an HTTP response (
http.client.HTTPResponse), a file object, or an instance ofoapi.oas.model.OpenAPI. -
model_path(str | pathlib.Path) –The file path where the data model for this client can be found. This must be a model generated using
oapi.model, and must be part of the same project that this client will be saved into. -
class_name(str, default:'Client') – -
base_class(type[oapi.client.Client], default:oapi.client.Client) –The base class to use for the client. If provided, this must be a sub-class of
oapi.client.Client. -
imports(str | tuple[str, ...], default:()) –One or more import statements to include (in addition to those generated automatically).
-
init_decorator(str, default:'') –A decorator to apply to the client class
.__init__method. If used, make sure to include any modules referenced in yourimports. For example: "@decorator_function(argument_a=1, argument_b=2)". -
include_init_parameters(str | tuple[str, ...], default:()) –The name of all parameters to include for the client's
.__init__method. -
add_init_parameters(str | tuple[str, ...], default:()) –Additional parameter declarations to add to the client's
.__init__method. These should include a type hint and default value (not just the parameter name). For example: 'additional_parameter_name: str | None = None'. Note: additional parameters will not do anything without the use of a decorator which utilizes the additional parameters, so use of this parameter should be accompanied by aninit_decorator. -
add_init_parameter_docs(str | tuple[str, ...], default:()) – -
init_parameter_defaults(collections.abc.Mapping[str, typing.Any] | collections.abc.Sequence[tuple[str, typing.Any]], default:()) –A mapping of parameter names to default values for the parameter.
-
init_parameter_defaults_source(collections.abc.Mapping[str, typing.Any] | collections.abc.Sequence[tuple[str, typing.Any]], default:()) –A mapping of parameter names to default values for the parameter expressed as source code. This is to allow for the passing of imported constants, expressions, etc.
-
module_docstring(str | None, default:None) –A docstring to insert at the top of the module.
-
class_docstring(str | None, default:None) –A docstring to insert in the client class.
Source code in src/oapi/client.py
2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 | |
get_source
get_source(path: str | pathlib.Path) -> str
This generates source code for the client module.
Parameters:
-
path(str | pathlib.Path) –The file path for the client module. This is needed in order to determine the relative import for the model module.
Source code in src/oapi/client.py
3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 | |
save
save(path: str | pathlib.Path) -> None
This method will save the generated module to a given path.
Source code in src/oapi/client.py
3999 4000 4001 4002 4003 4004 4005 4006 | |
urlencode
urlencode(
query: (
collections.abc.Mapping[str, typing.Any]
| sob.abc.Dictionary
| sob.abc.Object
| collections.abc.Sequence[tuple[str, typing.Any]]
),
doseq: bool = True,
safe: str = oapi.client.URLENCODE_SAFE,
encoding: str = "utf-8",
errors: str = "",
quote_via: typing.Callable[
[str, bytes | str, str, str], str
] = urllib.parse.quote,
) -> str
This function wraps urllib.parse.urlencode, but has different default
argument values. Additionally, when a mapping/dictionary is passed for a
query value, that dictionary/mapping performs an update to the query
dictionary.
Parameters:
-
query(collections.abc.Mapping[str, typing.Any] | sob.abc.Dictionary | sob.abc.Object | collections.abc.Sequence[tuple[str, typing.Any]]) – -
doseq(bool, default:True) – -
safe(str, default:oapi.client.URLENCODE_SAFE) – -
encoding(str, default:'utf-8') – -
errors(str, default:'') – -
quote_via(typing.Callable[[str, bytes | str, str, str], str], default:urllib.parse.quote) –
Source code in src/oapi/client.py
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
format_argument_value
format_argument_value(
name: str,
value: sob.abc.MarshallableTypes | typing.IO[bytes],
style: str,
*,
explode: bool = False,
multipart: bool = False
) -> (
str
| dict[str, str]
| dict[str, str | list[str]]
| collections.abc.Sequence[str]
| collections.abc.Sequence[bytes]
| bytes
| collections.abc.Sequence[typing.IO[bytes]]
| typing.IO[bytes]
| None
)
Format an argument value for use in a path, query, cookie, header, etc.
Parameters:
-
value(sob.abc.MarshallableTypes | typing.IO[bytes]) – -
style(str) – -
explode(bool, default:False) – -
multipart(bool, default:False) –Indicates the argument will be part of a multipart request
See: https://swagger.io/docs/specification/serialization/
Source code in src/oapi/client.py
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | |
get_request_curl
get_request_curl(
request: urllib.request.Request,
options: str = "-i",
censored_headers: collections.abc.Iterable[str] = (
"X-api-key",
"Authorization",
),
censored_parameters: tuple[str, ...] = (
"client_secret",
),
) -> str
Render an instance of urllib.request.Request as a curl command.
Parameters:
-
options(str, default:'-i') –Any additional parameters to pass to
curl, (such as "--compressed", "--insecure", etc.)
Source code in src/oapi/client.py
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 | |
default_retry_hook
default_retry_hook(error: Exception) -> bool
By default, don't retry for HTTP 404 (NOT FOUND) errors and HTTP 401 (UNAUTHORIZED) errors.
Source code in src/oapi/client.py
695 696 697 698 699 700 701 702 | |
retry
retry(
errors: (
tuple[type[Exception], ...] | type[Exception]
) = Exception,
retry_hook: typing.Callable[
[Exception], bool
] = oapi.client.default_retry_hook,
number_of_attempts: int = 1,
logger: logging.Logger | None = None,
) -> typing.Callable
This function decorates another, and causes the decorated function to be re-attempted a specified number of times, with exponential backoff, until the decorated function is successful or the maximum number of attempts is reached (in which case an exception is raised).
Parameters:
-
errors(tuple[type[Exception], ...] | type[Exception], default:Exception) –A sub-class of
Exception, or a tuple of one or more sub-classes ofException. The default isException, causing all errors to trigger a retry. -
retry_hook(typing.Callable[[Exception], bool], default:oapi.client.default_retry_hook) –A function accepting as its only argument the handled exception, and returning a boolean value indicating whether or not to retry the function.
-
number_of_attempts(int, default:1) –The maximum number of times to attempt execution of the function, including the first execution. Please note that, because the default for this parameter is 1, this decorator will do nothing if this argument is not provided.
Source code in src/oapi/client.py
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 | |
write_client_module
write_client_module(
client_path: str | pathlib.Path,
*,
open_api: (
str | sob.abc.Readable | oapi.oas.model.OpenAPI
),
model_path: str | pathlib.Path,
class_name: str = "Client",
base_class: type[
oapi.client.Client
] = oapi.client.Client,
imports: str | tuple[str, ...] = (),
init_decorator: str = "",
include_init_parameters: str | tuple[str, ...] = (),
add_init_parameters: str | tuple[str, ...] = (),
add_init_parameter_docs: str | tuple[str, ...] = (),
init_parameter_defaults: (
collections.abc.Mapping[str, typing.Any]
| collections.abc.Sequence[tuple[str, typing.Any]]
) = (),
init_parameter_defaults_source: (
collections.abc.Mapping[str, typing.Any]
| collections.abc.Sequence[tuple[str, typing.Any]]
) = (),
get_method_name_from_path_method_operation: typing.Callable[
[str, str, str | None], str
] = oapi.client.get_default_method_name_from_path_method_operation,
use_operation_id: bool = False,
module_docstring: str | None = None,
class_docstring: str | None = None
) -> None
This function parses an Open API document and outputs a module defining a client class for interfacing with the API described by an OpenAPI document.
Parameters:
-
client_path(str | pathlib.Path) –The file path where the client module will be saved (created or updated).
-
open_api(str | sob.abc.Readable | oapi.oas.model.OpenAPI) –An OpenAPI document. This can be a URL, file-path, an HTTP response (
http.client.HTTPResponse), a file object, or an instance ofoapi.oas.model.OpenAPI. -
model_path(str | pathlib.Path) –The file path where the data model for this client can be found. This must be a model generated using
oapi.model, and must be part of the same project that this client will be saved into. -
class_name(str, default:'Client') – -
base_class(type[oapi.client.Client], default:oapi.client.Client) –The base class to use for the client. If provided, this must be a sub-class of
oapi.client.Client. -
imports(str | tuple[str, ...], default:()) –One or more import statements to include (in addition to those generated automatically).
-
init_decorator(str, default:'') –A decorator to apply to the client class
.__init__method. If used, make sure to include any modules referenced in yourimports. For example: "@decorator_function(argument_a=1, argument_b=2)". -
include_init_parameters(str | tuple[str, ...], default:()) –The name of all parameters to include for the client's
.__init__method. -
add_init_parameters(str | tuple[str, ...], default:()) –Additional parameter declarations to add to the client's
.__init__method. These should include a type hint and default value (not just the parameter name). For example: 'additional_parameter_name: str | None = None'. Note: additional parameters will not do anything without the use of a decorator which utilizes the additional parameters, so use of this parameter should be accompanied by aninit_decorator. -
add_init_parameter_docs(str | tuple[str, ...], default:()) – -
init_parameter_defaults(collections.abc.Mapping[str, typing.Any] | collections.abc.Sequence[tuple[str, typing.Any]], default:()) –A mapping of parameter names to default values for the parameter.
-
init_parameter_defaults_source(collections.abc.Mapping[str, typing.Any] | collections.abc.Sequence[tuple[str, typing.Any]], default:()) –A mapping of parameter names to default values for the parameter expressed as source code. This is to allow for the passing of imported constants, expressions, etc.
-
module_docstring(str | None, default:None) –A docstring to insert at the top of the module.
-
class_docstring(str | None, default:None) –A docstring to insert in the client class.
Source code in src/oapi/client.py
4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 | |