|
| 1 | +""" |
| 2 | +Public API for managing OAuth integrations on Posit Connect. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from typing import Optional, Union |
| 8 | + |
| 9 | +from .api import RSConnectClient, RSConnectServer, SPCSConnectServer |
| 10 | +from .models import ( |
| 11 | + OAuthIntegration, |
| 12 | + OAuthIntegrationInput, |
| 13 | + OAuthIntegrationPermission, |
| 14 | + OAuthIntegrationUpdate, |
| 15 | + OAuthTemplate, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +def list_oauth_integrations( |
| 20 | + connect_server: Union[RSConnectServer, SPCSConnectServer], |
| 21 | +) -> list[OAuthIntegration]: |
| 22 | + with RSConnectClient(connect_server) as client: |
| 23 | + return client.oauth_integration_list() |
| 24 | + |
| 25 | + |
| 26 | +def get_oauth_integration( |
| 27 | + connect_server: Union[RSConnectServer, SPCSConnectServer], |
| 28 | + guid: str, |
| 29 | +) -> OAuthIntegration: |
| 30 | + with RSConnectClient(connect_server) as client: |
| 31 | + return client.oauth_integration_get(guid) |
| 32 | + |
| 33 | + |
| 34 | +def create_oauth_integration( |
| 35 | + connect_server: Union[RSConnectServer, SPCSConnectServer], |
| 36 | + template: str, |
| 37 | + config: dict[str, object], |
| 38 | + name: Optional[str] = None, |
| 39 | + description: Optional[str] = None, |
| 40 | + user_guids: Optional[list[str]] = None, |
| 41 | + group_guids: Optional[list[str]] = None, |
| 42 | +) -> OAuthIntegration: |
| 43 | + permissions: list[OAuthIntegrationPermission] = [] |
| 44 | + for g in user_guids or []: |
| 45 | + permissions.append({"user_guid": g, "group_guid": None}) |
| 46 | + for g in group_guids or []: |
| 47 | + permissions.append({"user_guid": None, "group_guid": g}) |
| 48 | + |
| 49 | + body: OAuthIntegrationInput = { |
| 50 | + "template": template, |
| 51 | + "config": config, |
| 52 | + } |
| 53 | + if name is not None: |
| 54 | + body["name"] = name |
| 55 | + if description is not None: |
| 56 | + body["description"] = description |
| 57 | + if permissions: |
| 58 | + body["permissions"] = permissions |
| 59 | + |
| 60 | + with RSConnectClient(connect_server) as client: |
| 61 | + return client.oauth_integration_create(body) |
| 62 | + |
| 63 | + |
| 64 | +def update_oauth_integration( |
| 65 | + connect_server: Union[RSConnectServer, SPCSConnectServer], |
| 66 | + guid: str, |
| 67 | + config: Optional[dict[str, object]] = None, |
| 68 | + name: Optional[str] = None, |
| 69 | + description: Optional[str] = None, |
| 70 | + user_guids: Optional[list[str]] = None, |
| 71 | + group_guids: Optional[list[str]] = None, |
| 72 | +) -> OAuthIntegration: |
| 73 | + with RSConnectClient(connect_server) as client: |
| 74 | + body: OAuthIntegrationUpdate = {} |
| 75 | + if name is not None: |
| 76 | + body["name"] = name |
| 77 | + if description is not None: |
| 78 | + body["description"] = description |
| 79 | + if config is not None: |
| 80 | + existing = client.oauth_integration_get(guid) |
| 81 | + merged_config = dict(existing["config"]) |
| 82 | + merged_config.update(config) |
| 83 | + body["config"] = merged_config |
| 84 | + |
| 85 | + permissions: list[OAuthIntegrationPermission] = [] |
| 86 | + if user_guids: |
| 87 | + for g in user_guids: |
| 88 | + permissions.append({"user_guid": g, "group_guid": None}) |
| 89 | + if group_guids: |
| 90 | + for g in group_guids: |
| 91 | + permissions.append({"user_guid": None, "group_guid": g}) |
| 92 | + if permissions: |
| 93 | + body["permissions"] = permissions |
| 94 | + |
| 95 | + return client.oauth_integration_update(guid, body) |
| 96 | + |
| 97 | + |
| 98 | +def delete_oauth_integration( |
| 99 | + connect_server: Union[RSConnectServer, SPCSConnectServer], |
| 100 | + guid: str, |
| 101 | +) -> None: |
| 102 | + with RSConnectClient(connect_server) as client: |
| 103 | + client.oauth_integration_delete(guid) |
| 104 | + |
| 105 | + |
| 106 | +def list_oauth_templates( |
| 107 | + connect_server: Union[RSConnectServer, SPCSConnectServer], |
| 108 | +) -> list[OAuthTemplate]: |
| 109 | + with RSConnectClient(connect_server) as client: |
| 110 | + return client.oauth_template_list() |
| 111 | + |
| 112 | + |
| 113 | +def get_oauth_template( |
| 114 | + connect_server: Union[RSConnectServer, SPCSConnectServer], |
| 115 | + key: str, |
| 116 | +) -> OAuthTemplate: |
| 117 | + with RSConnectClient(connect_server) as client: |
| 118 | + return client.oauth_template_get(key) |
0 commit comments