rowbind 0.1.0a1__py3-none-any.whl
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.
- rowbind/__init__.py +22 -0
- rowbind/_catalog.py +73 -0
- rowbind/_generated_catalog.py +32 -0
- rowbind/_generated_raw.py +59 -0
- rowbind/_invoice_sync.py +166 -0
- rowbind/_raw.py +103 -0
- rowbind/_transport.py +224 -0
- rowbind/_wire.py +21 -0
- rowbind/client.py +388 -0
- rowbind/examples/__init__.py +1 -0
- rowbind/examples/catalog_invoice.py +42 -0
- rowbind/examples/first_invoice.py +28 -0
- rowbind/examples/invoice_pages.py +21 -0
- rowbind/models.py +316 -0
- rowbind/py.typed +0 -0
- rowbind-0.1.0a1.dist-info/METADATA +5 -0
- rowbind-0.1.0a1.dist-info/RECORD +19 -0
- rowbind-0.1.0a1.dist-info/WHEEL +5 -0
- rowbind-0.1.0a1.dist-info/top_level.txt +1 -0
rowbind/__init__.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Rowbind's typed client for sandbox invoices on QuickBooks Desktop and NetSuite."""
|
|
2
|
+
from ._raw import NetSuiteRawRead, QuickBooksRawRead, SuiteQLPage, SuiteQLResult
|
|
3
|
+
from ._catalog import CatalogKind, CatalogLink, CatalogPage, CatalogRecord
|
|
4
|
+
from ._invoice_sync import InvoiceSyncStatus, SyncedInvoice, SyncedInvoiceDetails, SyncedInvoiceLine, SyncedInvoicePage
|
|
5
|
+
from ._transport import RowbindError
|
|
6
|
+
from .client import AsyncRowbind, OperationWaitError, Rowbind
|
|
7
|
+
from .models import (
|
|
8
|
+
Company, Connection, ConnectionHealth, ConnectSession, CreatedConnectSession, FailedOperation, InvoiceLine,
|
|
9
|
+
InvoiceLineRecord, InvoiceOperationId, InvoiceRecord, NetSuiteConnection, Operation,
|
|
10
|
+
OperationReceipt, PendingOperation, Provider, QuickBooksConnection, RecoveryEntry,
|
|
11
|
+
RecoveryPage, VerifiedOperation,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"CatalogKind", "CatalogLink", "CatalogPage", "CatalogRecord",
|
|
16
|
+
"AsyncRowbind", "Company", "Connection", "ConnectionHealth", "ConnectSession", "CreatedConnectSession",
|
|
17
|
+
"FailedOperation", "InvoiceLine", "InvoiceLineRecord", "InvoiceOperationId", "InvoiceRecord",
|
|
18
|
+
"NetSuiteConnection", "Operation", "OperationReceipt", "OperationWaitError", "PendingOperation",
|
|
19
|
+
"NetSuiteRawRead", "Provider", "QuickBooksConnection", "QuickBooksRawRead", "RecoveryEntry", "RecoveryPage", "Rowbind",
|
|
20
|
+
"RowbindError", "SuiteQLPage", "SuiteQLResult", "VerifiedOperation", "InvoiceSyncStatus",
|
|
21
|
+
"SyncedInvoice", "SyncedInvoiceDetails", "SyncedInvoiceLine", "SyncedInvoicePage",
|
|
22
|
+
]
|
rowbind/_catalog.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""Search selectable records and bind SaaS references in a single authorized book."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import Literal, Optional
|
|
6
|
+
|
|
7
|
+
from ._transport import RowbindError, Transport
|
|
8
|
+
from .models import _array, _invalid, _object, _string
|
|
9
|
+
|
|
10
|
+
CatalogKind = Literal["customer", "item"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(frozen=True)
|
|
14
|
+
class CatalogRecord:
|
|
15
|
+
native_id: str
|
|
16
|
+
display_name: str
|
|
17
|
+
sku: Optional[str] = None
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class CatalogPage:
|
|
22
|
+
records: tuple[CatalogRecord, ...]
|
|
23
|
+
next_cursor: Optional[str]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class CatalogLink:
|
|
28
|
+
kind: CatalogKind
|
|
29
|
+
external_id: str
|
|
30
|
+
native_id: str
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _text(value: str, field: str) -> str:
|
|
34
|
+
if not isinstance(value, str) or not value.strip() or len(value) > 200:
|
|
35
|
+
raise RowbindError("invalid_input", f"Provide a nonblank {field} of up to 200 characters.")
|
|
36
|
+
return value
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _path(connection_id: str, kind: CatalogKind) -> dict[str, str]:
|
|
40
|
+
if kind not in ("customer", "item"):
|
|
41
|
+
raise RowbindError("invalid_input", "Choose customer or item.")
|
|
42
|
+
return {"id": connection_id, "kind": kind}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _search(transport: Transport, operation: Literal["searchCatalog"], connection_id: str, *,
|
|
46
|
+
kind: CatalogKind, query: str, after: Optional[str]) -> CatalogPage:
|
|
47
|
+
params = {"query": _text(query, "query")}
|
|
48
|
+
if after is not None:
|
|
49
|
+
if not isinstance(after, str) or not after:
|
|
50
|
+
raise RowbindError("invalid_input", "Provide the search cursor returned by Rowbind.")
|
|
51
|
+
params["after"] = after
|
|
52
|
+
data = transport.request(operation, path=_path(connection_id, kind), query=params)
|
|
53
|
+
records = []
|
|
54
|
+
values = _array(data.get("records"))
|
|
55
|
+
if len(values) > 50:
|
|
56
|
+
raise _invalid("catalog page length")
|
|
57
|
+
for value in values:
|
|
58
|
+
record = _object(value)
|
|
59
|
+
records.append(CatalogRecord(_string(record, "nativeId"), _string(record, "displayName"),
|
|
60
|
+
_string(record, "sku") if "sku" in record else None))
|
|
61
|
+
if "nextCursor" not in data:
|
|
62
|
+
raise _invalid("catalog cursor")
|
|
63
|
+
cursor = None if data["nextCursor"] is None else _string(data, "nextCursor")
|
|
64
|
+
return CatalogPage(tuple(records), cursor)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _link(transport: Transport, operation: Literal["linkCatalog"], connection_id: str, *,
|
|
68
|
+
kind: CatalogKind, external_id: str, native_id: str) -> CatalogLink:
|
|
69
|
+
body = {"externalId": _text(external_id, "external ID"), "nativeId": _text(native_id, "native ID")}
|
|
70
|
+
data = transport.request(operation, path=_path(connection_id, kind), body=body)
|
|
71
|
+
if data.get("kind") != kind or data.get("externalId") != external_id or data.get("nativeId") != native_id:
|
|
72
|
+
raise _invalid("catalog link")
|
|
73
|
+
return CatalogLink(kind, external_id, native_id)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Generated from the Rowbind SDK method map and OpenAPI. Do not edit.
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import asyncio
|
|
5
|
+
from typing import Optional
|
|
6
|
+
|
|
7
|
+
from ._catalog import CatalogKind, CatalogLink, CatalogPage, _link, _search
|
|
8
|
+
from ._transport import Transport
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Catalog:
|
|
12
|
+
def __init__(self, transport: Transport) -> None:
|
|
13
|
+
self._transport = transport
|
|
14
|
+
|
|
15
|
+
def search(self, connection_id: str, *, kind: CatalogKind, query: str, after: Optional[str] = None) -> CatalogPage:
|
|
16
|
+
"""Search one book. Pass next_cursor as after to see more candidates."""
|
|
17
|
+
return _search(self._transport, "searchCatalog", connection_id, kind=kind, query=query, after=after)
|
|
18
|
+
|
|
19
|
+
def link(self, connection_id: str, *, kind: CatalogKind, external_id: str, native_id: str) -> CatalogLink:
|
|
20
|
+
"""Bind an external reference to the selected native record in this book."""
|
|
21
|
+
return _link(self._transport, "linkCatalog", connection_id, kind=kind, external_id=external_id, native_id=native_id)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class AsyncCatalog:
|
|
25
|
+
def __init__(self, sync: Catalog) -> None:
|
|
26
|
+
self._sync = sync
|
|
27
|
+
|
|
28
|
+
async def search(self, connection_id: str, *, kind: CatalogKind, query: str, after: Optional[str] = None) -> CatalogPage:
|
|
29
|
+
return await asyncio.to_thread(self._sync.search, connection_id, kind=kind, query=query, after=after)
|
|
30
|
+
|
|
31
|
+
async def link(self, connection_id: str, *, kind: CatalogKind, external_id: str, native_id: str) -> CatalogLink:
|
|
32
|
+
return await asyncio.to_thread(self._sync.link, connection_id, kind=kind, external_id=external_id, native_id=native_id)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Generated from the Rowbind SDK method map and OpenAPI. Do not edit.
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import asyncio
|
|
5
|
+
from typing import Literal
|
|
6
|
+
|
|
7
|
+
from ._raw import NetSuiteRawRead, ProviderAPI, QuickBooksRawRead, SuiteQLResult, _NetSuiteRaw, _QuickBooksRaw
|
|
8
|
+
from ._transport import Transport
|
|
9
|
+
from .models import NetSuiteConnection, QuickBooksConnection
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class QuickBooksRaw(_QuickBooksRaw):
|
|
13
|
+
def query(self, target: QuickBooksConnection, *, qbxml: str) -> QuickBooksRawRead:
|
|
14
|
+
return self._request("queryRaw", target, qbxml=qbxml)
|
|
15
|
+
|
|
16
|
+
def read(self, target: QuickBooksConnection, *, qbxml: str) -> QuickBooksRawRead:
|
|
17
|
+
return self._request("readRaw", target, qbxml=qbxml)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class AsyncQuickBooksRaw:
|
|
21
|
+
def __init__(self, sync: QuickBooksRaw) -> None:
|
|
22
|
+
self._sync = sync
|
|
23
|
+
|
|
24
|
+
async def query(self, target: QuickBooksConnection, *, qbxml: str) -> QuickBooksRawRead:
|
|
25
|
+
return await asyncio.to_thread(self._sync.query, target, qbxml=qbxml)
|
|
26
|
+
|
|
27
|
+
async def read(self, target: QuickBooksConnection, *, qbxml: str) -> QuickBooksRawRead:
|
|
28
|
+
return await asyncio.to_thread(self._sync.read, target, qbxml=qbxml)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class NetSuiteRaw(_NetSuiteRaw):
|
|
32
|
+
def suiteql(self, target: NetSuiteConnection, *, q: str, limit: int = 10, offset: int = 0) -> SuiteQLResult:
|
|
33
|
+
return self._page("queryRaw", target, q=q, limit=limit, offset=offset)
|
|
34
|
+
|
|
35
|
+
def read(self, target: NetSuiteConnection, *, method: Literal["GET"], path: str) -> NetSuiteRawRead:
|
|
36
|
+
return self._record("readRaw", target, method=method, path=path)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class AsyncNetSuiteRaw:
|
|
40
|
+
def __init__(self, sync: NetSuiteRaw) -> None:
|
|
41
|
+
self._sync = sync
|
|
42
|
+
|
|
43
|
+
async def suiteql(self, target: NetSuiteConnection, *, q: str, limit: int = 10, offset: int = 0) -> SuiteQLResult:
|
|
44
|
+
return await asyncio.to_thread(self._sync.suiteql, target, q=q, limit=limit, offset=offset)
|
|
45
|
+
|
|
46
|
+
async def read(self, target: NetSuiteConnection, *, method: Literal["GET"], path: str) -> NetSuiteRawRead:
|
|
47
|
+
return await asyncio.to_thread(self._sync.read, target, method=method, path=path)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class _RawProviders:
|
|
51
|
+
def __init__(self, transport: Transport) -> None:
|
|
52
|
+
self.quickbooks_desktop = ProviderAPI(QuickBooksRaw(transport))
|
|
53
|
+
self.netsuite = ProviderAPI(NetSuiteRaw(transport))
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class _AsyncRawProviders:
|
|
57
|
+
def __init__(self, sync: _RawProviders) -> None:
|
|
58
|
+
self.quickbooks_desktop = ProviderAPI(AsyncQuickBooksRaw(sync.quickbooks_desktop.raw))
|
|
59
|
+
self.netsuite = ProviderAPI(AsyncNetSuiteRaw(sync.netsuite.raw))
|
rowbind/_invoice_sync.py
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"""Typed observations from one completed invoice sync window."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import asyncio
|
|
5
|
+
import math
|
|
6
|
+
import re
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from typing import ClassVar, Literal, Optional, cast
|
|
9
|
+
|
|
10
|
+
from ._transport import Transport
|
|
11
|
+
from .models import InvoiceLineRecord, _array, _date_time, _invalid, _object, _optional_string, _string
|
|
12
|
+
|
|
13
|
+
SyncState = Literal["initializing", "syncing", "current", "unavailable"]
|
|
14
|
+
SyncFailure = Literal["source_offline", "wrong_book", "permission_denied", "book_diverged", "storage_error", "source_history_gap"]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True)
|
|
18
|
+
class InvoiceSyncStatus:
|
|
19
|
+
connection_id: str
|
|
20
|
+
status: SyncState
|
|
21
|
+
covered_through: Optional[str]
|
|
22
|
+
last_attempt_at: Optional[str]
|
|
23
|
+
last_failure: Optional[SyncFailure]
|
|
24
|
+
resource: ClassVar[Literal["invoices"]] = "invoices"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(frozen=True)
|
|
28
|
+
class SyncedInvoiceLine(InvoiceLineRecord):
|
|
29
|
+
description: Optional[str]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True)
|
|
33
|
+
class SyncedInvoiceDetails:
|
|
34
|
+
customer_id: Optional[str]
|
|
35
|
+
external_reference: Optional[str]
|
|
36
|
+
invoice_number: Optional[str]
|
|
37
|
+
issue_date: Optional[str]
|
|
38
|
+
due_date: Optional[str]
|
|
39
|
+
total: Optional[str]
|
|
40
|
+
subsidiary_id: Optional[str]
|
|
41
|
+
department_id: Optional[str]
|
|
42
|
+
lines: Optional[tuple[SyncedInvoiceLine, ...]]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass(frozen=True)
|
|
46
|
+
class SyncedInvoice:
|
|
47
|
+
invoice_id: str
|
|
48
|
+
revision: int
|
|
49
|
+
currency: str
|
|
50
|
+
balance_due: str
|
|
51
|
+
status: Optional[Literal["voided"]]
|
|
52
|
+
details: Optional[SyncedInvoiceDetails]
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass(frozen=True)
|
|
56
|
+
class SyncedInvoicePage:
|
|
57
|
+
connection_id: str
|
|
58
|
+
snapshot_revision: int
|
|
59
|
+
covered_through: str
|
|
60
|
+
invoices: tuple[SyncedInvoice, ...]
|
|
61
|
+
next_cursor: Optional[str]
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _nullable_time(data: dict[str, object], key: str) -> Optional[str]:
|
|
65
|
+
if key not in data:
|
|
66
|
+
raise _invalid(key)
|
|
67
|
+
return None if data[key] is None else _date_time(data, key)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _status(value: object, connection_id: str) -> InvoiceSyncStatus:
|
|
71
|
+
data = _object(value)
|
|
72
|
+
if data.get("connectionId") != connection_id or data.get("resource") != "invoices":
|
|
73
|
+
raise _invalid("invoice sync target")
|
|
74
|
+
status = _string(data, "status")
|
|
75
|
+
if status not in ("initializing", "syncing", "current", "unavailable"):
|
|
76
|
+
raise _invalid("invoice sync status")
|
|
77
|
+
failure = data.get("lastFailure")
|
|
78
|
+
if failure not in (None, "source_offline", "wrong_book", "permission_denied", "book_diverged", "storage_error", "source_history_gap"):
|
|
79
|
+
raise _invalid("lastFailure")
|
|
80
|
+
return InvoiceSyncStatus(connection_id, cast(SyncState, status), _nullable_time(data, "coveredThrough"),
|
|
81
|
+
_nullable_time(data, "lastAttemptAt"), cast(Optional[SyncFailure], failure))
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _details(value: object) -> SyncedInvoiceDetails:
|
|
85
|
+
data = _object(value)
|
|
86
|
+
lines = None
|
|
87
|
+
if "lines" in data:
|
|
88
|
+
parsed = []
|
|
89
|
+
for value in _array(data["lines"]):
|
|
90
|
+
line = _object(value)
|
|
91
|
+
quantity = line.get("quantity")
|
|
92
|
+
if isinstance(quantity, bool) or not isinstance(quantity, (int, float)) or (isinstance(quantity, float) and not math.isfinite(quantity)):
|
|
93
|
+
raise _invalid("invoice line quantity")
|
|
94
|
+
description = line.get("description")
|
|
95
|
+
if "description" in line and not isinstance(description, str):
|
|
96
|
+
raise _invalid("invoice line description")
|
|
97
|
+
parsed.append(SyncedInvoiceLine(_string(line, "itemId"), quantity, _string(line, "unitPrice"),
|
|
98
|
+
cast(Optional[str], description)))
|
|
99
|
+
lines = tuple(parsed)
|
|
100
|
+
return SyncedInvoiceDetails(_optional_string(data, "customerId"), _optional_string(data, "externalReference"),
|
|
101
|
+
_optional_string(data, "invoiceNumber"), _optional_string(data, "issueDate"),
|
|
102
|
+
_optional_string(data, "dueDate"), _optional_string(data, "total"),
|
|
103
|
+
_optional_string(data, "subsidiaryId"), _optional_string(data, "departmentId"), lines)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _revision(data: dict[str, object], key: str, minimum: int) -> int:
|
|
107
|
+
value = data.get(key)
|
|
108
|
+
if isinstance(value, bool) or not isinstance(value, int) or value < minimum:
|
|
109
|
+
raise _invalid(key)
|
|
110
|
+
return value
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _invoice(value: object, snapshot_revision: int) -> SyncedInvoice:
|
|
114
|
+
data = _object(value)
|
|
115
|
+
revision = _revision(data, "revision", 1)
|
|
116
|
+
if revision > snapshot_revision:
|
|
117
|
+
raise _invalid("invoice revision beyond the snapshot")
|
|
118
|
+
balance = _string(data, "balanceDue")
|
|
119
|
+
if not re.fullmatch(r"(0|[1-9][0-9]*)\.[0-9]{2}", balance):
|
|
120
|
+
raise _invalid("balanceDue")
|
|
121
|
+
if "status" in data and data["status"] != "voided":
|
|
122
|
+
raise _invalid("invoice status")
|
|
123
|
+
return SyncedInvoice(_string(data, "invoiceId"), revision, _string(data, "currency"), balance,
|
|
124
|
+
"voided" if "status" in data else None, _details(data["details"]) if "details" in data else None)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def invoice_page(value: object, connection_id: str) -> SyncedInvoicePage:
|
|
128
|
+
data = _object(value)
|
|
129
|
+
if data.get("connectionId") != connection_id or "nextCursor" not in data:
|
|
130
|
+
raise _invalid("invoice page target or cursor")
|
|
131
|
+
revision = _revision(data, "snapshotRevision", 0)
|
|
132
|
+
cursor = None if data["nextCursor"] is None else _string(data, "nextCursor")
|
|
133
|
+
return SyncedInvoicePage(connection_id, revision, _date_time(data, "coveredThrough"),
|
|
134
|
+
tuple(_invoice(row, revision) for row in _array(data.get("invoices"))), cursor)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class InvoiceSync:
|
|
138
|
+
def __init__(self, transport: Transport) -> None:
|
|
139
|
+
self._transport = transport
|
|
140
|
+
|
|
141
|
+
def enable(self, connection_id: str) -> InvoiceSyncStatus:
|
|
142
|
+
return _status(self._transport.request("enableInvoiceSync", path={"id": connection_id}), connection_id)
|
|
143
|
+
|
|
144
|
+
def status(self, connection_id: str) -> InvoiceSyncStatus:
|
|
145
|
+
return _status(self._transport.request("retrieveInvoiceSync", path={"id": connection_id}), connection_id)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
class Sync:
|
|
149
|
+
def __init__(self, transport: Transport) -> None:
|
|
150
|
+
self.invoices = InvoiceSync(transport)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
class AsyncInvoiceSync:
|
|
154
|
+
def __init__(self, sync: InvoiceSync) -> None:
|
|
155
|
+
self._sync = sync
|
|
156
|
+
|
|
157
|
+
async def enable(self, connection_id: str) -> InvoiceSyncStatus:
|
|
158
|
+
return await asyncio.to_thread(self._sync.enable, connection_id)
|
|
159
|
+
|
|
160
|
+
async def status(self, connection_id: str) -> InvoiceSyncStatus:
|
|
161
|
+
return await asyncio.to_thread(self._sync.status, connection_id)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class AsyncSync:
|
|
165
|
+
def __init__(self, sync: Sync) -> None:
|
|
166
|
+
self.invoices = AsyncInvoiceSync(sync.invoices)
|
rowbind/_raw.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""Provider-specific reads against an authorized sandbox book."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import cast, ClassVar, Generic, Literal, Optional, TypedDict, TypeVar
|
|
6
|
+
|
|
7
|
+
from ._transport import RowbindError, Transport
|
|
8
|
+
from .models import NetSuiteConnection, QuickBooksConnection
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(frozen=True)
|
|
12
|
+
class QuickBooksRawRead:
|
|
13
|
+
qbxml: str
|
|
14
|
+
request_id: Optional[str]
|
|
15
|
+
provider: ClassVar[Literal["quickbooksDesktop"]] = "quickbooksDesktop"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class NetSuiteRawRead:
|
|
20
|
+
record: dict[str, object]
|
|
21
|
+
request_id: Optional[str]
|
|
22
|
+
provider: ClassVar[Literal["netsuite"]] = "netsuite"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class _SuiteQLPageFields(TypedDict):
|
|
26
|
+
items: list[dict[str, object]]
|
|
27
|
+
hasMore: bool
|
|
28
|
+
count: int
|
|
29
|
+
offset: int
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class SuiteQLPage(_SuiteQLPageFields, total=False):
|
|
33
|
+
totalResults: int
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True)
|
|
37
|
+
class SuiteQLResult:
|
|
38
|
+
page: SuiteQLPage
|
|
39
|
+
request_id: Optional[str]
|
|
40
|
+
provider: ClassVar[Literal["netsuite"]] = "netsuite"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class _QuickBooksRaw:
|
|
44
|
+
def __init__(self, transport: Transport) -> None:
|
|
45
|
+
self._transport = transport
|
|
46
|
+
|
|
47
|
+
def _request(self, operation: Literal["readRaw", "queryRaw"], target: QuickBooksConnection, qbxml: str) -> QuickBooksRawRead:
|
|
48
|
+
if not isinstance(target, QuickBooksConnection):
|
|
49
|
+
raise RowbindError("provider_mismatch", "Select a QuickBooks Desktop connection for this request.")
|
|
50
|
+
if not isinstance(qbxml, str) or not qbxml.strip() or len(qbxml) > 4096:
|
|
51
|
+
raise RowbindError("invalid_input", "Provide a qbXML query of up to 4096 characters.")
|
|
52
|
+
data, request_id = self._transport.request(operation, path={"id": target.id}, include_request_id=True,
|
|
53
|
+
body={"provider": "quickbooksDesktop", "qbxml": qbxml})
|
|
54
|
+
fragment = data.get("qbxml")
|
|
55
|
+
if data.get("provider") != "quickbooksDesktop" or not isinstance(fragment, str) or not fragment:
|
|
56
|
+
raise RowbindError("invalid_response", "Expected a QuickBooks Desktop raw response.", request_id=request_id)
|
|
57
|
+
return QuickBooksRawRead(fragment, request_id)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class _NetSuiteRaw:
|
|
61
|
+
def __init__(self, transport: Transport) -> None:
|
|
62
|
+
self._transport = transport
|
|
63
|
+
|
|
64
|
+
def _record(self, operation: Literal["readRaw"], target: NetSuiteConnection, *, method: Literal["GET"], path: str) -> NetSuiteRawRead:
|
|
65
|
+
if not isinstance(target, NetSuiteConnection):
|
|
66
|
+
raise RowbindError("provider_mismatch", "Select a NetSuite connection for this request.")
|
|
67
|
+
if method != "GET" or not isinstance(path, str) or not path or len(path) > 512:
|
|
68
|
+
raise RowbindError("invalid_input", "Provide GET and a NetSuite record path of up to 512 characters.")
|
|
69
|
+
data, request_id = self._transport.request(operation, path={"id": target.id}, include_request_id=True,
|
|
70
|
+
body={"provider": "netsuite", "method": method, "path": path})
|
|
71
|
+
record = data.get("record")
|
|
72
|
+
if not isinstance(record, dict) or not isinstance(record.get("id"), str) or data.get("provider") != "netsuite":
|
|
73
|
+
raise RowbindError("invalid_response", "Expected a NetSuite raw response.", request_id=request_id)
|
|
74
|
+
return NetSuiteRawRead(record, request_id)
|
|
75
|
+
|
|
76
|
+
def _page(self, operation: Literal["queryRaw"], target: NetSuiteConnection, *, q: str, limit: int, offset: int) -> SuiteQLResult:
|
|
77
|
+
if not isinstance(target, NetSuiteConnection):
|
|
78
|
+
raise RowbindError("provider_mismatch", "Select a NetSuite connection for this request.")
|
|
79
|
+
if not isinstance(q, str) or not q.strip() or len(q) > 4096:
|
|
80
|
+
raise RowbindError("invalid_input", "Provide a SuiteQL query of up to 4096 characters.")
|
|
81
|
+
if type(limit) is not int or not 1 <= limit <= 100 or type(offset) is not int or not 0 <= offset <= 1_000_000 or offset % limit:
|
|
82
|
+
raise RowbindError("invalid_input", "Provide a limit of 1–100 and an offset of 0–1000000 divisible by the limit.")
|
|
83
|
+
data, request_id = self._transport.request(operation, path={"id": target.id}, include_request_id=True,
|
|
84
|
+
body={"provider": "netsuite", "q": q, "limit": limit, "offset": offset})
|
|
85
|
+
page = data.get("page")
|
|
86
|
+
if data.get("provider") != "netsuite" or not isinstance(page, dict):
|
|
87
|
+
raise RowbindError("invalid_response", "Expected a NetSuite query result.", request_id=request_id)
|
|
88
|
+
rows, count = page.get("items"), page.get("count")
|
|
89
|
+
if (not isinstance(rows, list) or not all(isinstance(row, dict) for row in rows)
|
|
90
|
+
or type(count) is not int or count != len(rows) or count > limit
|
|
91
|
+
or type(page.get("offset")) is not int or page["offset"] != offset
|
|
92
|
+
or not isinstance(page.get("hasMore"), bool)
|
|
93
|
+
or ("totalResults" in page and (type(page["totalResults"]) is not int or page["totalResults"] < 0))):
|
|
94
|
+
raise RowbindError("invalid_response", "Rowbind returned an invalid SuiteQL page.", request_id=request_id)
|
|
95
|
+
return SuiteQLResult(cast(SuiteQLPage, page), request_id)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
_Reader = TypeVar("_Reader")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@dataclass(frozen=True)
|
|
102
|
+
class ProviderAPI(Generic[_Reader]):
|
|
103
|
+
raw: _Reader
|