Skip to content
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
32 changes: 16 additions & 16 deletions python/pyiceberg/catalog/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,14 @@ class RestCatalog(Catalog):
token: str
config: Properties

host: str
uri: str

def __init__(
self,
name: str,
properties: Properties,
host: str,
client_id: Optional[str] = None,
client_secret: Optional[str] = None,
uri: str,
credentials: Optional[str] = None,
token: Optional[str] = None,
):
"""Rest Catalog
Expand All @@ -170,18 +169,15 @@ def __init__(
Args:
name: Name to identify the catalog
properties: Properties that are passed along to the configuration
host: The base-url of the REST Catalog endpoint
client_id: The id to identify the client
client_secret: The secret for the client
uri: The base-url of the REST Catalog endpoint
credentials: The credentials for authentication against the client
token: The bearer token
"""
self.host = host
if client_id and client_secret:
self.token = self._fetch_access_token(client_id, client_secret)
self.uri = uri
if credentials:
self.token = self._fetch_access_token(credentials)
elif token:
self.token = token
else:
raise ValueError("Either set the client_id and client_secret, or provide a valid token")
self.config = self._fetch_config(properties)
super().__init__(name, properties)

Expand All @@ -200,23 +196,26 @@ def _split_credential(token: str) -> Tuple[str, str]:

@property
def headers(self) -> Properties:
return {
AUTHORIZATION_HEADER: f"{BEARER_PREFIX} {self.token}",
headers = {
"Content-type": "application/json",
"X-Client-Version": __version__,
}
if self.token:
headers[AUTHORIZATION_HEADER] = f"{BEARER_PREFIX} {self.token}"
return headers

def url(self, endpoint: str, prefixed: bool = True, **kwargs) -> str:
"""Constructs the endpoint

Args:
endpoint: Resource identifier that points to the REST catalog
prefixed: If the prefix return by the config needs to be appended

Returns:
The base url of the rest catalog
"""

url = self.host
url = self.uri
url = url + "v1/" if url.endswith("/") else url + "/v1/"

if prefixed:
Expand All @@ -225,7 +224,8 @@ def url(self, endpoint: str, prefixed: bool = True, **kwargs) -> str:

return url + endpoint.format(**kwargs)

def _fetch_access_token(self, client_id: str, client_secret: str) -> str:
def _fetch_access_token(self, credentials: str) -> str:
client_id, client_secret = credentials.split(":")
data = {GRANT_TYPE: CLIENT_CREDENTIALS, CLIENT_ID: client_id, CLIENT_SECRET: client_secret, SCOPE: CATALOG_SCOPE}
url = self.url(Endpoints.get_token, prefixed=False)
# Uses application/x-www-form-urlencoded by default
Expand Down
16 changes: 8 additions & 8 deletions python/pyiceberg/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ class ValidationError(Exception):
"""Raises when there is an issue with the schema"""


class NoSuchTableError(Exception):
"""Raises when the table can't be found in the REST catalog"""


class NoSuchNamespaceError(Exception):
"""Raised when a referenced name-space is not found"""


class RESTError(Exception):
"""Raises when there is an unknown response from the REST Catalog"""

Expand Down Expand Up @@ -62,11 +70,3 @@ class ForbiddenError(RESTError):

class AuthorizationExpiredError(RESTError):
"""When the credentials are expired when performing an action on the REST catalog"""


class NoSuchTableError(RESTError):
"""Raises when the table can't be found in the REST catalog"""


class NoSuchNamespaceError(RESTError):
"""Raised when a referenced name-space is not found"""
Loading