definite-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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 luabase
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,21 @@
1
+ Metadata-Version: 2.1
2
+ Name: definite-sdk
3
+ Version: 0.1.0
4
+ Summary: Definite SDK for Python
5
+ License: MIT
6
+ Author: Definite
7
+ Author-email: hello@definite.app
8
+ Requires-Python: >=3.9,<4.0
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.9
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Requires-Dist: requests (>=2.31.0,<3.0.0)
16
+ Description-Content-Type: text/markdown
17
+
18
+ # Definite SDK
19
+
20
+ A Python client for interacting with the Definite API, providing a convenient interface for key-value store operations.
21
+
@@ -0,0 +1,3 @@
1
+ # Definite SDK
2
+
3
+ A Python client for interacting with the Definite API, providing a convenient interface for key-value store operations.
@@ -0,0 +1,23 @@
1
+ from store import DefiniteKVStore
2
+
3
+ API_URL = "https://api.definite.app"
4
+
5
+
6
+ class DefiniteClient:
7
+ """Client for interacting with the Definite API."""
8
+
9
+ def __init__(self, api_key: str, api_url: str = API_URL):
10
+ """Creates a definite client with the provided API key.
11
+
12
+ See: https://docs.definite.app/definite-api for how to obtain an API key.
13
+ """
14
+ self.api_key = api_key
15
+ self.api_url = api_url
16
+
17
+ def get_kv_store(self, name: str) -> DefiniteKVStore:
18
+ """Initializes a key-value store with the provided name.
19
+
20
+ See DefiniteKVStore for more how to interact with the store.
21
+ """
22
+
23
+ return DefiniteKVStore(name, self.api_key, self.api_url)
@@ -0,0 +1,193 @@
1
+ from typing import Iterator
2
+
3
+ import requests
4
+
5
+ STORE_ENDPOINT = "/v1/store"
6
+
7
+
8
+ class DefiniteKVStore:
9
+ """
10
+ A key-value store hosted by Definite.
11
+
12
+ Initialization:
13
+ >>> client = DefiniteSdkClient("MY_API_KEY")
14
+ >>> store = client.get_kv_store("my_store")
15
+
16
+ Accessing values:
17
+ >>> print(store["key"])
18
+
19
+ Setting values:
20
+ >>> store["key"] = "value"
21
+ >>> store.commit()
22
+
23
+ To permanently delete "my_store":
24
+ >>> store.delete()
25
+
26
+ You MUST call commit() to save changes to the store.
27
+
28
+ The store uses versioning to prevent conflicts/stomping. If the store has
29
+ been modified since you last loaded it, the commit will fail.
30
+ """
31
+
32
+ def __init__(self, name: str, api_key: str, api_url: str):
33
+ """
34
+ Initializes the DefiniteKVStore with the provided name and API key.
35
+
36
+ Args:
37
+ name (str): The name of the key-value store.
38
+ api_key (str): The API key for authorization.
39
+
40
+ Raises:
41
+ Exception: If the store fails to load.
42
+ """
43
+
44
+ self._api_key = api_key
45
+ self._name = name
46
+ self._store_url = api_url + STORE_ENDPOINT
47
+ response = requests.get(
48
+ self._store_url,
49
+ json={"name": self._name},
50
+ headers={"Authorization": "Bearer " + self._api_key},
51
+ )
52
+
53
+ if response.status_code == 404:
54
+ # Store not found. Create a new one.
55
+ self._data = {}
56
+ self._version_id = None
57
+ elif response.status_code == 200:
58
+ # Store found. Load the data.
59
+ response_json = response.json()
60
+ self._data = response_json["data"]
61
+ self._version_id = response_json["version_id"]
62
+ else:
63
+ raise Exception("Failed to load the store: " + response.text)
64
+
65
+ def commit(self):
66
+ """
67
+ Commits the current state of the store to the remote server.
68
+
69
+ Raises:
70
+ Exception: If the commit fails.
71
+
72
+ Example:
73
+ store.commit()
74
+ """
75
+ response = requests.post(
76
+ self._store_url,
77
+ json={
78
+ "name": self._name,
79
+ "data": self._data,
80
+ "existing_version_id": self._version_id,
81
+ },
82
+ headers={"Authorization": "Bearer " + self._api_key},
83
+ )
84
+
85
+ if response.status_code != 200:
86
+ raise Exception("Failed to commit the DefiniteKVStore: " + response.text)
87
+ else:
88
+ response_json = response.json()
89
+ self._version_id = response_json.get("version_id")
90
+
91
+ def delete(self):
92
+ """
93
+ Deletes the store from the remote server.
94
+
95
+ Raises:
96
+ Exception: If the delete fails.
97
+
98
+ Example:
99
+ store.delete()
100
+ """
101
+ response = requests.delete(
102
+ self._store_url,
103
+ json={"name": self._name},
104
+ headers={"Authorization": "Bearer " + self._api_key},
105
+ )
106
+
107
+ if response.status_code != 200:
108
+ raise Exception("Failed to delete the DefiniteKVStore: " + response.text)
109
+
110
+ self._data = {}
111
+ self._version_id = None
112
+
113
+ def __getitem__(self, key: str) -> str | None:
114
+ """
115
+ Gets the value for a given key.
116
+
117
+ Args:
118
+ key (str): The key to retrieve the value for.
119
+
120
+ Returns:
121
+ The value associated with the key, or None if the key does not exist.
122
+
123
+ Example:
124
+ value = store["key1"]
125
+ """
126
+ return self._data.get(key)
127
+
128
+ def __setitem__(self, key: str, value: str):
129
+ """
130
+ Sets a key-value pair in the store.
131
+
132
+ Args:
133
+ key (str): The key to set.
134
+ value (str): The value to set.
135
+
136
+ Raises:
137
+ AssertionError: If key or value is not a string.
138
+
139
+ Example:
140
+ store["key1"] = "value1"
141
+ """
142
+ assert isinstance(key, str)
143
+ assert isinstance(value, str)
144
+ self._data[key] = value
145
+
146
+ def __delitem__(self, key: str) -> None:
147
+ """
148
+ Deletes a key-value pair from the store.
149
+
150
+ Args:
151
+ key (str): The key to delete.
152
+
153
+ Example:
154
+ del store["key1"]
155
+ """
156
+ del self._data[key]
157
+
158
+ def __iter__(self) -> Iterator[str]:
159
+ """
160
+ Returns an iterator over the keys in the store.
161
+
162
+ Returns:
163
+ An iterator over the keys in the store.
164
+
165
+ Example:
166
+ for key in store:
167
+ print(key)
168
+ """
169
+ return iter(self._data)
170
+
171
+ def __len__(self) -> int:
172
+ """
173
+ Returns the number of key-value pairs in the store.
174
+
175
+ Returns:
176
+ The number of key-value pairs in the store.
177
+
178
+ Example:
179
+ length = len(store)
180
+ """
181
+ return len(self._data)
182
+
183
+ def __repr__(self) -> str:
184
+ """
185
+ Returns a string representation of the store, similar to a dictionary.
186
+
187
+ Returns:
188
+ A string representation of the store.
189
+
190
+ Example:
191
+ print(store)
192
+ """
193
+ return repr(self._data)
@@ -0,0 +1,26 @@
1
+ [tool.poetry]
2
+ name = "definite-sdk"
3
+ version = "0.1.0"
4
+ description = "Definite SDK for Python"
5
+ authors = ["Definite <hello@definite.app>"]
6
+ license = "MIT"
7
+ readme = "README.md"
8
+
9
+ [tool.poetry.dependencies]
10
+ python = "^3.9"
11
+ requests = "^2.31.0"
12
+
13
+
14
+ [tool.poetry.group.dev.dependencies]
15
+ black = "^24.4.2"
16
+ flake8 = "^7.0.0"
17
+ mypy = "^1.10.0"
18
+ types-requests = "^2.31.0.20240406"
19
+ pytest = "^8.2.0"
20
+
21
+ [build-system]
22
+ requires = ["poetry-core"]
23
+ build-backend = "poetry.core.masonry.api"
24
+
25
+ [tool.isort]
26
+ profile = "black"