Skip to content

Commit

Permalink
remove api_key from context and add docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
tdstein committed Jul 24, 2024
1 parent c772916 commit d901ade
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/posit/connect/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ def __init__(self, *args, **kwargs) -> None:
if "url" in kwargs and isinstance(kwargs["url"], str):
url = kwargs["url"]

cfg = Config(api_key=api_key, url=url)
self.cfg = Config(api_key=api_key, url=url)
session = Session()
session.auth = Auth(config=cfg)
session.auth = Auth(config=self.cfg)
session.hooks["response"].append(hooks.check_for_deprecation_header)
session.hooks["response"].append(hooks.handle_errors)
self.ctx = Context(api_key=cfg.api_key, session=session, url=cfg.url)
self.ctx = Context(session=session, url=self.cfg.url)

@property
def version(self) -> str:
Expand Down Expand Up @@ -197,7 +197,7 @@ def oauth(self) -> OAuthIntegration:
OAuthIntegration
The OAuth integration instance.
"""
return OAuthIntegration(self.ctx)
return OAuthIntegration(self.ctx, self.cfg.api_key)

@property
def groups(self) -> Groups:
Expand Down
16 changes: 15 additions & 1 deletion src/posit/connect/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@

@dataclass(frozen=True)
class Context:
api_key: str
"""Context for information shared across the resource stack.
Attributes
----------
session: request.Session
url: str
The Connect API URL (e.g., 'https://connect.example.com/__api__')
Notes
-----
This class should only contain attributes that are common across all resources.
Adding attributes for convenience that are not required by all resources is an anti-pattern.
"""

session: requests.Session
url: str
7 changes: 4 additions & 3 deletions src/posit/connect/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ class Credentials(TypedDict, total=False):


class OAuthIntegration:
def __init__(self, ctx: Context) -> None:
self.url = urls.append(ctx.url, "v1/oauth/integrations/credentials")
def __init__(self, ctx: Context, api_key: str) -> None:
self.ctx = ctx
self.api_key = api_key
self.url = urls.append(ctx.url, "v1/oauth/integrations/credentials")

def get_credentials(
self, user_session_token: Optional[str] = None
Expand All @@ -26,7 +27,7 @@ def get_credentials(
data = dict()
data["grant_type"] = "urn:ietf:params:oauth:grant-type:token-exchange"
data["subject_token_type"] = "urn:posit:connect:api-key"
data["subject_token"] = self.ctx.api_key
data["subject_token"] = self.api_key

# if this content is running on Connect, then it is allowed to request
# the content viewer's credentials
Expand Down
2 changes: 0 additions & 2 deletions tests/posit/connect/metrics/test_shiny_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def test(self):

# setup
ctx = Context(
api_key="12345",
session=requests.Session(),
url="https://connect.example/__api__",
)
Expand Down Expand Up @@ -119,7 +118,6 @@ def test(self):

# setup
ctx = Context(
api_key="12345",
session=requests.Session(),
url="https://connect.example/__api__",
)
Expand Down
2 changes: 0 additions & 2 deletions tests/posit/connect/metrics/test_visits.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def test(self):

# setup
ctx = Context(
api_key="12345",
session=requests.Session(),
url="https://connect.example/__api__",
)
Expand Down Expand Up @@ -128,7 +127,6 @@ def test(self):

# setup
ctx = Context(
api_key="12345",
session=requests.Session(),
url="https://connect.example/__api__",
)
Expand Down
1 change: 0 additions & 1 deletion tests/posit/connect/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ def test(self):

# setup
ctx = Context(
api_key="12345",
session=requests.Session(),
url="https://connect.example/__api__",
)
Expand Down
8 changes: 0 additions & 8 deletions tests/posit/connect/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def test(self):

# setup
ctx = Context(
api_key="12345",
session=requests.Session(),
url="https://connect.example/__api__",
)
Expand Down Expand Up @@ -79,7 +78,6 @@ def test_request_shape(self):

# setup
ctx = Context(
api_key="12345",
session=requests.Session(),
url="https://connect.example/__api__",
)
Expand Down Expand Up @@ -128,7 +126,6 @@ def test_role_update(self):

# setup
ctx = Context(
api_key="12345",
session=requests.Session(),
url="https://connect.example/__api__",
)
Expand Down Expand Up @@ -159,7 +156,6 @@ def test(self):

# setup
ctx = Context(
api_key="12345",
session=requests.Session(),
url="https://connect.example/__api__",
)
Expand Down Expand Up @@ -205,7 +201,6 @@ def test(self):

# setup
ctx = Context(
api_key="12345",
session=requests.Session(),
url="https://connect.example/__api__",
)
Expand Down Expand Up @@ -239,7 +234,6 @@ def test(self):

# setup
ctx = Context(
api_key="12345",
session=requests.Session(),
url="https://connect.example/__api__",
)
Expand Down Expand Up @@ -269,7 +263,6 @@ def test(self):

# setup
ctx = Context(
api_key="12345",
session=requests.Session(),
url="https://connect.example/__api__",
)
Expand Down Expand Up @@ -300,7 +293,6 @@ def test(self):

# setup
ctx = Context(
api_key="12345",
session=requests.Session(),
url="https://connect.example/__api__",
)
Expand Down

0 comments on commit d901ade

Please sign in to comment.