Skip to content

Commit

Permalink
feat: Allow specifying org id in client (#3878)
Browse files Browse the repository at this point in the history
* feat: Allow specifying org id in client

* review feedback
  • Loading branch information
conradoverta committed Jun 12, 2023
1 parent 3a80bba commit 67af06a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
4 changes: 4 additions & 0 deletions client/verta/verta/client.py
Expand Up @@ -94,6 +94,8 @@ class Client(object):
Whether to print extra verbose information to aid in debugging.
extra_auth_headers : dict, default {}
Extra headers to include on requests, like to permit traffic through a restrictive application load balancer
organization_id : str, optional
(alpha) Organization to use for the client calls. If not provided, the default organization will be used.
_connect : str, default True
Whether to connect to server (``False`` for unit tests).
Expand Down Expand Up @@ -142,6 +144,7 @@ def __init__(
extra_auth_headers={},
jwt_token=None,
jwt_token_sig=None,
organization_id=None,
_connect=True,
):
self._load_config()
Expand Down Expand Up @@ -170,6 +173,7 @@ def __init__(
dev_key=dev_key,
jwt_token=jwt_token,
jwt_token_sig=jwt_token_sig,
organization_id=organization_id, # TODO: add organization_name as parameter and resolve that
)
self._workspace = self._get_with_fallback(None, env_var="VERTA_WORKSPACE")

Expand Down
28 changes: 20 additions & 8 deletions client/verta/verta/credentials.py
Expand Up @@ -46,32 +46,38 @@ class EmailCredentials(Credentials):
A user email address.
dev_key : str
A dev key to use for authentication.
organization_id : str, optional
An organization ID to use for authentication.
"""

EMAIL_ENV = "VERTA_EMAIL"
DEV_KEY_ENV = "VERTA_DEV_KEY"

def __init__(self, email, dev_key):
def __init__(self, email, dev_key, organization_id=None):
self.email = email
self.dev_key = dev_key
self.organization_id = organization_id

def export_env_vars_to_os(self):
os.environ[self.EMAIL_ENV] = self.email
os.environ[self.DEV_KEY_ENV] = self.dev_key

def headers(self):
return {
headers = {
"source": "PythonClient",
"email": self.email,
"developer_key": self.dev_key,
# without underscore, for NGINX support
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls#missing-disappearing-http-headers
"developer-key": self.dev_key,
}
if self.organization_id:
headers["organization-id"] = self.organization_id
return headers

def __repr__(self):
key = self.dev_key[:8] + re.sub(r"[^-]", "*", self.dev_key[8:])
return "EmailCredentials({}, {})".format(self.email, key)
return "EmailCredentials({}, {}, {})".format(self.email, key, self.organization_id)

@classmethod
def load_from_os_env(cls):
Expand All @@ -92,14 +98,17 @@ class JWTCredentials(Credentials):
A jwt token.
jwt_token_sig : str
A jwt token signature.
organization_id : str, optional
An organization ID to use for authentication.
"""

JWT_TOKEN_ENV = "VERTA_JWT_TOKEN"
JWT_TOKEN_SIG_ENV = "VERTA_JWT_TOKEN_SIG"

def __init__(self, jwt_token, jwt_token_sig):
def __init__(self, jwt_token, jwt_token_sig, organization_id=None):
self.jwt_token = jwt_token
self.jwt_token_sig = jwt_token_sig
self.organization_id = organization_id

def export_env_vars_to_os(self):
os.environ[self.JWT_TOKEN_ENV] = self.jwt_token
Expand All @@ -113,16 +122,19 @@ def headers(self):
# without underscore, for NGINX support
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls#missing-disappearing-http-headers
"bearer-access-token": self.jwt_token,
"organization-id": self.organization_id or "",
}
if self.jwt_token_sig:
headers["bearer_access_token_sig"] = headers[
"bearer-access-token-sig"
] = self.jwt_token_sig
if self.organization_id:
headers["organization-id"] = self.organization_id
return headers

def __repr__(self):
token = self.jwt_token[:8] + re.sub(r"[^-]", "*", self.jwt_token[8:])
return "JWTCredentials({}, {})".format(token, self.jwt_token_sig)
return "JWTCredentials({}, {}, {})".format(token, self.jwt_token_sig, self.organization_id)

@classmethod
def load_from_os_env(cls):
Expand Down Expand Up @@ -152,11 +164,11 @@ def load_from_os_env():
return credentials


def _build(email=None, dev_key=None, jwt_token=None, jwt_token_sig=None):
def _build(email=None, dev_key=None, jwt_token=None, jwt_token_sig=None, organization_id=None):
if email and dev_key:
return EmailCredentials(email, dev_key)
return EmailCredentials(email, dev_key, organization_id=organization_id)
elif jwt_token:
return JWTCredentials(jwt_token, jwt_token_sig)
return JWTCredentials(jwt_token, jwt_token_sig, organization_id=organization_id)
elif email or dev_key:
raise ValueError("`email` and `dev_key` must be provided together")
else:
Expand Down

2 comments on commit 67af06a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker Tag: main-2023-06-12T21-29-55--67af06a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Total coverage (common): 11.24
Total coverage (server): 60.47

Changed Files coverage (common): coverage 100
Changed Files coverage (server): 100

Please sign in to comment.