|
| 1 | + |
| 2 | +import typing |
| 3 | +import httpx |
| 4 | +import urllib.parse |
| 5 | +from json.decoder import JSONDecodeError |
| 6 | + |
| 7 | +from .core.api_error import ApiError |
| 8 | +from .core.jsonable_encoder import jsonable_encoder |
| 9 | +from .environment import WebflowEnvironment |
| 10 | +from .types import OauthScope |
| 11 | + |
| 12 | +try: |
| 13 | + import pydantic.v1 as pydantic # type: ignore |
| 14 | +except ImportError: |
| 15 | + import pydantic # type: ignore |
| 16 | + |
| 17 | +# this is used as the default value for optional parameters |
| 18 | +OMIT = typing.cast(typing.Any, ...) |
| 19 | + |
| 20 | + |
| 21 | +def authorize_url( |
| 22 | + *, |
| 23 | + client_id: str, |
| 24 | + state: typing.Optional[str] = OMIT, |
| 25 | + redirect_uri: typing.Optional[str] = OMIT, |
| 26 | + scope: typing.Optional[typing.Union[OauthScope, typing.List[OauthScope]]] = OMIT, |
| 27 | +) -> str: |
| 28 | + """ |
| 29 | + Get the URL to authorize a user |
| 30 | +
|
| 31 | + Parameters: |
| 32 | + - client_id: str. The OAuth client ID |
| 33 | +
|
| 34 | + - state: typing.Optional[str]. The state. |
| 35 | +
|
| 36 | + - redirect_uri: typing.Optional[str]. The redirect URI. |
| 37 | +
|
| 38 | + - scope: typing.Optional[typing.Union[OauthScope, typing.List[OauthScope]]]. |
| 39 | + OAuth Scopes. |
| 40 | + --- |
| 41 | + from webflow.oauth import authorize_url |
| 42 | + from webflow import OauthScope |
| 43 | +
|
| 44 | + url = authorize_url( |
| 45 | + client_id = "<YOUR_CLIENT_ID>", |
| 46 | + redirect_uri = "https://my.server.com/oauth/callback", |
| 47 | + scopes = [OauthScope.ReadSites, OauthScope.WriteItems", OauthScope.ReadUsers], |
| 48 | + ) |
| 49 | + """ |
| 50 | + params: typing.Dict[str, typing.Any] = { |
| 51 | + "client_id": client_id, |
| 52 | + "response_type": "code", |
| 53 | + } |
| 54 | + if state is not OMIT: |
| 55 | + params["state"] = state |
| 56 | + if redirect_uri is not OMIT: |
| 57 | + params["redirect_uri"] = redirect_uri |
| 58 | + if scope is not OMIT and isinstance(scope, str): |
| 59 | + params["scope"] = scope.value |
| 60 | + elif scope is not OMIT: |
| 61 | + params["scope"] = ", ".join([s.value for s in scope]) # type: ignore |
| 62 | + return f"https://webflow.com/oauth/authorize?{urllib.parse.urlencode(params)}" |
| 63 | + |
| 64 | + |
| 65 | +def get_access_token( |
| 66 | + *, |
| 67 | + client_id: str, |
| 68 | + client_secret: str, |
| 69 | + code: str, |
| 70 | + redirect_uri: typing.Optional[str] = OMIT, |
| 71 | +) -> str: |
| 72 | + """ |
| 73 | + Get the URL to authorize a user |
| 74 | +
|
| 75 | + Parameters: |
| 76 | + - client_id: str. The OAuth client ID |
| 77 | +
|
| 78 | + - client_secret: str. The OAuth client secret |
| 79 | +
|
| 80 | + - code: str. The OAuth code |
| 81 | +
|
| 82 | + - redirect_uri: typing.Optional[str]. The redirect URI. |
| 83 | + --- |
| 84 | + from webflow.oauth import get_access_token |
| 85 | +
|
| 86 | + token = get_access_token( |
| 87 | + client_id = "<YOUR_CLIENT_ID>", |
| 88 | + client_secret = "<YOUR_CLIENT_ID>", |
| 89 | + code= "<YOUR_CODE>" |
| 90 | + redirect_uri = "https://my.server.com/oauth/callback", |
| 91 | + ) |
| 92 | + """ |
| 93 | + request: typing.Dict[str, typing.Any] = { |
| 94 | + "client_id": client_id, |
| 95 | + "client_secret": client_secret, |
| 96 | + "code": code, |
| 97 | + "grant_type": "authorization_code", |
| 98 | + } |
| 99 | + if redirect_uri is not OMIT: |
| 100 | + request["redirect_uri"] = redirect_uri |
| 101 | + response = httpx.request( |
| 102 | + "POST", |
| 103 | + "https://api.webflow.com/oauth/access_token", |
| 104 | + json=jsonable_encoder(request), |
| 105 | + timeout=60, |
| 106 | + ) |
| 107 | + if 200 <= response.status_code < 300: |
| 108 | + _response_json = response.json() |
| 109 | + return _response_json["access_token"] |
| 110 | + try: |
| 111 | + raise ApiError(status_code=response.status_code, body=response.json()) |
| 112 | + except JSONDecodeError: |
| 113 | + raise ApiError(status_code=response.status_code, body=response.text) |
| 114 | + |
0 commit comments