Skip to content

Commit

Permalink
feat: add csv() modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
anand2312 committed Sep 30, 2023
1 parent 17e9f09 commit 2394306
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
12 changes: 12 additions & 0 deletions postgrest/_async/request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ def text_search(
session=self.session, # type: ignore
)

def csv(self) -> AsyncSingleRequestBuilder[str]:
"""Specify that the query must retrieve data as a single CSV string."""
self.headers["Accept"] = "text/csv"
return AsyncSingleRequestBuilder[str](

Check warning on line 258 in postgrest/_async/request_builder.py

View check run for this annotation

Codecov / codecov/patch

postgrest/_async/request_builder.py#L257-L258

Added lines #L257 - L258 were not covered by tests
session=self.session, # type: ignore
path=self.path,
http_method=self.http_method,
headers=self.headers,
params=self.params,
json=self.json,
)


class AsyncRequestBuilder(Generic[_ReturnT]):
def __init__(self, session: AsyncClient, path: str) -> None:
Expand Down
12 changes: 12 additions & 0 deletions postgrest/_sync/request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ def text_search(
session=self.session, # type: ignore
)

def csv(self) -> SyncSingleRequestBuilder[str]:
"""Specify that the query must retrieve data as a single CSV string."""
self.headers["Accept"] = "text/csv"
return SyncSingleRequestBuilder[str](

Check warning on line 258 in postgrest/_sync/request_builder.py

View check run for this annotation

Codecov / codecov/patch

postgrest/_sync/request_builder.py#L257-L258

Added lines #L257 - L258 were not covered by tests
session=self.session, # type: ignore
path=self.path,
http_method=self.http_method,
headers=self.headers,
params=self.params,
json=self.json,
)


class SyncRequestBuilder(Generic[_ReturnT]):
def __init__(self, session: SyncClient, path: str) -> None:
Expand Down
8 changes: 7 additions & 1 deletion postgrest/base_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,14 @@ class SingleAPIResponse(APIResponse[_ReturnT], Generic[_ReturnT]):
def from_http_request_response(
cls: Type[Self], request_response: RequestResponse
) -> Self:
data = request_response.json()
count = cls._get_count_from_http_request_response(request_response)
try:
data = request_response.json()
except JSONDecodeError:
if len(request_response.text) > 0:
data = request_response.text

Check warning on line 208 in postgrest/base_request_builder.py

View check run for this annotation

Codecov / codecov/patch

postgrest/base_request_builder.py#L206-L208

Added lines #L206 - L208 were not covered by tests
else:
data = []

Check warning on line 210 in postgrest/base_request_builder.py

View check run for this annotation

Codecov / codecov/patch

postgrest/base_request_builder.py#L210

Added line #L210 was not covered by tests
return cls[_ReturnT](data=data, count=count) # type: ignore

@classmethod
Expand Down

0 comments on commit 2394306

Please sign in to comment.