catworld-sdk 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- catworld_sdk-0.1.0/.gitignore +16 -0
- catworld_sdk-0.1.0/LICENSE +21 -0
- catworld_sdk-0.1.0/PKG-INFO +46 -0
- catworld_sdk-0.1.0/README.md +33 -0
- catworld_sdk-0.1.0/pyproject.toml +18 -0
- catworld_sdk-0.1.0/src/catworld/__init__.py +3 -0
- catworld_sdk-0.1.0/src/catworld/client.py +43 -0
- catworld_sdk-0.1.0/src/catworld/exceptions.py +6 -0
- catworld_sdk-0.1.0/tests/test_client.py +5 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
node_modules/
|
|
2
|
+
.next/
|
|
3
|
+
coverage/
|
|
4
|
+
.env
|
|
5
|
+
.env.local
|
|
6
|
+
.env.*.local
|
|
7
|
+
!.env.example
|
|
8
|
+
*.log
|
|
9
|
+
.codex-dev.log
|
|
10
|
+
tmp/
|
|
11
|
+
var/
|
|
12
|
+
dist/
|
|
13
|
+
sdk/python/dist/
|
|
14
|
+
sdk/python/*.egg-info/
|
|
15
|
+
sdk/python/**/__pycache__/sdk/python/.pytest_cache/
|
|
16
|
+
sdk/python/pytest-cache-files-*/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vinicius Moreira
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: catworld-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Cliente oficial Python para a API Catworld
|
|
5
|
+
Author-email: Vinicius Moreira <vinicius@77indicadores.com.br>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Requires-Dist: httpx<1,>=0.27
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# catworld-sdk
|
|
15
|
+
|
|
16
|
+
Cliente oficial Python para a API Catworld.
|
|
17
|
+
|
|
18
|
+
## Instalação
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install catworld-sdk
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Uso
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from catworld import CatworldClient
|
|
28
|
+
|
|
29
|
+
with CatworldClient("https://seu-catworld.exemplo.com", "cw_live_...") as client:
|
|
30
|
+
projects = client.projects()
|
|
31
|
+
datasets = client.datasets()
|
|
32
|
+
|
|
33
|
+
result = client.upload("dados.csv", dataset_id="...", mode="replace")
|
|
34
|
+
print(result["status"], result["rowCount"])
|
|
35
|
+
|
|
36
|
+
rows = client.query("SELECT TOP 10 * FROM [schema].[tabela]")
|
|
37
|
+
print(rows["rows"])
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Métodos
|
|
41
|
+
|
|
42
|
+
- `projects()` / `datasets()` / `tables(dataset_id)` / `rows(table_id, limit=100)`
|
|
43
|
+
- `query(sql, timeout=30, limit=10000)`
|
|
44
|
+
- `upload(path, dataset_id, mode="replace", key_column=None, poll_interval=2)`
|
|
45
|
+
|
|
46
|
+
O token de API precisa de permissão `WRITE` (ou `ADMIN`) no escopo do dataset para usar `upload()`.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# catworld-sdk
|
|
2
|
+
|
|
3
|
+
Cliente oficial Python para a API Catworld.
|
|
4
|
+
|
|
5
|
+
## Instalação
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install catworld-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Uso
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from catworld import CatworldClient
|
|
15
|
+
|
|
16
|
+
with CatworldClient("https://seu-catworld.exemplo.com", "cw_live_...") as client:
|
|
17
|
+
projects = client.projects()
|
|
18
|
+
datasets = client.datasets()
|
|
19
|
+
|
|
20
|
+
result = client.upload("dados.csv", dataset_id="...", mode="replace")
|
|
21
|
+
print(result["status"], result["rowCount"])
|
|
22
|
+
|
|
23
|
+
rows = client.query("SELECT TOP 10 * FROM [schema].[tabela]")
|
|
24
|
+
print(rows["rows"])
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Métodos
|
|
28
|
+
|
|
29
|
+
- `projects()` / `datasets()` / `tables(dataset_id)` / `rows(table_id, limit=100)`
|
|
30
|
+
- `query(sql, timeout=30, limit=10000)`
|
|
31
|
+
- `upload(path, dataset_id, mode="replace", key_column=None, poll_interval=2)`
|
|
32
|
+
|
|
33
|
+
O token de API precisa de permissão `WRITE` (ou `ADMIN`) no escopo do dataset para usar `upload()`.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.26"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
[project]
|
|
5
|
+
name = "catworld-sdk"
|
|
6
|
+
version = "0.1.0"
|
|
7
|
+
description = "Cliente oficial Python para a API Catworld"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
license = "MIT"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
authors = [{ name = "Vinicius Moreira", email = "vinicius@77indicadores.com.br" }]
|
|
12
|
+
dependencies = ["httpx>=0.27,<1"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
]
|
|
17
|
+
[tool.hatch.build.targets.wheel]
|
|
18
|
+
packages = ["src/catworld"]
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
from .client import CatworldClient
|
|
2
|
+
from .exceptions import AuthenticationError,CatworldError,ConnectionError,PermissionDeniedError,QueryTimeoutError,ValidationError
|
|
3
|
+
__all__=["CatworldClient","CatworldError","AuthenticationError","PermissionDeniedError","ValidationError","QueryTimeoutError","ConnectionError"]
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import time
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any
|
|
5
|
+
import httpx
|
|
6
|
+
from .exceptions import AuthenticationError,PermissionDeniedError,ValidationError,QueryTimeoutError,ConnectionError
|
|
7
|
+
class CatworldClient:
|
|
8
|
+
def __init__(self,base_url:str,token:str,timeout:float=30):
|
|
9
|
+
self._client=httpx.Client(base_url=base_url.rstrip("/"),headers={"Authorization":f"Bearer {token}"},timeout=timeout)
|
|
10
|
+
def close(self): self._client.close()
|
|
11
|
+
def __enter__(self): return self
|
|
12
|
+
def __exit__(self,*_): self.close()
|
|
13
|
+
def projects(self): return self._request("GET","/api/v1/projects")
|
|
14
|
+
def datasets(self): return self._request("GET","/api/v1/datasets")
|
|
15
|
+
def tables(self,dataset_id:str): return self._request("GET",f"/api/v1/datasets/{dataset_id}/tables")
|
|
16
|
+
def rows(self,table_id:str,limit:int=100): return self._request("GET",f"/api/v1/tables/{table_id}/rows",params={"limit":limit})
|
|
17
|
+
def query(self,sql:str,timeout:int=30,limit:int=10000): return self._request("POST","/api/v1/queries",json={"sql":sql,"timeout":timeout,"limit":limit})
|
|
18
|
+
def upload(self,path:str|Path,dataset_id:str,mode:str="replace",key_column:str|None=None,poll_interval:float=2):
|
|
19
|
+
file=Path(path);created=self._request("POST","/api/v1/uploads",json={"filename":file.name,"sizeBytes":file.stat().st_size});
|
|
20
|
+
with file.open("rb") as stream:
|
|
21
|
+
response=self._client.put(created["sas"]["url"],content=stream,headers={"content-type":"application/octet-stream"},timeout=None);response.raise_for_status()
|
|
22
|
+
upload_id=created["upload"]["id"];self._request("POST",f"/api/v1/uploads/{upload_id}/uploaded");preview=self._wait(upload_id,"AWAITING_CONFIRMATION",poll_interval)
|
|
23
|
+
mapping=preview["previewJson"];import json as _json
|
|
24
|
+
if isinstance(mapping,str): mapping=_json.loads(mapping)
|
|
25
|
+
self._request("POST",f"/api/v1/uploads/{upload_id}/confirm",json={"datasetId":dataset_id,"mode":mode,"keyColumn":key_column,"mapping":mapping["columns"]})
|
|
26
|
+
return self._wait(upload_id,"COMPLETED",poll_interval)
|
|
27
|
+
def _wait(self,upload_id,target,interval):
|
|
28
|
+
for _ in range(1800):
|
|
29
|
+
upload=self._request("GET",f"/api/v1/uploads/{upload_id}")
|
|
30
|
+
if upload["status"]==target:return upload
|
|
31
|
+
if upload["status"]=="FAILED":raise ValidationError(upload.get("errorMessage") or "Upload falhou")
|
|
32
|
+
time.sleep(interval)
|
|
33
|
+
raise QueryTimeoutError("Tempo de processamento excedido")
|
|
34
|
+
def _request(self,method,path,**kwargs)->Any:
|
|
35
|
+
try: response=self._client.request(method,path,**kwargs)
|
|
36
|
+
except httpx.HTTPError as exc: raise ConnectionError(str(exc)) from exc
|
|
37
|
+
if response.is_success:return response.json()["data"]
|
|
38
|
+
body=response.json();message=body.get("error",{}).get("message",response.text)
|
|
39
|
+
if response.status_code==401:raise AuthenticationError(message)
|
|
40
|
+
if response.status_code==403:raise PermissionDeniedError(message)
|
|
41
|
+
if response.status_code in (400,422):raise ValidationError(message)
|
|
42
|
+
if response.status_code==408:raise QueryTimeoutError(message)
|
|
43
|
+
raise ConnectionError(message)
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
class CatworldError(Exception): pass
|
|
2
|
+
class AuthenticationError(CatworldError): pass
|
|
3
|
+
class PermissionDeniedError(CatworldError): pass
|
|
4
|
+
class ValidationError(CatworldError): pass
|
|
5
|
+
class QueryTimeoutError(CatworldError): pass
|
|
6
|
+
class ConnectionError(CatworldError): pass
|