Skip to content

Commit

Permalink
New upload URL (#188)
Browse files Browse the repository at this point in the history
* update spec

* linting

* new upload channel

* new release version

* update deps
  • Loading branch information
philkra authored Dec 21, 2023
1 parent f9af0df commit 7607dc6
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 71 deletions.
2 changes: 1 addition & 1 deletion examples/datasets/access_logs.csv
Original file line number Diff line number Diff line change
Expand Up @@ -1029,4 +1029,4 @@ ip,timestamp,method,url_path,version,status_code,bytes,referrer,user_agent,respo
65.209.139.92,27/Dec/2037:12:00:00 +0530,PUT,usr/admin/developer,HTTP/1.0,403,5142,http://www.parker-miller.org/tag/list/list/privacy/,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML; like Gecko) Chrome/89.0.4380.0 Safari/537.36 Edg/89.0.759.0,2603
14.9.21.127,27/Dec/2037:12:00:00 +0530,PUT,usr/login,HTTP/1.0,303,4984,-,Mozilla/5.0 (Linux; Android 10; ONEPLUS A6000) AppleWebKit/537.36 (KHTML; like Gecko) Chrome/87.0.4280.141 Mobile Safari/537.36,1137
55.170.37.108,27/Dec/2037:12:00:00 +0530,PUT,usr/register,HTTP/1.0,303,5028,http://www.parker-miller.org/tag/list/list/privacy/,Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0,2391
252.222.142.134,27/Dec/2037:12:00:00 +0530,DELETE,usr/admin/developer,HTTP/1.0,404,5026,-,Mozilla/5.0 (Linux; Android 10; ONEPLUS A6000) AppleWebKit/537.36 (KHTML; like Gecko) Chrome/77.0.3865.116 Mobile Safari/537.36 EdgA/45.12.4.5121,2551
252.222.142.134,27/Dec/2037:12:00:00 +0530,DELETE,usr/admin/developer,HTTP/1.0,404,5026,-,Mozilla/5.0 (Linux; Android 10; ONEPLUS A6000) AppleWebKit/537.36 (KHTML; like Gecko) Chrome/77.0.3865.116 Mobile Safari/537.36 EdgA/45.12.4.5121,2551
115 changes: 59 additions & 56 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "xata"
version = "1.2.2"
version = "1.2.3"
description = "Python SDK for Xata.io"
authors = ["Xata <support@xata.io>"]
license = "Apache-2.0"
Expand Down
4 changes: 4 additions & 0 deletions tests/unit-tests/api_request_domains_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ def test_workspace_domain(self):

expected = "https://%s.%s.%s" % (ws_id, DEFAULT_REGION, domain)
assert expected == client.table().get_base_url()

def test_upload_base_url(self):
client = XataClient(api_key="123", db_url="https://12345.region-42.staging-xata.dev/db/testopia-042")
assert "https://12345.region-42.upload.staging-xata.dev" == client.databases().get_upload_base_url()
36 changes: 25 additions & 11 deletions xata/api/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

# ------------------------------------------------------- #
# Files
# Files
# Specification: core:v1.0
# CRUD API for operating on binary content in file and file[] columns.
# Specification: workspace:v1.0
# ------------------------------------------------------- #

from requests import request
Expand All @@ -46,6 +46,7 @@ def get_item(
"""
Retrieves file content from an array by file ID
Reference: https://xata.io/docs/api-reference/db/db_branch_name/tables/table_name/data/record_id/column/column_name/file/file_id#download-content-from-a-file-item-in-a-file-array-column
Path: /db/{db_branch_name}/tables/{table_name}/data/{record_id}/column/{column_name}/file/{file_id}
Method: GET
Response status codes:
Expand All @@ -54,6 +55,7 @@ def get_item(
- 401: Authentication Error
- 404: Example response
- 5XX: Unexpected Error
- default: Unexpected Error
Response: */*
:param table_name: str The Table name
Expand All @@ -63,7 +65,7 @@ def get_item(
:param db_name: str = None The name of the database to query. Default: database name from the client.
:param branch_name: str = None The name of the branch to query. Default: branch name from the client.
:return ApiResponse
:returns ApiResponse
"""
db_branch_name = self.client.get_db_branch_name(db_name, branch_name)
url_path = f"/db/{db_branch_name}/tables/{table_name}/data/{record_id}/column/{column_name}/file/{file_id}"
Expand All @@ -83,6 +85,7 @@ def put_item(
"""
Uploads the file content to an array given the file ID
Reference: https://xata.io/docs/api-reference/db/db_branch_name/tables/table_name/data/record_id/column/column_name/file/file_id#upload-or-update-the-content-of-a-file-item-in-a-file-array-column
Path: /db/{db_branch_name}/tables/{table_name}/data/{record_id}/column/{column_name}/file/{file_id}
Method: PUT
Response status codes:
Expand All @@ -93,6 +96,7 @@ def put_item(
- 404: Example response
- 422: Example response
- 5XX: Unexpected Error
- default: Unexpected Error
:param table_name: str The Table name
:param record_id: str The Record name
Expand All @@ -103,12 +107,13 @@ def put_item(
:param db_name: str = None The name of the database to query. Default: database name from the client.
:param branch_name: str = None The name of the branch to query. Default: branch name from the client.
:return ApiResponse
:returns ApiResponse
"""

db_branch_name = self.client.get_db_branch_name(db_name, branch_name)
url_path = f"/db/{db_branch_name}/tables/{table_name}/data/{record_id}/column/{column_name}/file/{file_id}"
headers = {"content-type": content_type}
return self.request("PUT", url_path, headers, data=data)
return self.request("PUT", url_path, headers, data=data, override_base_url=self.get_upload_base_url())

def delete_item(
self,
Expand All @@ -122,6 +127,7 @@ def delete_item(
"""
Deletes an item from an file array column given the file ID
Reference: https://xata.io/docs/api-reference/db/db_branch_name/tables/table_name/data/record_id/column/column_name/file/file_id#delete-an-item-from-a-file-array
Path: /db/{db_branch_name}/tables/{table_name}/data/{record_id}/column/{column_name}/file/{file_id}
Method: DELETE
Response status codes:
Expand All @@ -130,6 +136,7 @@ def delete_item(
- 401: Authentication Error
- 404: Example response
- 5XX: Unexpected Error
- default: Unexpected Error
:param table_name: str The Table name
:param record_id: str The Record name
Expand All @@ -138,7 +145,7 @@ def delete_item(
:param db_name: str = None The name of the database to query. Default: database name from the client.
:param branch_name: str = None The name of the branch to query. Default: branch name from the client.
:return ApiResponse
:returns ApiResponse
"""
db_branch_name = self.client.get_db_branch_name(db_name, branch_name)
url_path = f"/db/{db_branch_name}/tables/{table_name}/data/{record_id}/column/{column_name}/file/{file_id}"
Expand All @@ -150,6 +157,7 @@ def get(
"""
Retrieves the file content from a file column
Reference: https://xata.io/docs/api-reference/db/db_branch_name/tables/table_name/data/record_id/column/column_name/file#download-content-from-a-file-column
Path: /db/{db_branch_name}/tables/{table_name}/data/{record_id}/column/{column_name}/file
Method: GET
Response status codes:
Expand All @@ -159,6 +167,7 @@ def get(
- 401: Authentication Error
- 404: Example response
- 5XX: Unexpected Error
- default: Unexpected Error
Response: */*
:param table_name: str The Table name
Expand All @@ -167,7 +176,7 @@ def get(
:param db_name: str = None The name of the database to query. Default: database name from the client.
:param branch_name: str = None The name of the branch to query. Default: branch name from the client.
:return ApiResponse
:returns ApiResponse
"""
db_branch_name = self.client.get_db_branch_name(db_name, branch_name)
url_path = f"/db/{db_branch_name}/tables/{table_name}/data/{record_id}/column/{column_name}/file"
Expand All @@ -186,6 +195,7 @@ def put(
"""
Uploads the file content to the given file column
Reference: https://xata.io/docs/api-reference/db/db_branch_name/tables/table_name/data/record_id/column/column_name/file#upload-content-to-a-file-column
Path: /db/{db_branch_name}/tables/{table_name}/data/{record_id}/column/{column_name}/file
Method: PUT
Response status codes:
Expand All @@ -196,27 +206,30 @@ def put(
- 404: Example response
- 422: Example response
- 5XX: Unexpected Error
- default: Unexpected Error
:param table_name: str The Table name
:param record_id: str The Record name
:param column_name: str The Column name
:param data: bytes
:param data: bytes content
:param content_type: str Default: "application/octet-stream"
:param db_name: str = None The name of the database to query. Default: database name from the client.
:param branch_name: str = None The name of the branch to query. Default: branch name from the client.
:return ApiResponse
:returns ApiResponse
"""
db_branch_name = self.client.get_db_branch_name(db_name, branch_name)
url_path = f"/db/{db_branch_name}/tables/{table_name}/data/{record_id}/column/{column_name}/file"
headers = {"content-type": content_type}
return self.request("PUT", url_path, headers, data=data)
return self.request("PUT", url_path, headers, data=data, override_base_url=self.get_upload_base_url())

def delete(
self, table_name: str, record_id: str, column_name: str, db_name: str = None, branch_name: str = None
) -> ApiResponse:
"""
Deletes a file referred in a file column
Reference: https://xata.io/docs/api-reference/db/db_branch_name/tables/table_name/data/record_id/column/column_name/file#remove-the-content-from-a-file-column
Path: /db/{db_branch_name}/tables/{table_name}/data/{record_id}/column/{column_name}/file
Method: DELETE
Response status codes:
Expand All @@ -225,14 +238,15 @@ def delete(
- 401: Authentication Error
- 404: Example response
- 5XX: Unexpected Error
- default: Unexpected Error
:param table_name: str The Table name
:param record_id: str The Record name
:param column_name: str The Column name
:param db_name: str = None The name of the database to query. Default: database name from the client.
:param branch_name: str = None The name of the branch to query. Default: branch name from the client.
:return ApiResponse
:returns ApiResponse
"""
db_branch_name = self.client.get_db_branch_name(db_name, branch_name)
url_path = f"/db/{db_branch_name}/tables/{table_name}/data/{record_id}/column/{column_name}/file"
Expand Down
14 changes: 13 additions & 1 deletion xata/api_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ def get_base_url(self) -> str:
cfg = self.client.get_config()
return "https://%s.%s.%s" % (cfg["workspaceId"], cfg["region"], cfg["domain_workspace"])

def get_upload_base_url(self) -> str:
"""
Upload of files has dedidcated URL that is not represented in the OAS
:returns str
"""
cfg = self.client.get_config()
return "https://%s.%s.upload.%s" % (cfg["workspaceId"], cfg["region"], cfg["domain_workspace"])

def request(
self,
http_method: str,
Expand All @@ -53,6 +62,7 @@ def request(
payload: dict = None,
data: bytes = None,
is_streaming: bool = False,
override_base_url=None,
) -> ApiResponse:
"""
:param http_method: str
Expand All @@ -61,6 +71,7 @@ def request(
:param payload: dict = None
:param data: bytes = None
:param is_streaming: bool = False
:param override_base_url = None Set alternative base URL
:returns ApiResponse
Expand All @@ -69,7 +80,8 @@ def request(
:raises ServerError
"""
headers = {**headers, **self.client.get_headers()}
url = "%s/%s" % (self.get_base_url(), url_path.lstrip("/"))
base_url = self.get_base_url() if override_base_url is None else override_base_url
url = "%s/%s" % (base_url, url_path.lstrip("/"))

# In order not exhaust the connection pool with open connections from unread streams
# we opt for Session usage on all non-stream requests
Expand Down
2 changes: 1 addition & 1 deletion xata/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

# TODO this is a manual task, to keep in sync with pyproject.toml
# could/should be automated to keep in sync
__version__ = "1.2.2"
__version__ = "1.2.3"

PERSONAL_API_KEY_LOCATION = "~/.config/xata/key"
DEFAULT_DATA_PLANE_DOMAIN = "xata.sh"
Expand Down

0 comments on commit 7607dc6

Please sign in to comment.