stackshift 1.3.1__py3-none-any.whl → 1.7.0__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.
- stackshift/__init__.py +48 -0
- stackshift/agents.py +98 -0
- stackshift/client.py +30 -2
- stackshift/databases.py +226 -0
- stackshift/hosted_agents.py +78 -0
- stackshift/mail.py +6 -0
- stackshift/mail_agent_inboxes.py +199 -0
- stackshift/mail_agreements.py +158 -0
- stackshift/mail_events_webhooks.py +5 -1
- stackshift/mail_inbox_webhooks.py +89 -0
- stackshift/mail_team_inboxes.py +148 -0
- stackshift/partner.py +159 -1
- {stackshift-1.3.1.dist-info → stackshift-1.7.0.dist-info}/METADATA +162 -2
- {stackshift-1.3.1.dist-info → stackshift-1.7.0.dist-info}/RECORD +16 -9
- {stackshift-1.3.1.dist-info → stackshift-1.7.0.dist-info}/WHEEL +0 -0
- {stackshift-1.3.1.dist-info → stackshift-1.7.0.dist-info}/top_level.txt +0 -0
stackshift/__init__.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
from .client import StackShift
|
|
2
|
+
from .agents import AgentConnectionOperationsClient, AgentConnectionsClient, AgentsClient
|
|
2
3
|
from .ai_credits import AICreditsClient
|
|
3
4
|
from .assets import ASSET_TRANSFORM_PRESETS, AssetsClient, asset_transform_options, asset_transform_spec, get_asset_transform_preset
|
|
5
|
+
from .databases import DatabaseBranchesClient, DatabaseOperationFailed, DatabasesClient
|
|
6
|
+
from .hosted_agents import HostedAgentBudgetClient, HostedAgentSecretsClient, HostedAgentsClient
|
|
4
7
|
from .mail import (
|
|
5
8
|
MailClient,
|
|
6
9
|
MailDomainsClient,
|
|
@@ -13,6 +16,7 @@ from .mail_events_webhooks import MailEventsClient, MailWebhooksClient, verify_w
|
|
|
13
16
|
from .mail_streams import MailStreamsClient
|
|
14
17
|
from .mail_campaigns import MailAudiencesClient, MailCampaignsClient
|
|
15
18
|
from .mail_exports import MailExportsClient
|
|
19
|
+
from .mail_agreements import AgreementLimitsClient, AgreementsClient
|
|
16
20
|
from .mail_mailboxes import (
|
|
17
21
|
MailboxAliasesClient,
|
|
18
22
|
MailboxDomainsClient,
|
|
@@ -21,6 +25,24 @@ from .mail_mailboxes import (
|
|
|
21
25
|
MailboxesClient,
|
|
22
26
|
MailboxRoutesClient,
|
|
23
27
|
)
|
|
28
|
+
from .mail_agent_inboxes import AgentInboxesClient, AgentInboxMessagesClient
|
|
29
|
+
from .mail_team_inboxes import (
|
|
30
|
+
TeamInboxActivityClient,
|
|
31
|
+
TeamInboxConversationsClient,
|
|
32
|
+
TeamInboxesClient,
|
|
33
|
+
TeamInboxPendingRepliesClient,
|
|
34
|
+
TeamInboxSettingsClient,
|
|
35
|
+
)
|
|
36
|
+
from .mail_inbox_webhooks import (
|
|
37
|
+
INBOX_AGENT_INBOX_EVENT_TYPES,
|
|
38
|
+
INBOX_CONVERSATION_EVENT_TYPES,
|
|
39
|
+
INBOX_SENDING_EVENT_TYPES,
|
|
40
|
+
InboxAgentInboxEvent,
|
|
41
|
+
InboxConversationEvent,
|
|
42
|
+
InboxSendingEvent,
|
|
43
|
+
InboxWebhookEvent,
|
|
44
|
+
verify_inbox_webhook,
|
|
45
|
+
)
|
|
24
46
|
from .partner import PartnerClient
|
|
25
47
|
from .projects import ProjectsClient
|
|
26
48
|
from .workload_security import WorkloadSecurityClient
|
|
@@ -28,8 +50,17 @@ from .workload_security import WorkloadSecurityClient
|
|
|
28
50
|
__all__ = [
|
|
29
51
|
"StackShift",
|
|
30
52
|
"AICreditsClient",
|
|
53
|
+
"AgentConnectionOperationsClient",
|
|
54
|
+
"AgentConnectionsClient",
|
|
55
|
+
"AgentsClient",
|
|
31
56
|
"ASSET_TRANSFORM_PRESETS",
|
|
32
57
|
"AssetsClient",
|
|
58
|
+
"DatabaseBranchesClient",
|
|
59
|
+
"DatabaseOperationFailed",
|
|
60
|
+
"DatabasesClient",
|
|
61
|
+
"HostedAgentBudgetClient",
|
|
62
|
+
"HostedAgentSecretsClient",
|
|
63
|
+
"HostedAgentsClient",
|
|
33
64
|
"MailClient",
|
|
34
65
|
"MailDomainsClient",
|
|
35
66
|
"MailEventsClient",
|
|
@@ -44,12 +75,29 @@ __all__ = [
|
|
|
44
75
|
"MailAudiencesClient",
|
|
45
76
|
"MailCampaignsClient",
|
|
46
77
|
"MailExportsClient",
|
|
78
|
+
"AgreementLimitsClient",
|
|
79
|
+
"AgreementsClient",
|
|
47
80
|
"MailboxAliasesClient",
|
|
48
81
|
"MailboxDomainsClient",
|
|
49
82
|
"MailboxGroupsClient",
|
|
50
83
|
"MailboxImportsClient",
|
|
51
84
|
"MailboxesClient",
|
|
52
85
|
"MailboxRoutesClient",
|
|
86
|
+
"AgentInboxesClient",
|
|
87
|
+
"AgentInboxMessagesClient",
|
|
88
|
+
"TeamInboxActivityClient",
|
|
89
|
+
"TeamInboxConversationsClient",
|
|
90
|
+
"TeamInboxesClient",
|
|
91
|
+
"TeamInboxPendingRepliesClient",
|
|
92
|
+
"TeamInboxSettingsClient",
|
|
93
|
+
"INBOX_AGENT_INBOX_EVENT_TYPES",
|
|
94
|
+
"INBOX_CONVERSATION_EVENT_TYPES",
|
|
95
|
+
"INBOX_SENDING_EVENT_TYPES",
|
|
96
|
+
"InboxAgentInboxEvent",
|
|
97
|
+
"InboxConversationEvent",
|
|
98
|
+
"InboxSendingEvent",
|
|
99
|
+
"InboxWebhookEvent",
|
|
100
|
+
"verify_inbox_webhook",
|
|
53
101
|
"PartnerClient",
|
|
54
102
|
"ProjectsClient",
|
|
55
103
|
"WorkloadSecurityClient",
|
stackshift/agents.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
from urllib.parse import quote
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _connection_data(payload: Any) -> Any:
|
|
8
|
+
if isinstance(payload, dict) and "data" in payload:
|
|
9
|
+
return payload["data"]
|
|
10
|
+
return payload
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _query(**values: Any) -> dict[str, Any] | None:
|
|
14
|
+
present = {key: value for key, value in values.items() if value is not None}
|
|
15
|
+
return present or None
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class AgentConnectionOperationsClient:
|
|
19
|
+
def __init__(self, client: Any) -> None:
|
|
20
|
+
self._client = client
|
|
21
|
+
|
|
22
|
+
def list(self, *, limit: int | None = None, cursor: str | None = None) -> dict[str, Any]:
|
|
23
|
+
return _connection_data(self._client.request("GET", "/agents/connections/operations", params=_query(limit=limit, cursor=cursor)))
|
|
24
|
+
|
|
25
|
+
def propose(
|
|
26
|
+
self,
|
|
27
|
+
*,
|
|
28
|
+
action: str,
|
|
29
|
+
configuration: dict[str, Any],
|
|
30
|
+
idempotency_key: str,
|
|
31
|
+
resource_id: str | None = None,
|
|
32
|
+
expected_resource_version: str | None = None,
|
|
33
|
+
reason: str | None = None,
|
|
34
|
+
) -> dict[str, Any]:
|
|
35
|
+
if not idempotency_key:
|
|
36
|
+
raise ValueError("Proposing an operation needs an idempotency_key. Keep it and send the same one when you retry.")
|
|
37
|
+
fields = {
|
|
38
|
+
"action": action, "resource_id": resource_id, "configuration": configuration,
|
|
39
|
+
"expected_resource_version": expected_resource_version, "reason": reason,
|
|
40
|
+
}
|
|
41
|
+
return _connection_data(self._client.request(
|
|
42
|
+
"POST", "/agents/connections/operations",
|
|
43
|
+
headers={"Idempotency-Key": idempotency_key},
|
|
44
|
+
json={key: value for key, value in fields.items() if value is not None},
|
|
45
|
+
))
|
|
46
|
+
|
|
47
|
+
def get(self, operation_id: str) -> dict[str, Any]:
|
|
48
|
+
return _connection_data(self._client.request("GET", f"/agents/connections/operations/{quote(operation_id, safe='')}"))
|
|
49
|
+
|
|
50
|
+
def wait(self, operation_id: str, *, timeout_seconds: int | None = None) -> dict[str, Any]:
|
|
51
|
+
return _connection_data(self._client.request(
|
|
52
|
+
"GET", f"/agents/connections/operations/{quote(operation_id, safe='')}/wait",
|
|
53
|
+
params=_query(timeout_seconds=timeout_seconds),
|
|
54
|
+
))
|
|
55
|
+
|
|
56
|
+
def approval(self, operation_id: str) -> dict[str, Any]:
|
|
57
|
+
return _connection_data(self._client.request("GET", f"/agents/connections/operations/{quote(operation_id, safe='')}/approval"))
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class AgentConnectionsClient:
|
|
61
|
+
def __init__(self, client: Any) -> None:
|
|
62
|
+
self._client = client
|
|
63
|
+
self.operations = AgentConnectionOperationsClient(client)
|
|
64
|
+
|
|
65
|
+
def identity(self) -> dict[str, Any]:
|
|
66
|
+
return _connection_data(self._client.request("GET", "/agents/connections/identity"))
|
|
67
|
+
|
|
68
|
+
def resources(
|
|
69
|
+
self, *, kind: str | None = None, query: str | None = None, limit: int | None = None, cursor: str | None = None,
|
|
70
|
+
) -> dict[str, Any]:
|
|
71
|
+
return _connection_data(self._client.request(
|
|
72
|
+
"GET", "/agents/connections/resources",
|
|
73
|
+
params=_query(kind=kind, query=query, limit=limit, cursor=cursor),
|
|
74
|
+
))
|
|
75
|
+
|
|
76
|
+
def capabilities(self, *, family: str | None = None) -> list[dict[str, Any]]:
|
|
77
|
+
return _connection_data(self._client.request("GET", "/agents/connections/capabilities", params=_query(family=family)))
|
|
78
|
+
|
|
79
|
+
def plan(self) -> dict[str, Any]:
|
|
80
|
+
return _connection_data(self._client.request("GET", "/agents/connections/plan"))
|
|
81
|
+
|
|
82
|
+
def task_facts(self, objective_id: str) -> dict[str, Any]:
|
|
83
|
+
return _connection_data(self._client.request("GET", "/agents/connections/task-facts", params={"objective_id": objective_id}))
|
|
84
|
+
|
|
85
|
+
def history(self, *, limit: int | None = None, cursor: str | None = None) -> dict[str, Any]:
|
|
86
|
+
return _connection_data(self._client.request("GET", "/agents/connections/history", params=_query(limit=limit, cursor=cursor)))
|
|
87
|
+
|
|
88
|
+
def lower_level(self, *, level: str, expected_version: int | None = None) -> dict[str, Any]:
|
|
89
|
+
fields = {"level": level, "expected_version": expected_version}
|
|
90
|
+
return _connection_data(self._client.request(
|
|
91
|
+
"PUT", "/agents/connections/level",
|
|
92
|
+
json={key: value for key, value in fields.items() if value is not None},
|
|
93
|
+
))
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class AgentsClient:
|
|
97
|
+
def __init__(self, client: Any) -> None:
|
|
98
|
+
self.connections = AgentConnectionsClient(client)
|
stackshift/client.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import os
|
|
4
|
+
import re
|
|
4
5
|
from typing import Any
|
|
5
6
|
|
|
6
7
|
import requests
|
|
@@ -26,16 +27,22 @@ class StackShift:
|
|
|
26
27
|
self.cdn_base_url = cdn_base_url.rstrip("/")
|
|
27
28
|
self.session = session or requests.Session()
|
|
28
29
|
|
|
30
|
+
from .agents import AgentsClient
|
|
29
31
|
from .ai_credits import AICreditsClient
|
|
30
32
|
from .assets import AssetsClient
|
|
33
|
+
from .databases import DatabasesClient
|
|
34
|
+
from .hosted_agents import HostedAgentsClient
|
|
31
35
|
from .mail import MailClient
|
|
32
36
|
from .partner import PartnerClient
|
|
33
37
|
from .projects import ProjectsClient
|
|
34
38
|
from .workload_security import WorkloadSecurityClient
|
|
35
39
|
|
|
40
|
+
self.agents = AgentsClient(self)
|
|
36
41
|
self.ai_credits = AICreditsClient(self)
|
|
37
42
|
self.projects = ProjectsClient(self)
|
|
38
43
|
self.assets = AssetsClient(self)
|
|
44
|
+
self.databases = DatabasesClient(self)
|
|
45
|
+
self.hosted_agents = HostedAgentsClient(self)
|
|
39
46
|
self.mail = MailClient(self)
|
|
40
47
|
self.partner = PartnerClient(self)
|
|
41
48
|
self.workload_security = WorkloadSecurityClient(self)
|
|
@@ -57,19 +64,40 @@ class StackShift:
|
|
|
57
64
|
error = payload.get("error") if isinstance(payload, dict) else None
|
|
58
65
|
if isinstance(error, dict):
|
|
59
66
|
raise StackShiftAPIError(
|
|
60
|
-
error.get("message") or f"StackShift API request failed with {response.status_code}",
|
|
67
|
+
error.get("message") or payload.get("message") or f"StackShift API request failed with {response.status_code}",
|
|
61
68
|
status_code=response.status_code,
|
|
62
69
|
code=error.get("code"),
|
|
63
70
|
details=error.get("details"),
|
|
64
71
|
)
|
|
65
72
|
message = payload.get("message") if isinstance(payload, dict) else None
|
|
66
|
-
raise StackShiftAPIError(
|
|
73
|
+
raise StackShiftAPIError(
|
|
74
|
+
message or f"StackShift API request failed with {response.status_code}",
|
|
75
|
+
status_code=response.status_code,
|
|
76
|
+
code=_top_level_error_code(payload),
|
|
77
|
+
details=_top_level_error_details(payload),
|
|
78
|
+
)
|
|
67
79
|
|
|
68
80
|
if isinstance(payload, dict) and "success" in payload and "data" in payload:
|
|
69
81
|
return payload["data"]
|
|
70
82
|
return payload
|
|
71
83
|
|
|
72
84
|
|
|
85
|
+
def _top_level_error_code(payload: Any) -> str | None:
|
|
86
|
+
if not isinstance(payload, dict):
|
|
87
|
+
return None
|
|
88
|
+
if isinstance(payload.get("code"), str):
|
|
89
|
+
return payload["code"]
|
|
90
|
+
error = payload.get("error")
|
|
91
|
+
return error if isinstance(error, str) and re.fullmatch(r"[a-z][a-z0-9_]*", error) else None
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _top_level_error_details(payload: Any) -> dict[str, Any] | None:
|
|
95
|
+
if not isinstance(payload, dict):
|
|
96
|
+
return None
|
|
97
|
+
details = {key: value for key, value in payload.items() if key not in ("success", "status", "status_code", "message", "error", "code")}
|
|
98
|
+
return details or None
|
|
99
|
+
|
|
100
|
+
|
|
73
101
|
class StackShiftAPIError(RuntimeError):
|
|
74
102
|
def __init__(
|
|
75
103
|
self,
|
stackshift/databases.py
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
import uuid
|
|
5
|
+
from contextlib import contextmanager
|
|
6
|
+
from typing import Any, Iterator
|
|
7
|
+
from urllib.parse import quote
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class DatabaseOperationFailed(RuntimeError):
|
|
11
|
+
def __init__(self, operation: dict[str, Any]) -> None:
|
|
12
|
+
super().__init__(operation.get("error_message") or f"The {operation.get('kind')} did not finish.")
|
|
13
|
+
self.operation = operation
|
|
14
|
+
self.code = operation.get("error_code")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _idempotency_headers(key: str | None) -> dict[str, str]:
|
|
18
|
+
return {"Idempotency-Key": key} if key else {}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class DatabaseBranchesClient:
|
|
22
|
+
def __init__(self, client: Any) -> None:
|
|
23
|
+
self._client = client
|
|
24
|
+
|
|
25
|
+
def list(self, database_id: str) -> dict[str, Any]:
|
|
26
|
+
return self._client.request("GET", f"/databases/{quote(database_id, safe='')}/branches")
|
|
27
|
+
|
|
28
|
+
def create(
|
|
29
|
+
self,
|
|
30
|
+
database_id: str,
|
|
31
|
+
*,
|
|
32
|
+
name: str,
|
|
33
|
+
from_: str | dict[str, str] | None = None,
|
|
34
|
+
ttl: str | None = None,
|
|
35
|
+
sleep_after_minutes: int | None = None,
|
|
36
|
+
created_by: str | None = None,
|
|
37
|
+
source_ref: str | None = None,
|
|
38
|
+
confirm_charge_minor: int | None = None,
|
|
39
|
+
instance_class: str | None = None,
|
|
40
|
+
idempotency_key: str | None = None,
|
|
41
|
+
) -> dict[str, Any]:
|
|
42
|
+
fields = {
|
|
43
|
+
"name": name, "from": from_, "ttl": ttl, "sleep_after_minutes": sleep_after_minutes, "created_by": created_by,
|
|
44
|
+
"source_ref": source_ref, "confirm_charge_minor": confirm_charge_minor, "instance_class": instance_class,
|
|
45
|
+
}
|
|
46
|
+
return self._client.request(
|
|
47
|
+
"POST", f"/databases/{quote(database_id, safe='')}/branches",
|
|
48
|
+
headers=_idempotency_headers(idempotency_key),
|
|
49
|
+
json={key: value for key, value in fields.items() if value is not None},
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
def options(self, database_id: str) -> dict[str, Any]:
|
|
53
|
+
return self._client.request("GET", f"/databases/{quote(database_id, safe='')}/branches/options")
|
|
54
|
+
|
|
55
|
+
def get(self, database_id: str, branch_id: str) -> dict[str, Any]:
|
|
56
|
+
return self._client.request("GET", f"/databases/{quote(database_id, safe='')}/branches/{quote(branch_id, safe='')}")
|
|
57
|
+
|
|
58
|
+
def changes(self, database_id: str, branch_id: str) -> dict[str, Any]:
|
|
59
|
+
return self._client.request("GET", f"/databases/{quote(database_id, safe='')}/branches/{quote(branch_id, safe='')}/changes")
|
|
60
|
+
|
|
61
|
+
def resume_scheduled_jobs(self, database_id: str, branch_id: str, *, ids: list[int]) -> dict[str, Any]:
|
|
62
|
+
return self._client.request(
|
|
63
|
+
"POST", f"/databases/{quote(database_id, safe='')}/branches/{quote(branch_id, safe='')}/scheduled-jobs/resume",
|
|
64
|
+
json={"ids": ids},
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
def update(
|
|
68
|
+
self,
|
|
69
|
+
database_id: str,
|
|
70
|
+
branch_id: str,
|
|
71
|
+
*,
|
|
72
|
+
name: str | None = None,
|
|
73
|
+
ttl: str | None = None,
|
|
74
|
+
sleep_after_minutes: int | None = None,
|
|
75
|
+
) -> dict[str, Any]:
|
|
76
|
+
fields = {"name": name, "ttl": ttl, "sleep_after_minutes": sleep_after_minutes}
|
|
77
|
+
return self._client.request(
|
|
78
|
+
"PATCH", f"/databases/{quote(database_id, safe='')}/branches/{quote(branch_id, safe='')}",
|
|
79
|
+
json={key: value for key, value in fields.items() if value is not None},
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
def retry(self, database_id: str, branch_id: str, *, confirm_charge_minor: int | None = None, idempotency_key: str | None = None) -> dict[str, Any]:
|
|
83
|
+
return self._client.request(
|
|
84
|
+
"POST", f"/databases/{quote(database_id, safe='')}/branches/{quote(branch_id, safe='')}/retry",
|
|
85
|
+
headers=_idempotency_headers(idempotency_key or str(uuid.uuid4())),
|
|
86
|
+
json={"confirm_charge_minor": confirm_charge_minor} if confirm_charge_minor is not None else {},
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
def delete(
|
|
90
|
+
self,
|
|
91
|
+
database_id: str,
|
|
92
|
+
branch_id: str,
|
|
93
|
+
*,
|
|
94
|
+
cascade: bool = False,
|
|
95
|
+
confirm_name: str | None = None,
|
|
96
|
+
idempotency_key: str | None = None,
|
|
97
|
+
) -> dict[str, Any]:
|
|
98
|
+
if cascade and not confirm_name:
|
|
99
|
+
raise ValueError("Deleting a Branch together with the Branches under it needs confirm_name: pass the name of the Branch.")
|
|
100
|
+
return self._client.request(
|
|
101
|
+
"DELETE", f"/databases/{quote(database_id, safe='')}/branches/{quote(branch_id, safe='')}",
|
|
102
|
+
headers=_idempotency_headers(idempotency_key or str(uuid.uuid4())),
|
|
103
|
+
params={"cascade": "true", "confirm": confirm_name} if cascade else None,
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
def reset(self, database_id: str, branch_id: str, *, idempotency_key: str | None = None) -> dict[str, Any]:
|
|
107
|
+
return self._client.request(
|
|
108
|
+
"POST", f"/databases/{quote(database_id, safe='')}/branches/{quote(branch_id, safe='')}/reset",
|
|
109
|
+
headers=_idempotency_headers(idempotency_key or str(uuid.uuid4())),
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
def promote(self, database_id: str, branch_id: str, *, confirm_name: str, idempotency_key: str | None = None) -> dict[str, Any]:
|
|
113
|
+
return self._client.request(
|
|
114
|
+
"POST", f"/databases/{quote(database_id, safe='')}/branches/{quote(branch_id, safe='')}/promote",
|
|
115
|
+
headers=_idempotency_headers(idempotency_key or str(uuid.uuid4())),
|
|
116
|
+
json={"confirm_name": confirm_name},
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
def operation(self, database_id: str, operation_id: str) -> dict[str, Any]:
|
|
120
|
+
return self._client.request("GET", f"/databases/{quote(database_id, safe='')}/branches/operations/{quote(operation_id, safe='')}")
|
|
121
|
+
|
|
122
|
+
def wait_for_operation(self, database_id: str, operation_id: str, *, timeout: float = 300.0, interval: float = 1.0) -> dict[str, Any]:
|
|
123
|
+
deadline = time.monotonic() + timeout
|
|
124
|
+
while True:
|
|
125
|
+
operation = self.operation(database_id, operation_id)
|
|
126
|
+
if operation["status"] in ("succeeded", "failed"):
|
|
127
|
+
return operation
|
|
128
|
+
if time.monotonic() >= deadline:
|
|
129
|
+
raise TimeoutError(f"The {operation['kind']} was still {operation['status']} after {timeout} seconds.")
|
|
130
|
+
time.sleep(interval)
|
|
131
|
+
|
|
132
|
+
def url(self, database_id: str, branch_id: str, *, direct: bool = False) -> str:
|
|
133
|
+
credentials = self._client.request("GET", f"/databases/{quote(branch_id, safe='')}/credentials")
|
|
134
|
+
role = "direct" if direct else "pooled"
|
|
135
|
+
matches = [endpoint for endpoint in credentials.get("endpoints") or [] if endpoint.get("role") == role]
|
|
136
|
+
endpoint = next((item for item in matches if item.get("network") == "public"), matches[0] if matches else None)
|
|
137
|
+
if endpoint:
|
|
138
|
+
return endpoint["connection_string"]
|
|
139
|
+
if direct:
|
|
140
|
+
return credentials["connection_string"]
|
|
141
|
+
raise ValueError("This database has no shared connection string. Pass direct=True to use the direct one.")
|
|
142
|
+
|
|
143
|
+
@contextmanager
|
|
144
|
+
def with_branch(
|
|
145
|
+
self,
|
|
146
|
+
database_id: str,
|
|
147
|
+
*,
|
|
148
|
+
name: str | None = None,
|
|
149
|
+
ttl: str = "24h",
|
|
150
|
+
from_: str | dict[str, str] | None = None,
|
|
151
|
+
sleep_after_minutes: int | None = None,
|
|
152
|
+
created_by: str | None = None,
|
|
153
|
+
source_ref: str | None = None,
|
|
154
|
+
direct: bool = False,
|
|
155
|
+
timeout: float = 300.0,
|
|
156
|
+
interval: float = 1.0,
|
|
157
|
+
cleanup_timeout: float = 600.0,
|
|
158
|
+
) -> Iterator[dict[str, Any]]:
|
|
159
|
+
created = self.create(
|
|
160
|
+
database_id, name=name or f"test-{uuid.uuid4().hex[:8]}", from_=from_, ttl=ttl,
|
|
161
|
+
sleep_after_minutes=sleep_after_minutes, created_by=created_by, source_ref=source_ref,
|
|
162
|
+
)
|
|
163
|
+
branch_id = created["branch_database_id"]
|
|
164
|
+
|
|
165
|
+
def remove() -> None:
|
|
166
|
+
self.wait_for_operation(database_id, created["id"], timeout=cleanup_timeout, interval=interval)
|
|
167
|
+
removal = self.delete(database_id, branch_id)
|
|
168
|
+
removed = self.wait_for_operation(database_id, removal["id"], timeout=timeout, interval=interval)
|
|
169
|
+
if removed["status"] == "failed":
|
|
170
|
+
raise DatabaseOperationFailed(removed)
|
|
171
|
+
|
|
172
|
+
try:
|
|
173
|
+
ready = self.wait_for_operation(database_id, created["id"], timeout=timeout, interval=interval)
|
|
174
|
+
if ready["status"] == "failed":
|
|
175
|
+
raise DatabaseOperationFailed(ready)
|
|
176
|
+
yield {**self.get(database_id, branch_id), "url": self.url(database_id, branch_id, direct=direct)}
|
|
177
|
+
except BaseException as error:
|
|
178
|
+
try:
|
|
179
|
+
remove()
|
|
180
|
+
except Exception as cleanup_error:
|
|
181
|
+
error.cleanup_error = cleanup_error
|
|
182
|
+
if hasattr(error, "add_note"):
|
|
183
|
+
error.add_note(f"The branch could not be deleted: {cleanup_error}")
|
|
184
|
+
raise
|
|
185
|
+
remove()
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
class DatabasesClient:
|
|
189
|
+
def __init__(self, client: Any) -> None:
|
|
190
|
+
self._client = client
|
|
191
|
+
self.branches = DatabaseBranchesClient(client)
|
|
192
|
+
|
|
193
|
+
def sleep(self, database_id: str, *, enabled: bool, after_minutes: int | None = None, allow_scheduled_jobs: bool | None = None) -> dict[str, Any]:
|
|
194
|
+
fields = {"enabled": enabled, "after_minutes": after_minutes, "allow_scheduled_jobs": allow_scheduled_jobs}
|
|
195
|
+
return self._client.request(
|
|
196
|
+
"PUT", f"/databases/{quote(database_id, safe='')}/sleep",
|
|
197
|
+
json={key: value for key, value in fields.items() if value is not None},
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
def wake(self, database_id: str, *, wait: bool = False) -> dict[str, Any]:
|
|
201
|
+
return self._client.request("POST", f"/databases/{quote(database_id, safe='')}/wake", params={"wait": "true"} if wait else None)
|
|
202
|
+
|
|
203
|
+
def scheduled_jobs(self, database_id: str) -> dict[str, Any]:
|
|
204
|
+
return self._client.request("GET", f"/databases/{quote(database_id, safe='')}/scheduled-jobs")
|
|
205
|
+
|
|
206
|
+
def wake_events(self, database_id: str, *, failed: bool = False, limit: int | None = None) -> dict[str, Any]:
|
|
207
|
+
params = {"failed": "true" if failed else None, "limit": limit}
|
|
208
|
+
return self._client.request(
|
|
209
|
+
"GET", f"/databases/{quote(database_id, safe='')}/wake-events",
|
|
210
|
+
params={key: value for key, value in params.items() if value is not None},
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
def billing(self, database_id: str) -> dict[str, Any]:
|
|
214
|
+
return self._client.request("GET", f"/databases/{quote(database_id, safe='')}/billing")
|
|
215
|
+
|
|
216
|
+
def branch_usage(self, database_id: str) -> dict[str, Any]:
|
|
217
|
+
return self._client.request("GET", f"/databases/{quote(database_id, safe='')}/branch-usage")
|
|
218
|
+
|
|
219
|
+
def pay_branch_usage(self, database_id: str) -> dict[str, Any]:
|
|
220
|
+
return self._client.request("POST", f"/databases/{quote(database_id, safe='')}/branch-usage/pay")
|
|
221
|
+
|
|
222
|
+
def move_to_instant_branches(self, database_id: str) -> dict[str, Any]:
|
|
223
|
+
return self._client.request("POST", f"/databases/{quote(database_id, safe='')}/move-to-instant-branches")
|
|
224
|
+
|
|
225
|
+
def instant_branches_move(self, database_id: str) -> dict[str, Any]:
|
|
226
|
+
return self._client.request("GET", f"/databases/{quote(database_id, safe='')}/move-to-instant-branches")
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
from urllib.parse import quote
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _path(agent_id: str) -> str:
|
|
8
|
+
return f"/agents/{quote(agent_id, safe='')}"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _query(**values: Any) -> dict[str, Any] | None:
|
|
12
|
+
present = {key: value for key, value in values.items() if value is not None}
|
|
13
|
+
return present or None
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class HostedAgentSecretsClient:
|
|
17
|
+
def __init__(self, client: Any) -> None:
|
|
18
|
+
self._client = client
|
|
19
|
+
|
|
20
|
+
def set(self, agent_id: str, name: str, value: str) -> None:
|
|
21
|
+
self._client.request("PUT", f"{_path(agent_id)}/secrets/{quote(name, safe='')}", json={"value": value})
|
|
22
|
+
|
|
23
|
+
def unset(self, agent_id: str, name: str) -> None:
|
|
24
|
+
self._client.request("DELETE", f"{_path(agent_id)}/secrets/{quote(name, safe='')}")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class HostedAgentBudgetClient:
|
|
28
|
+
def __init__(self, client: Any) -> None:
|
|
29
|
+
self._client = client
|
|
30
|
+
|
|
31
|
+
def set(self, agent_id: str, budget: dict[str, Any]) -> None:
|
|
32
|
+
self._client.request("PUT", f"{_path(agent_id)}/budget", json={"budget": budget})
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class HostedAgentsClient:
|
|
36
|
+
def __init__(self, client: Any) -> None:
|
|
37
|
+
self._client = client
|
|
38
|
+
self.secrets = HostedAgentSecretsClient(client)
|
|
39
|
+
self.budget = HostedAgentBudgetClient(client)
|
|
40
|
+
|
|
41
|
+
def list(self) -> dict[str, Any]:
|
|
42
|
+
return self._client.request("GET", "/agents/")
|
|
43
|
+
|
|
44
|
+
def get(self, agent_id: str) -> dict[str, Any]:
|
|
45
|
+
return self._client.request("GET", _path(agent_id))
|
|
46
|
+
|
|
47
|
+
def create(self, request: dict[str, Any], *, idempotency_key: str) -> dict[str, Any]:
|
|
48
|
+
if not idempotency_key:
|
|
49
|
+
raise ValueError("Creating an agent needs an idempotency_key. Keep it and send the same one when you retry.")
|
|
50
|
+
return self._client.request("POST", "/agents/", headers={"Idempotency-Key": idempotency_key}, json=request)
|
|
51
|
+
|
|
52
|
+
def deploy(self, agent_id: str, *, branch: str | None = None, commit_sha: str | None = None) -> dict[str, Any]:
|
|
53
|
+
body = {key: value for key, value in {"branch": branch, "commit_sha": commit_sha}.items() if value is not None}
|
|
54
|
+
return self._client.request("POST", f"{_path(agent_id)}/deploy", json=body)
|
|
55
|
+
|
|
56
|
+
def rollback(self, agent_id: str, version: int) -> dict[str, Any]:
|
|
57
|
+
return self._client.request("POST", f"{_path(agent_id)}/rollback", json={"version": version})
|
|
58
|
+
|
|
59
|
+
def stop(self, agent_id: str, reason: str | None = None) -> dict[str, Any]:
|
|
60
|
+
return self._client.request("POST", f"{_path(agent_id)}/stop", json={} if reason is None else {"reason": reason})
|
|
61
|
+
|
|
62
|
+
def start(self, agent_id: str) -> dict[str, Any]:
|
|
63
|
+
return self._client.request("POST", f"{_path(agent_id)}/start")
|
|
64
|
+
|
|
65
|
+
def delete(self, agent_id: str) -> None:
|
|
66
|
+
self._client.request("DELETE", _path(agent_id))
|
|
67
|
+
|
|
68
|
+
def runs(self, agent_id: str, *, limit: int | None = None, cursor: str | None = None) -> dict[str, Any]:
|
|
69
|
+
return self._client.request("GET", f"{_path(agent_id)}/runs", params=_query(limit=limit, cursor=cursor))
|
|
70
|
+
|
|
71
|
+
def run(self, agent_id: str, run_id: str) -> dict[str, Any]:
|
|
72
|
+
return self._client.request("GET", f"{_path(agent_id)}/runs/{quote(run_id, safe='')}")
|
|
73
|
+
|
|
74
|
+
def spend(self, agent_id: str, *, days: int | None = None) -> dict[str, Any]:
|
|
75
|
+
return self._client.request("GET", f"{_path(agent_id)}/spend", params=_query(days=days))
|
|
76
|
+
|
|
77
|
+
def logs(self, agent_id: str, *, offset: int | None = None) -> dict[str, Any]:
|
|
78
|
+
return self._client.request("GET", f"{_path(agent_id)}/logs", params=_query(offset=offset))
|
stackshift/mail.py
CHANGED
|
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|
|
3
3
|
from typing import Any
|
|
4
4
|
from urllib.parse import quote
|
|
5
5
|
|
|
6
|
+
from .mail_agreements import AgreementsClient
|
|
6
7
|
from .mail_advanced import MailAnalyticsClient, MailBatchClient, MailInboundClient, MailScheduledClient
|
|
7
8
|
from .mail_events_webhooks import MailEventsClient, MailWebhooksClient
|
|
8
9
|
from .mail_reputation import MailLimitsClient, MailReputationClient
|
|
@@ -13,6 +14,8 @@ from .mail_marketing import MailAutomationsClient, MailFormsClient, MailSegments
|
|
|
13
14
|
from .mail_migration import MailMigrationClient
|
|
14
15
|
from .mail_campaigns import MailAudiencesClient, MailCampaignsClient
|
|
15
16
|
from .mail_exports import MailExportsClient
|
|
17
|
+
from .mail_agent_inboxes import AgentInboxesClient
|
|
18
|
+
from .mail_team_inboxes import TeamInboxesClient
|
|
16
19
|
from .mail_mailboxes import (
|
|
17
20
|
MailboxAliasesClient,
|
|
18
21
|
MailboxDomainsClient,
|
|
@@ -52,12 +55,15 @@ class MailClient:
|
|
|
52
55
|
self.groups = MailboxGroupsClient(client)
|
|
53
56
|
self.routes = MailboxRoutesClient(client)
|
|
54
57
|
self.mailbox_imports = MailboxImportsClient(client)
|
|
58
|
+
self.agent_inboxes = AgentInboxesClient(client)
|
|
59
|
+
self.team_inboxes = TeamInboxesClient(client)
|
|
55
60
|
self.limits = MailLimitsClient(client)
|
|
56
61
|
self.reputation = MailReputationClient(client)
|
|
57
62
|
self.scheduled = MailScheduledClient(client)
|
|
58
63
|
self.batch = MailBatchClient(client)
|
|
59
64
|
self.inbound = MailInboundClient(client)
|
|
60
65
|
self.analytics = MailAnalyticsClient(client)
|
|
66
|
+
self.agreements = AgreementsClient(client)
|
|
61
67
|
|
|
62
68
|
def send(
|
|
63
69
|
self,
|