anaplan-sdk 0.4.3a1__tar.gz → 0.4.3a2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/PKG-INFO +1 -1
- anaplan_sdk-0.4.3a2/anaplan_sdk/__init__.py +15 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_async_clients/_bulk.py +35 -79
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_auth.py +114 -143
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_clients/_bulk.py +36 -70
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_oauth.py +82 -23
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/guides/authentication.md +77 -121
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/pyproject.toml +1 -1
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/uv.lock +505 -503
- anaplan_sdk-0.4.3a1/anaplan_sdk/__init__.py +0 -5
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/.github/dependabot.yml +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/.github/workflows/docs.yml +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/.github/workflows/lint.yml +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/.github/workflows/tests.yml +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/.gitignore +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/.pre-commit-config.yaml +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/LICENSE +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/README.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_async_clients/__init__.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_async_clients/_alm.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_async_clients/_audit.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_async_clients/_cloud_works.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_async_clients/_cw_flow.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_async_clients/_transactional.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_base.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_clients/__init__.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_clients/_alm.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_clients/_audit.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_clients/_cloud_works.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_clients/_cw_flow.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/_clients/_transactional.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/exceptions.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/models/__init__.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/models/_alm.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/models/_base.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/models/_bulk.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/models/_transactional.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/models/cloud_works.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/anaplan_sdk/models/flows.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/anaplan_explained.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/async/async_alm_client.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/async/async_audit_client.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/async/async_client.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/async/async_cw_client.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/async/async_flows_client.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/async/async_oauth_client.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/async/async_transactional_client.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/exceptions.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/models/alm.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/models/bulk.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/models/cloud_works.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/models/flows.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/models/transactional.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/sync/sync_alm_client.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/sync/sync_audit_client.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/sync/sync_client.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/sync/sync_cw_client.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/sync/sync_flows_client.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/sync/sync_oauth_client.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/api/sync/sync_transactional_client.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/assets/overview.html +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/css/styles.css +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/guides/alm.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/guides/audit.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/guides/bulk.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/guides/bulk_vs_transactional.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/guides/cloud_works.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/guides/index.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/guides/logging.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/guides/multiple_models.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/guides/transactional.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/img/anaplan-sdk.webp +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/index.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/installation.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/js/assets/hljs.js +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/js/assets/hljs.min.js +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/js/assets/python.js +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/js/assets/python.min.js +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/js/highlight.js +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/js/highlight.min.js +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/docs/quickstart.md +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/mkdocs.yml +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/async/conftest.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/async/test_async_alm_client.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/async/test_async_audit_client.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/async/test_async_client.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/async/test_async_cloud_works_client.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/async/test_async_flows_client.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/async/test_async_transactional_client.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/conftest.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/sync/conftest.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/sync/test_alm_client.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/sync/test_audit_client.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/sync/test_client.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/sync/test_cloud_works_client.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/sync/test_flows_client.py +0 -0
- {anaplan_sdk-0.4.3a1 → anaplan_sdk-0.4.3a2}/tests/sync/test_transactional_client.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: anaplan-sdk
|
3
|
-
Version: 0.4.
|
3
|
+
Version: 0.4.3a2
|
4
4
|
Summary: Streamlined Python Interface for Anaplan
|
5
5
|
Project-URL: Homepage, https://vinzenzklass.github.io/anaplan-sdk/
|
6
6
|
Project-URL: Repository, https://github.com/VinzenzKlass/anaplan-sdk
|
@@ -0,0 +1,15 @@
|
|
1
|
+
from ._async_clients import AsyncClient
|
2
|
+
from ._auth import AnaplanOAuthCodeAuth, AnaplanRefreshTokenAuth
|
3
|
+
from ._clients import Client
|
4
|
+
from ._oauth import AsyncOauth, Oauth
|
5
|
+
|
6
|
+
__all__ = [
|
7
|
+
"AsyncClient",
|
8
|
+
"Client",
|
9
|
+
"AnaplanOAuthCodeAuth",
|
10
|
+
"AnaplanRefreshTokenAuth",
|
11
|
+
"AsyncOauth",
|
12
|
+
"Oauth",
|
13
|
+
"models",
|
14
|
+
"exceptions",
|
15
|
+
]
|
@@ -6,7 +6,7 @@ from typing import AsyncIterator, Iterator
|
|
6
6
|
import httpx
|
7
7
|
from typing_extensions import Self
|
8
8
|
|
9
|
-
from anaplan_sdk._auth import
|
9
|
+
from anaplan_sdk._auth import _create_auth
|
10
10
|
from anaplan_sdk._base import _AsyncBaseClient, action_url
|
11
11
|
from anaplan_sdk.exceptions import AnaplanActionError, InvalidIdentifierException
|
12
12
|
from anaplan_sdk.models import (
|
@@ -32,14 +32,8 @@ logger = logging.getLogger("anaplan_sdk")
|
|
32
32
|
|
33
33
|
class AsyncClient(_AsyncBaseClient):
|
34
34
|
"""
|
35
|
-
|
36
|
-
|
37
|
-
abstractions over the API, so you can deal with python objects and simple functions rather than
|
38
|
-
implementation details like http, json, compression, chunking etc.
|
39
|
-
|
40
|
-
|
41
|
-
For more information, quick start guides and detailed instructions refer to:
|
42
|
-
[Anaplan SDK](https://vinzenzklass.github.io/anaplan-sdk).
|
35
|
+
Asynchronous Anaplan Client. For guides and examples
|
36
|
+
refer to https://vinzenzklass.github.io/anaplan-sdk.
|
43
37
|
"""
|
44
38
|
|
45
39
|
def __init__(
|
@@ -51,15 +45,8 @@ class AsyncClient(_AsyncBaseClient):
|
|
51
45
|
certificate: str | bytes | None = None,
|
52
46
|
private_key: str | bytes | None = None,
|
53
47
|
private_key_password: str | bytes | None = None,
|
54
|
-
client_id: str | None = None,
|
55
|
-
client_secret: str | None = None,
|
56
|
-
redirect_uri: str | None = None,
|
57
|
-
refresh_token: str | None = None,
|
58
|
-
oauth2_scope: str = "openid profile email offline_access",
|
59
|
-
on_auth_code: AuthCodeCallback = None,
|
60
|
-
on_token_refresh: AuthTokenRefreshCallback = None,
|
61
|
-
oauth_token: dict[str, str] | None = None,
|
62
48
|
token: str | None = None,
|
49
|
+
auth: httpx.Auth | None = None,
|
63
50
|
timeout: float | httpx.Timeout = 30,
|
64
51
|
retry_count: int = 2,
|
65
52
|
status_poll_delay: int = 1,
|
@@ -67,86 +54,55 @@ class AsyncClient(_AsyncBaseClient):
|
|
67
54
|
allow_file_creation: bool = False,
|
68
55
|
) -> None:
|
69
56
|
"""
|
70
|
-
|
71
|
-
https://
|
72
|
-
so you can deal with python objects and simple functions rather than implementation details
|
73
|
-
like http, json, compression, chunking etc.
|
74
|
-
|
75
|
-
|
76
|
-
For more information, quick start guides and detailed instructions refer to:
|
77
|
-
https://vinzenzklass.github.io/anaplan-sdk.
|
57
|
+
Asynchronous Anaplan Client. For guides and examples
|
58
|
+
refer to https://vinzenzklass.github.io/anaplan-sdk.
|
78
59
|
|
79
60
|
:param workspace_id: The Anaplan workspace Id. You can copy this from the browser URL or
|
80
|
-
|
61
|
+
find them using an HTTP Client like Postman, Paw, Insomnia etc.
|
81
62
|
:param model_id: The identifier of the model.
|
82
63
|
:param user_email: A valid email registered with the Anaplan Workspace you are attempting
|
83
|
-
|
84
|
-
:param password: Password for the given `user_email
|
85
|
-
|
86
|
-
|
87
|
-
:param
|
88
|
-
|
89
|
-
:param
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
:param
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
handled appropriately regardless of the execution context (in a thread,
|
102
|
-
with or without an event loop, etc.).
|
103
|
-
**Note**: When using asynchronous callbacks in complex applications
|
104
|
-
with multiple event loops, be aware that callbacks may execute in a
|
105
|
-
separate event loop context from where they were defined, which can
|
106
|
-
make debugging challenging.
|
107
|
-
:param on_token_refresh: A callback function that is called whenever the token is refreshed.
|
108
|
-
This includes the initial token retrieval and any subsequent calls.
|
109
|
-
With this you can for example securely store the token in your
|
110
|
-
application or on your server for later reuse. The function
|
111
|
-
must accept a single argument, which is the token dictionary
|
112
|
-
returned by the Oauth2 token endpoint and does not return anything.
|
113
|
-
This can be either a synchronous function or an async coroutine
|
114
|
-
function. **Note**: When using asynchronous callbacks in complex
|
115
|
-
applications with multiple event loops, be aware that callbacks
|
116
|
-
may execute in a separate event loop context from where they were
|
117
|
-
defined, which can make debugging challenging.
|
64
|
+
to access.
|
65
|
+
:param password: Password for the given `user_email` for basic Authentication.
|
66
|
+
:param certificate: The certificate content or the absolute path to the certificate file.
|
67
|
+
:param private_key: The private key content or the absolute path to the private key file.
|
68
|
+
:param private_key_password: The password to access the private key file. This is only
|
69
|
+
considered if you provided a private key file and it password-protected.
|
70
|
+
:param token: An Anaplan API Token. This will be used to authenticate the client. If
|
71
|
+
sufficient other authentication parameters are provided, the token will be used
|
72
|
+
until it expires, after which a new one will be created. If you provide only this
|
73
|
+
parameter, the client will raise an error upon first authentication failure. For
|
74
|
+
short-lived instances, such as in web applications where user specific clients are
|
75
|
+
created, this is the recommended way to authenticate, since this has the least
|
76
|
+
overhead.
|
77
|
+
:param auth: You can provide a subclass of `httpx.Auth` to use for authentication. You can
|
78
|
+
provide an instance of one of the classes provided by the SDK, or an instance of
|
79
|
+
your own subclass of `httpx.Auth`. This will give you full control over the
|
80
|
+
authentication process, but you will need to implement the entire authentication
|
81
|
+
logic yourself.
|
118
82
|
:param timeout: The timeout in seconds for the HTTP requests. Alternatively, you can pass
|
119
|
-
|
83
|
+
an instance of `httpx.Timeout` to set the timeout for the HTTP requests.
|
120
84
|
:param retry_count: The number of times to retry an HTTP request if it fails. Set this to 0
|
121
|
-
|
122
|
-
|
85
|
+
to never retry. Defaults to 2, meaning each HTTP Operation will be tried a total
|
86
|
+
number of 2 times.
|
123
87
|
:param status_poll_delay: The delay between polling the status of a task.
|
124
88
|
:param upload_chunk_size: The size of the chunks to upload. This is the maximum size of
|
125
|
-
|
89
|
+
each chunk. Defaults to 25MB.
|
126
90
|
:param allow_file_creation: Whether to allow the creation of new files. Defaults to False
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
creating new files and uploading content to them.
|
91
|
+
since this is typically unintentional and may well be unwanted behaviour in the API
|
92
|
+
altogether. A file that is created this way will not be referenced by any action in
|
93
|
+
anaplan until manually assigned so there is typically no value in dynamically
|
94
|
+
creating new files and uploading content to them.
|
132
95
|
"""
|
133
96
|
_client = httpx.AsyncClient(
|
134
97
|
auth=(
|
135
|
-
|
98
|
+
auth
|
99
|
+
or _create_auth(
|
136
100
|
token=token,
|
137
|
-
oauth_token=oauth_token,
|
138
101
|
user_email=user_email,
|
139
102
|
password=password,
|
140
103
|
certificate=certificate,
|
141
104
|
private_key=private_key,
|
142
105
|
private_key_password=private_key_password,
|
143
|
-
client_id=client_id,
|
144
|
-
client_secret=client_secret,
|
145
|
-
redirect_uri=redirect_uri,
|
146
|
-
refresh_token=refresh_token,
|
147
|
-
oauth2_scope=oauth2_scope,
|
148
|
-
on_auth_code=on_auth_code,
|
149
|
-
on_token_refresh=on_token_refresh,
|
150
106
|
)
|
151
107
|
),
|
152
108
|
timeout=timeout,
|
@@ -1,14 +1,11 @@
|
|
1
|
-
import asyncio
|
2
|
-
import inspect
|
3
1
|
import logging
|
4
2
|
import os
|
5
|
-
import threading
|
6
3
|
from base64 import b64encode
|
7
|
-
from
|
8
|
-
from typing import Any, Awaitable, Callable, Coroutine
|
4
|
+
from typing import Awaitable, Callable
|
9
5
|
|
10
6
|
import httpx
|
11
7
|
|
8
|
+
from ._oauth import _OAuthRequestFactory
|
12
9
|
from .exceptions import AnaplanException, InvalidCredentialsException, InvalidPrivateKeyException
|
13
10
|
|
14
11
|
logger = logging.getLogger("anaplan_sdk")
|
@@ -22,12 +19,12 @@ AuthTokenRefreshCallback = (
|
|
22
19
|
class _AnaplanAuth(httpx.Auth):
|
23
20
|
requires_response_body = True
|
24
21
|
|
25
|
-
def __init__(self,
|
26
|
-
|
22
|
+
def __init__(self, token: str | None = None):
|
23
|
+
self._token: str = token or ""
|
24
|
+
if not token:
|
27
25
|
logger.info("Creating Authentication Token.")
|
28
26
|
with httpx.Client(timeout=15.0) as client:
|
29
|
-
|
30
|
-
self._parse_auth_response(res)
|
27
|
+
self._parse_auth_response(client.send(self._build_auth_request()))
|
31
28
|
|
32
29
|
def _build_auth_request(self) -> httpx.Request:
|
33
30
|
raise NotImplementedError("Must be implemented in subclass.")
|
@@ -47,26 +44,25 @@ class _AnaplanAuth(httpx.Auth):
|
|
47
44
|
raise InvalidCredentialsException
|
48
45
|
if not response.is_success:
|
49
46
|
raise AnaplanException(f"Authentication failed: {response.status_code} {response.text}")
|
50
|
-
self._token
|
47
|
+
self._token = response.json()["tokenInfo"]["tokenValue"]
|
51
48
|
|
52
49
|
|
53
|
-
class
|
50
|
+
class _StaticTokenAuth(httpx.Auth):
|
54
51
|
def __init__(self, token: str):
|
55
|
-
logger.warning("Using static token authentication. Tokens will not be refreshed.")
|
56
52
|
self._token = token
|
57
53
|
|
58
54
|
def auth_flow(self, request):
|
59
55
|
request.headers["Authorization"] = f"AnaplanAuthToken {self._token}"
|
60
56
|
response = yield request
|
61
57
|
if response.status_code == 401:
|
62
|
-
raise InvalidCredentialsException("
|
58
|
+
raise InvalidCredentialsException("Token is invalid or expired.")
|
63
59
|
|
64
60
|
|
65
|
-
class
|
66
|
-
def __init__(self, user_email: str, password: str):
|
61
|
+
class _AnaplanBasicAuth(_AnaplanAuth):
|
62
|
+
def __init__(self, user_email: str, password: str, token: str | None = None):
|
67
63
|
self.user_email = user_email
|
68
64
|
self.password = password
|
69
|
-
super().__init__()
|
65
|
+
super().__init__(token)
|
70
66
|
|
71
67
|
def _build_auth_request(self) -> httpx.Request:
|
72
68
|
cred = b64encode(f"{self.user_email}:{self.password}".encode()).decode()
|
@@ -77,7 +73,7 @@ class AnaplanBasicAuth(_AnaplanAuth):
|
|
77
73
|
)
|
78
74
|
|
79
75
|
|
80
|
-
class
|
76
|
+
class _AnaplanCertAuth(_AnaplanAuth):
|
81
77
|
requires_request_body = True
|
82
78
|
|
83
79
|
def __init__(
|
@@ -85,10 +81,11 @@ class AnaplanCertAuth(_AnaplanAuth):
|
|
85
81
|
certificate: str | bytes,
|
86
82
|
private_key: str | bytes,
|
87
83
|
private_key_password: str | bytes | None = None,
|
84
|
+
token: str | None = None,
|
88
85
|
):
|
89
86
|
self.__set_certificate(certificate)
|
90
87
|
self.__set_private_key(private_key, private_key_password)
|
91
|
-
super().__init__()
|
88
|
+
super().__init__(token)
|
92
89
|
|
93
90
|
def _build_auth_request(self) -> httpx.Request:
|
94
91
|
encoded_cert, encoded_string, encoded_signed_string = self._prep_credentials()
|
@@ -157,168 +154,142 @@ class AnaplanCertAuth(_AnaplanAuth):
|
|
157
154
|
raise InvalidPrivateKeyException from error
|
158
155
|
|
159
156
|
|
160
|
-
class
|
157
|
+
class AnaplanOAuthCodeAuth(_AnaplanAuth):
|
161
158
|
def __init__(
|
162
159
|
self,
|
163
160
|
client_id: str,
|
164
161
|
client_secret: str,
|
165
|
-
|
166
|
-
|
162
|
+
redirect_url: str,
|
163
|
+
token: dict[str, str] | None = None,
|
164
|
+
authorization_url: str = "https://us1a.app.anaplan.com/auth/prelogin",
|
165
|
+
token_url: str = "https://us1a.app.anaplan.com/oauth/token",
|
166
|
+
validation_url: str = "https://auth.anaplan.com/token/validate",
|
167
167
|
scope: str = "openid profile email offline_access",
|
168
|
-
|
169
|
-
on_token_refresh: AuthTokenRefreshCallback = None,
|
168
|
+
state_generator: Callable[[], str] | None = None,
|
170
169
|
):
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
170
|
+
"""
|
171
|
+
Initializes the AnaplanOAuthCodeAuth class for OAuth2 authentication using the
|
172
|
+
Authorization Code Flow. This is a utility class for local development and requires user
|
173
|
+
interaction. For Web Applications and other scenarios, refer to `Oauth` or `AsyncOauth`.
|
174
|
+
This class will refresh the access token automatically when it expires.
|
175
|
+
:param client_id: The client ID of your Anaplan Oauth 2.0 application. This Application
|
176
|
+
must be an Authorization Code Grant application.
|
177
|
+
:param client_secret: The client secret of your Anaplan Oauth 2.0 application.
|
178
|
+
:param redirect_url: The URL to which the user will be redirected after authorizing the
|
179
|
+
application.
|
180
|
+
:param authorization_url: The URL to which the user will be redirected to authorize the
|
181
|
+
application. Defaults to the Anaplan Prelogin Page, where the user can select the
|
182
|
+
login method.
|
183
|
+
:param token_url: The URL to post the authorization code to in order to fetch the access
|
184
|
+
token.
|
185
|
+
:param validation_url: The URL to validate the access token.
|
186
|
+
:param scope: The scope of the access request.
|
187
|
+
:param state_generator: A callable that generates a random state string. You can optionally
|
188
|
+
pass this if you need to customize the state generation logic. If not provided,
|
189
|
+
the state will be generated by `oauthlib`.
|
190
|
+
"""
|
191
|
+
|
192
|
+
self._oauth_token = token or {}
|
193
|
+
self._oauth = _OAuthRequestFactory(
|
194
|
+
client_id=client_id,
|
195
|
+
client_secret=client_secret,
|
196
|
+
redirect_url=redirect_url,
|
197
|
+
scope=scope,
|
198
|
+
authorization_url=authorization_url,
|
199
|
+
token_url=token_url,
|
200
|
+
validation_url=validation_url,
|
201
|
+
state_generator=state_generator,
|
180
202
|
)
|
181
|
-
|
182
|
-
self._client_id = client_id
|
183
|
-
self._client_secret = client_secret
|
184
|
-
self._redirect_uri = redirect_uri
|
185
|
-
self._refresh_token = refresh_token
|
186
|
-
self._scope = scope
|
187
|
-
self._id_token = None
|
188
|
-
self._on_auth_code = on_auth_code
|
189
|
-
self._on_token_refresh = on_token_refresh
|
190
|
-
if not refresh_token:
|
203
|
+
if not token:
|
191
204
|
self.__auth_code_flow()
|
192
|
-
super().__init__(
|
205
|
+
super().__init__(self._token)
|
193
206
|
|
194
207
|
def _build_auth_request(self) -> httpx.Request:
|
195
|
-
|
196
|
-
token_url=self._token_url,
|
197
|
-
refresh_token=self._refresh_token,
|
198
|
-
client_secret=self._client_secret,
|
199
|
-
client_id=self._client_id,
|
200
|
-
)
|
201
|
-
return httpx.Request(method="post", url=url, headers=headers, content=body)
|
208
|
+
return self._oauth.refresh_token_request(self._oauth_token["refresh_token"])
|
202
209
|
|
203
210
|
def _parse_auth_response(self, response: httpx.Response) -> None:
|
204
211
|
if response.status_code == 401:
|
205
212
|
raise InvalidCredentialsException
|
206
213
|
if not response.is_success:
|
207
214
|
raise AnaplanException(f"Authentication failed: {response.status_code} {response.text}")
|
208
|
-
|
209
|
-
self._token =
|
210
|
-
self._refresh_token = token["refresh_token"]
|
211
|
-
if self._on_token_refresh:
|
212
|
-
_run_callback(self._on_token_refresh, token)
|
213
|
-
self._id_token = token.get("id_token")
|
215
|
+
self._oauth_token = response.json()
|
216
|
+
self._token: str = self._oauth_token["access_token"]
|
214
217
|
|
215
218
|
def __auth_code_flow(self):
|
216
219
|
from oauthlib.oauth2 import OAuth2Error
|
217
220
|
|
218
221
|
try:
|
219
222
|
logger.info("Creating Authentication Token with OAuth2 Authorization Code Flow.")
|
220
|
-
url, _
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
)
|
225
|
-
authorization_response = (
|
226
|
-
_run_callback(self._on_auth_code, url)
|
227
|
-
if self._on_auth_code
|
228
|
-
else input(
|
229
|
-
f"Please go to {url} and authorize the app.\n"
|
230
|
-
"Then paste the entire redirect URL here: "
|
231
|
-
)
|
223
|
+
url, _ = self._oauth.authorization_url()
|
224
|
+
authorization_response = input(
|
225
|
+
f"Please go to {url} and authorize the app.\n"
|
226
|
+
"Then paste the entire redirect URL here: "
|
232
227
|
)
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
authorization_response=authorization_response,
|
237
|
-
client_secret=self._client_secret,
|
238
|
-
)
|
239
|
-
self._parse_auth_response(httpx.post(url=url, headers=headers, content=body))
|
228
|
+
with httpx.Client() as client:
|
229
|
+
res = client.send(self._oauth.token_request(authorization_response))
|
230
|
+
self._parse_auth_response(res)
|
240
231
|
except (httpx.HTTPError, ValueError, TypeError, OAuth2Error) as error:
|
241
232
|
raise InvalidCredentialsException("Error during OAuth2 authorization flow.") from error
|
242
233
|
|
243
234
|
|
235
|
+
class AnaplanRefreshTokenAuth(_AnaplanAuth):
|
236
|
+
def __init__(
|
237
|
+
self,
|
238
|
+
client_id: str,
|
239
|
+
client_secret: str,
|
240
|
+
redirect_url: str,
|
241
|
+
token: dict[str, str],
|
242
|
+
token_url: str = "https://us1a.app.anaplan.com/oauth/token",
|
243
|
+
):
|
244
|
+
"""
|
245
|
+
This class is a utility class for long-lived `Client` or `AsyncClient` instances that use
|
246
|
+
OAuth. It expects that you have a valid OAuth token with a refresh token, which will be used
|
247
|
+
to refresh the access token when it expires.
|
248
|
+
:param client_id: The client ID of your Anaplan Oauth 2.0 application. This Application
|
249
|
+
must be an Authorization Code Grant application.
|
250
|
+
:param client_secret: The client secret of your Anaplan Oauth 2.0 application.
|
251
|
+
:param redirect_url: The URL to which the user will be redirected after authorizing the
|
252
|
+
application.
|
253
|
+
:param token_url: The URL to post the refresh token request to in order to fetch the access
|
254
|
+
token.
|
255
|
+
"""
|
256
|
+
self._oauth_token = token
|
257
|
+
self._oauth = _OAuthRequestFactory(
|
258
|
+
client_id=client_id,
|
259
|
+
client_secret=client_secret,
|
260
|
+
redirect_url=redirect_url,
|
261
|
+
token_url=token_url,
|
262
|
+
)
|
263
|
+
super().__init__(self._token)
|
264
|
+
|
265
|
+
def _build_auth_request(self) -> httpx.Request:
|
266
|
+
return self._oauth.refresh_token_request(self._oauth_token["refresh_token"])
|
267
|
+
|
268
|
+
def _parse_auth_response(self, response: httpx.Response) -> None:
|
269
|
+
if response.status_code == 401:
|
270
|
+
raise InvalidCredentialsException
|
271
|
+
if not response.is_success:
|
272
|
+
raise AnaplanException(f"Authentication failed: {response.status_code} {response.text}")
|
273
|
+
self._oauth_token = response.json()
|
274
|
+
self._token: str = self._oauth_token["access_token"]
|
275
|
+
|
276
|
+
|
244
277
|
def _create_auth(
|
245
278
|
user_email: str | None = None,
|
246
279
|
password: str | None = None,
|
247
280
|
certificate: str | bytes | None = None,
|
248
281
|
private_key: str | bytes | None = None,
|
249
282
|
private_key_password: str | bytes | None = None,
|
250
|
-
client_id: str | None = None,
|
251
|
-
client_secret: str | None = None,
|
252
|
-
redirect_uri: str | None = None,
|
253
|
-
refresh_token: str | None = None,
|
254
|
-
oauth2_scope: str = "openid profile email offline_access",
|
255
|
-
on_auth_code: AuthCodeCallback = None,
|
256
|
-
on_token_refresh: AuthTokenRefreshCallback = None,
|
257
283
|
token: str | None = None,
|
258
|
-
oauth_token: dict[str, str] | None = None,
|
259
284
|
) -> httpx.Auth:
|
260
|
-
if token:
|
261
|
-
# TODO: If other parameters are provided that allow refreshing the token, use them to create
|
262
|
-
# use them to create one of the other auth classes instead.
|
263
|
-
return StaticTokenAuth(token)
|
264
|
-
if oauth_token:
|
265
|
-
if not isinstance(oauth_token, dict) and all(
|
266
|
-
f in oauth_token for f in ("access_token", "refresh_token", "token_type", "scope")
|
267
|
-
):
|
268
|
-
raise ValueError(
|
269
|
-
"oauth_token must be a dictionary with at least 'access_token', 'refresh_token', "
|
270
|
-
"'token_type', and 'scope' keys."
|
271
|
-
)
|
272
|
-
# TODO: If client_id, client_secret, and redirect_uri are provided, use them to create an
|
273
|
-
# AnaplanOauth2AuthCodeAuth (extend to accept existing `access_token` instance. Else, use
|
274
|
-
# the StaticTokenAuth directly.
|
275
|
-
return StaticTokenAuth(oauth_token["access_token"])
|
276
285
|
if certificate and private_key:
|
277
|
-
return
|
286
|
+
return _AnaplanCertAuth(certificate, private_key, private_key_password, token)
|
278
287
|
if user_email and password:
|
279
|
-
return
|
280
|
-
if
|
281
|
-
return
|
282
|
-
client_id=client_id,
|
283
|
-
client_secret=client_secret,
|
284
|
-
redirect_uri=redirect_uri,
|
285
|
-
refresh_token=refresh_token,
|
286
|
-
scope=oauth2_scope,
|
287
|
-
on_auth_code=on_auth_code,
|
288
|
-
on_token_refresh=on_token_refresh,
|
289
|
-
)
|
288
|
+
return _AnaplanBasicAuth(user_email, password, token)
|
289
|
+
if token:
|
290
|
+
return _StaticTokenAuth(token)
|
290
291
|
raise ValueError(
|
291
292
|
"No valid authentication parameters provided. Please provide either:\n"
|
292
|
-
"- user_email and password
|
293
|
-
"- certificate and private_key
|
294
|
-
"- client_id, client_secret, and redirect_uri"
|
293
|
+
"- `user_email` and `password`, or\n"
|
294
|
+
"- `certificate` and `private_key`\n"
|
295
295
|
)
|
296
|
-
|
297
|
-
|
298
|
-
def _run_callback(func, *arg, **kwargs):
|
299
|
-
if not inspect.iscoroutinefunction(func):
|
300
|
-
return func(*arg, **kwargs)
|
301
|
-
coro = func(*arg, **kwargs)
|
302
|
-
try:
|
303
|
-
loop = asyncio.get_running_loop()
|
304
|
-
except RuntimeError:
|
305
|
-
return asyncio.run(coro)
|
306
|
-
|
307
|
-
if threading.current_thread() is threading.main_thread():
|
308
|
-
if not loop.is_running():
|
309
|
-
return loop.run_until_complete(coro)
|
310
|
-
else:
|
311
|
-
with ThreadPoolExecutor() as pool:
|
312
|
-
future = pool.submit(__run_in_new_loop, coro)
|
313
|
-
return future.result(timeout=30)
|
314
|
-
else:
|
315
|
-
return asyncio.run_coroutine_threadsafe(coro, loop).result()
|
316
|
-
|
317
|
-
|
318
|
-
def __run_in_new_loop(coroutine: Coroutine[Any, Any, Any]):
|
319
|
-
new_loop = asyncio.new_event_loop()
|
320
|
-
asyncio.set_event_loop(new_loop)
|
321
|
-
try:
|
322
|
-
return new_loop.run_until_complete(coroutine)
|
323
|
-
finally:
|
324
|
-
new_loop.close()
|