Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: applies formatting #56

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 46 additions & 48 deletions src/posit/connect/content_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class TestContents:

@responses.activate
def test_get_all_content(self):
responses.get(
Expand Down Expand Up @@ -149,53 +148,52 @@ def test_get_all_content(self):
)
responses.get(
"https://connect.example/__api__/v1/content/f2f37341-e21d-3d80-c698-a935ad614066",
json=
{
"guid": "f2f37341-e21d-3d80-c698-a935ad614066",
"name": "Performance-Data-1671216053560",
"title": "Performance Data",
"description": "",
"access_type": "logged_in",
"connection_timeout": None,
"read_timeout": None,
"init_timeout": None,
"idle_timeout": None,
"max_processes": None,
"min_processes": None,
"max_conns_per_process": None,
"load_factor": None,
"memory_request": None,
"memory_limit": None,
"cpu_request": None,
"cpu_limit": None,
"amd_gpu_limit": None,
"nvidia_gpu_limit": None,
"service_account_name": None,
"default_image_name": None,
"created_time": "2022-12-16T18:40:53Z",
"last_deployed_time": "2024-02-24T09:56:30Z",
"bundle_id": "401171",
"app_mode": "quarto-static",
"content_category": "",
"parameterized": False,
"cluster_name": "Local",
"image_name": None,
"r_version": None,
"py_version": "3.9.17",
"quarto_version": "1.3.340",
"r_environment_management": None,
"default_r_environment_management": None,
"py_environment_management": True,
"default_py_environment_management": None,
"run_as": None,
"run_as_current_user": False,
"owner_guid": "87c12c08-11cd-4de1-8da3-12a7579c4998",
"content_url": "https://connect.example/content/f2f37341-e21d-3d80-c698-a935ad614066/",
"dashboard_url": "https://connect.example/connect/#/apps/f2f37341-e21d-3d80-c698-a935ad614066",
"app_role": "viewer",
"id": "8274",
}
)
json={
"guid": "f2f37341-e21d-3d80-c698-a935ad614066",
"name": "Performance-Data-1671216053560",
"title": "Performance Data",
"description": "",
"access_type": "logged_in",
"connection_timeout": None,
"read_timeout": None,
"init_timeout": None,
"idle_timeout": None,
"max_processes": None,
"min_processes": None,
"max_conns_per_process": None,
"load_factor": None,
"memory_request": None,
"memory_limit": None,
"cpu_request": None,
"cpu_limit": None,
"amd_gpu_limit": None,
"nvidia_gpu_limit": None,
"service_account_name": None,
"default_image_name": None,
"created_time": "2022-12-16T18:40:53Z",
"last_deployed_time": "2024-02-24T09:56:30Z",
"bundle_id": "401171",
"app_mode": "quarto-static",
"content_category": "",
"parameterized": False,
"cluster_name": "Local",
"image_name": None,
"r_version": None,
"py_version": "3.9.17",
"quarto_version": "1.3.340",
"r_environment_management": None,
"default_r_environment_management": None,
"py_environment_management": True,
"default_py_environment_management": None,
"run_as": None,
"run_as_current_user": False,
"owner_guid": "87c12c08-11cd-4de1-8da3-12a7579c4998",
"content_url": "https://connect.example/content/f2f37341-e21d-3d80-c698-a935ad614066/",
"dashboard_url": "https://connect.example/connect/#/apps/f2f37341-e21d-3d80-c698-a935ad614066",
"app_role": "viewer",
"id": "8274",
},
)
con = Client("12345", "https://connect.example")
all_content = con.content.find()
assert len(all_content) == 3
Expand Down
17 changes: 12 additions & 5 deletions src/posit/connect/external/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

HeaderFactory = Callable[[], Dict[str, str]]


# https://github.com/databricks/databricks-sdk-py/blob/v0.20.0/databricks/sdk/credentials_provider.py
# https://github.com/databricks/databricks-sql-python/blob/v3.1.0/src/databricks/sql/auth/authenticators.py
# In order to keep compatibility with the Databricks SDK
class CredentialsProvider(abc.ABC):
"""CredentialsProvider is the protocol (call-side interface)
for authenticating requests to Databricks REST APIs"""
for authenticating requests to Databricks REST APIs"""

@abc.abstractmethod
def auth_type(self) -> str:
Expand All @@ -33,8 +34,11 @@ def auth_type(self) -> str:

def __call__(self, *args, **kwargs) -> HeaderFactory:
def inner() -> Dict[str, str]:
access_token = self.posit_oauth.get_credentials(self.user_session_token)['access_token']
access_token = self.posit_oauth.get_credentials(self.user_session_token)[
"access_token"
]
return {"Authorization": f"Bearer {access_token}"}

return inner


Expand All @@ -44,8 +48,9 @@ def is_local() -> bool:
return not os.getenv("RSTUDIO_PRODUCT") == "CONNECT"


def viewer_credentials_provider(client: Optional[Client] = None, user_session_token: Optional[str] = None) -> Optional[CredentialsProvider]:

def viewer_credentials_provider(
client: Optional[Client] = None, user_session_token: Optional[str] = None
) -> Optional[CredentialsProvider]:
# If the content is not running on Connect then viewer auth should
# fall back to the locally configured credentials hierarchy
if is_local():
Expand All @@ -57,7 +62,9 @@ def viewer_credentials_provider(client: Optional[Client] = None, user_session_to
# If the user-session-token wasn't provided and we're running on Connect then we raise an exception.
# user_session_token is required to impersonate the viewer.
if user_session_token is None:
raise ValueError("The user-session-token is required for viewer authentication.")
raise ValueError(
"The user-session-token is required for viewer authentication."
)

return PositOAuthIntegrationCredentialsProvider(client.oauth, user_session_token)

Expand Down
9 changes: 2 additions & 7 deletions src/posit/connect/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@ class Credentials(TypedDict, total=False):


class OAuthIntegration:

def __init__(
self, config: Config, session: Session
) -> None:
def __init__(self, config: Config, session: Session) -> None:
self.url = urls.append_path(config.url, "v1/oauth/integrations/credentials")
self.config = config
self.session = session


def get_credentials(self, user_session_token: Optional[str]=None) -> Credentials:

def get_credentials(self, user_session_token: Optional[str] = None) -> Credentials:
# craft a basic credential exchange request where the self.config.api_key owner
# is requesting their own credentials
data = dict()
Expand Down
12 changes: 3 additions & 9 deletions src/posit/connect/oauth_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from .client import Client

class TestOAuthIntegrations:

class TestOAuthIntegrations:
@responses.activate
def test_get_credentials(self):
responses.post(
Expand Down Expand Up @@ -41,11 +41,5 @@ def test_get_credentials(self):
},
)
con = Client(api_key="12345", url="https://connect.example/")
assert (
con.oauth.get_credentials()["access_token"]
== "sdk-user-token"
)
assert (
con.oauth.get_credentials("cit")["access_token"]
== "viewer-token"
)
assert con.oauth.get_credentials()["access_token"] == "sdk-user-token"
assert con.oauth.get_credentials("cit")["access_token"] == "viewer-token"
1 change: 0 additions & 1 deletion src/posit/connect/urls_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ def test_validate_without_scheme():
def test_validate_without_netloc():
with pytest.raises(ValueError):
validate("http:///__api__")

1 change: 0 additions & 1 deletion src/posit/connect/users_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class TestUsers:

@responses.activate
def test_get_users(self):
responses.get(
Expand Down
Loading