git-dibs-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,93 @@
1
+ name: publish
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ inputs:
6
+ repository:
7
+ description: "Where to publish the package"
8
+ required: true
9
+ default: testpypi
10
+ type: choice
11
+ options:
12
+ - testpypi
13
+ - pypi
14
+
15
+ permissions:
16
+ contents: read
17
+
18
+ jobs:
19
+ build:
20
+ runs-on: ubuntu-latest
21
+
22
+ steps:
23
+ - uses: actions/checkout@v4
24
+ with:
25
+ fetch-depth: 0
26
+
27
+ - uses: astral-sh/setup-uv@v6
28
+
29
+ - uses: actions/setup-python@v5
30
+ with:
31
+ python-version: "3.13"
32
+
33
+ - name: Build distributions
34
+ run: uv build --no-sources
35
+
36
+ - name: Upload distributions
37
+ uses: actions/upload-artifact@v4
38
+ with:
39
+ name: dist
40
+ path: dist/*
41
+
42
+ publish-testpypi:
43
+ if: github.event_name == 'workflow_dispatch' && inputs.repository == 'testpypi'
44
+ needs: build
45
+ runs-on: ubuntu-latest
46
+ environment:
47
+ name: testpypi
48
+ url: https://test.pypi.org/project/git-dibs-sdk/
49
+ permissions:
50
+ id-token: write
51
+ contents: read
52
+
53
+ steps:
54
+ - uses: actions/checkout@v4
55
+ with:
56
+ fetch-depth: 0
57
+
58
+ - uses: actions/download-artifact@v4
59
+ with:
60
+ name: dist
61
+ path: dist
62
+
63
+ - name: Publish to TestPyPI
64
+ uses: pypa/gh-action-pypi-publish@release/v1
65
+ with:
66
+ repository-url: https://test.pypi.org/legacy/
67
+ packages-dir: dist/
68
+
69
+ publish-pypi:
70
+ if: github.event_name == 'workflow_dispatch' && inputs.repository == 'pypi'
71
+ needs: build
72
+ runs-on: ubuntu-latest
73
+ environment:
74
+ name: pypi
75
+ url: https://pypi.org/project/git-dibs-sdk/
76
+ permissions:
77
+ id-token: write
78
+ contents: read
79
+
80
+ steps:
81
+ - uses: actions/checkout@v4
82
+ with:
83
+ fetch-depth: 0
84
+
85
+ - uses: actions/download-artifact@v4
86
+ with:
87
+ name: dist
88
+ path: dist
89
+
90
+ - name: Publish to PyPI
91
+ uses: pypa/gh-action-pypi-publish@release/v1
92
+ with:
93
+ packages-dir: dist/
@@ -0,0 +1,26 @@
1
+ name: test
2
+
3
+ on:
4
+ push:
5
+ pull_request:
6
+
7
+ jobs:
8
+ unittest:
9
+ runs-on: ubuntu-latest
10
+ strategy:
11
+ fail-fast: false
12
+ matrix:
13
+ python-version: ["3.10", "3.11", "3.12", "3.13"]
14
+
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+
18
+ - uses: actions/setup-python@v5
19
+ with:
20
+ python-version: ${{ matrix.python-version }}
21
+
22
+ - name: Install package
23
+ run: python -m pip install --upgrade pip && python -m pip install -e .
24
+
25
+ - name: Run tests
26
+ run: python -m unittest discover -s tests -v
@@ -0,0 +1,210 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+ #poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ #pdm.lock
116
+ #pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ #pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # SageMath parsed files
135
+ *.sage.py
136
+
137
+ # Environments
138
+ .env
139
+ .envrc
140
+ .venv
141
+ env/
142
+ venv/
143
+ ENV/
144
+ env.bak/
145
+ venv.bak/
146
+
147
+ # Spyder project settings
148
+ .spyderproject
149
+ .spyproject
150
+
151
+ # Rope project settings
152
+ .ropeproject
153
+
154
+ # mkdocs documentation
155
+ /site
156
+
157
+ # mypy
158
+ .mypy_cache/
159
+ .dmypy.json
160
+ dmypy.json
161
+
162
+ # Pyre type checker
163
+ .pyre/
164
+
165
+ # pytype static type analyzer
166
+ .pytype/
167
+
168
+ # Cython debug symbols
169
+ cython_debug/
170
+
171
+ # PyCharm
172
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
173
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
174
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
175
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
176
+ #.idea/
177
+
178
+ # Abstra
179
+ # Abstra is an AI-powered process automation framework.
180
+ # Ignore directories containing user credentials, local state, and settings.
181
+ # Learn more at https://abstra.io/docs
182
+ .abstra/
183
+
184
+ # Visual Studio Code
185
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
186
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
187
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
188
+ # you could uncomment the following to ignore the entire vscode folder
189
+ # .vscode/
190
+
191
+ # Ruff stuff:
192
+ .ruff_cache/
193
+
194
+ # PyPI configuration file
195
+ .pypirc
196
+
197
+ # Cursor
198
+ # Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
199
+ # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
200
+ # refer to https://docs.cursor.com/context/ignore-files
201
+ .cursorignore
202
+ .cursorindexingignore
203
+
204
+ # Marimo
205
+ marimo/_static/
206
+ marimo/_lsp/
207
+ __marimo__/
208
+
209
+ uv.lock
210
+ src/git_dibs_sdk/_version.py
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <https://unlicense.org>
@@ -0,0 +1,133 @@
1
+ Metadata-Version: 2.4
2
+ Name: git-dibs-sdk
3
+ Version: 0.1.0
4
+ Summary: Python SDK for reserving and querying Git Dibs commits
5
+ Project-URL: Homepage, https://gitdibs.com
6
+ Project-URL: Github, https://github.com/gieseanw/git-dibs-sdk
7
+ Project-URL: Repository, https://github.com/gieseanw/git-dibs-sdk.git
8
+ Author-email: Andy Giese <gieseanw@gmail.com>
9
+ License-Expression: Unlicense
10
+ License-File: LICENSE
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: Public Domain
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Requires-Python: >=3.10
20
+ Description-Content-Type: text/markdown
21
+
22
+ # git-dibs-sdk
23
+
24
+ If you're reading this, you're already in too deep. Yes, I really made a python sdk for the useless utility that is "Git Dibs". It actually works.
25
+
26
+
27
+ ## Install
28
+
29
+ ```powershell
30
+ python -m pip install git-dibs-sdk
31
+ ```
32
+
33
+ ## Quick Start
34
+
35
+ ```python
36
+ from git_dibs_sdk import DibsAlreadyCalledError, GitDibsClient
37
+
38
+ client = GitDibsClient()
39
+
40
+ dibs = client.lookup_commit("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
41
+ if dibs is None:
42
+ print("commit is available")
43
+ else:
44
+ print(dibs.reserved_by, dibs.upvote_count)
45
+
46
+ try:
47
+ created = client.reserve_commit(
48
+ commit_hash="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
49
+ reserved_by="bb",
50
+ )
51
+ print(created.hash)
52
+ except DibsAlreadyCalledError as error:
53
+ print(error)
54
+ ```
55
+
56
+ ## Client Surface
57
+
58
+ ```python
59
+ from git_dibs_sdk import GitDibsClient
60
+
61
+ client = GitDibsClient(
62
+ base_url="https://gitdibs.com",
63
+ timeout=10.0,
64
+ )
65
+ ```
66
+
67
+ Available methods:
68
+
69
+ - `lookup_commit(commit_hash) -> Dibs | None`
70
+ - `reserve_commit(commit_hash, reserved_by) -> Dibs`
71
+ - `list_recent_reservations() -> list[Dibs]`
72
+ - `list_popular_reservations() -> list[Dibs]`
73
+ - `search_reservations(query=None, after=None, limit=None) -> DibsSearchResult`
74
+ - `upvote_commit(commit_hash, voter_fingerprint) -> UpvoteResult`
75
+
76
+ `Dibs` fields:
77
+
78
+ - `hash`
79
+ - `reserved_at_utc`
80
+ - `reserved_by`
81
+ - `upvote_count`
82
+
83
+ `DibsSearchResult` fields:
84
+
85
+ - `dibs`
86
+ - `query`
87
+ - `after`
88
+ - `limit`
89
+ - `has_more`
90
+ - `next_after`
91
+
92
+ `UpvoteResult` fields:
93
+
94
+ - `applied`
95
+ - `upvote_count`
96
+
97
+ ## Error Handling
98
+
99
+ All transport, HTTP, and response-shape failures are raised as `GitDibsError` subclasses.
100
+
101
+ - `DibsAlreadyCalledError`: the commit is already reserved
102
+ - `GitDibsHttpError`: a non-success HTTP response came back from the API
103
+
104
+ Example:
105
+
106
+ ```python
107
+ from git_dibs_sdk import DibsAlreadyCalledError, GitDibsError, GitDibsClient
108
+
109
+ client = GitDibsClient()
110
+
111
+ try:
112
+ client.reserve_commit(
113
+ "cccccccccccccccccccccccccccccccccccccccc",
114
+ "TestUser2",
115
+ )
116
+ except DibsAlreadyCalledError as error:
117
+ print(error.commit_hash, error.reserved_by)
118
+ except GitDibsError as error:
119
+ print(f"request failed: {error}")
120
+ ```
121
+
122
+ ## API Notes
123
+
124
+ This SDK matches the current Git Dibs API contract:
125
+
126
+ - `GET /api/commits/<commit>`
127
+ - `GET /api/dibs/recent`
128
+ - `GET /api/dibs/popular`
129
+ - `GET /api/dibs/search?q=<optional-prefix>&after=<optional-full-commit>&limit=<optional-1-to-50>`
130
+ - `POST /api/dibs`
131
+ - `POST /api/upvotes`
132
+
133
+ Lookup returns `204 No Content` when a commit is available and `200 OK` with a dibs payload when it is reserved.
@@ -0,0 +1,112 @@
1
+ # git-dibs-sdk
2
+
3
+ If you're reading this, you're already in too deep. Yes, I really made a python sdk for the useless utility that is "Git Dibs". It actually works.
4
+
5
+
6
+ ## Install
7
+
8
+ ```powershell
9
+ python -m pip install git-dibs-sdk
10
+ ```
11
+
12
+ ## Quick Start
13
+
14
+ ```python
15
+ from git_dibs_sdk import DibsAlreadyCalledError, GitDibsClient
16
+
17
+ client = GitDibsClient()
18
+
19
+ dibs = client.lookup_commit("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
20
+ if dibs is None:
21
+ print("commit is available")
22
+ else:
23
+ print(dibs.reserved_by, dibs.upvote_count)
24
+
25
+ try:
26
+ created = client.reserve_commit(
27
+ commit_hash="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
28
+ reserved_by="bb",
29
+ )
30
+ print(created.hash)
31
+ except DibsAlreadyCalledError as error:
32
+ print(error)
33
+ ```
34
+
35
+ ## Client Surface
36
+
37
+ ```python
38
+ from git_dibs_sdk import GitDibsClient
39
+
40
+ client = GitDibsClient(
41
+ base_url="https://gitdibs.com",
42
+ timeout=10.0,
43
+ )
44
+ ```
45
+
46
+ Available methods:
47
+
48
+ - `lookup_commit(commit_hash) -> Dibs | None`
49
+ - `reserve_commit(commit_hash, reserved_by) -> Dibs`
50
+ - `list_recent_reservations() -> list[Dibs]`
51
+ - `list_popular_reservations() -> list[Dibs]`
52
+ - `search_reservations(query=None, after=None, limit=None) -> DibsSearchResult`
53
+ - `upvote_commit(commit_hash, voter_fingerprint) -> UpvoteResult`
54
+
55
+ `Dibs` fields:
56
+
57
+ - `hash`
58
+ - `reserved_at_utc`
59
+ - `reserved_by`
60
+ - `upvote_count`
61
+
62
+ `DibsSearchResult` fields:
63
+
64
+ - `dibs`
65
+ - `query`
66
+ - `after`
67
+ - `limit`
68
+ - `has_more`
69
+ - `next_after`
70
+
71
+ `UpvoteResult` fields:
72
+
73
+ - `applied`
74
+ - `upvote_count`
75
+
76
+ ## Error Handling
77
+
78
+ All transport, HTTP, and response-shape failures are raised as `GitDibsError` subclasses.
79
+
80
+ - `DibsAlreadyCalledError`: the commit is already reserved
81
+ - `GitDibsHttpError`: a non-success HTTP response came back from the API
82
+
83
+ Example:
84
+
85
+ ```python
86
+ from git_dibs_sdk import DibsAlreadyCalledError, GitDibsError, GitDibsClient
87
+
88
+ client = GitDibsClient()
89
+
90
+ try:
91
+ client.reserve_commit(
92
+ "cccccccccccccccccccccccccccccccccccccccc",
93
+ "TestUser2",
94
+ )
95
+ except DibsAlreadyCalledError as error:
96
+ print(error.commit_hash, error.reserved_by)
97
+ except GitDibsError as error:
98
+ print(f"request failed: {error}")
99
+ ```
100
+
101
+ ## API Notes
102
+
103
+ This SDK matches the current Git Dibs API contract:
104
+
105
+ - `GET /api/commits/<commit>`
106
+ - `GET /api/dibs/recent`
107
+ - `GET /api/dibs/popular`
108
+ - `GET /api/dibs/search?q=<optional-prefix>&after=<optional-full-commit>&limit=<optional-1-to-50>`
109
+ - `POST /api/dibs`
110
+ - `POST /api/upvotes`
111
+
112
+ Lookup returns `204 No Content` when a commit is available and `200 OK` with a dibs payload when it is reserved.
@@ -0,0 +1,46 @@
1
+ [project]
2
+ name = "git-dibs-sdk"
3
+ authors = [
4
+ { name="Andy Giese", email="gieseanw@gmail.com" },
5
+ ]
6
+ # Version is derived from Git tags via hatch-vcs at build time.
7
+ dynamic = ["version"]
8
+ description = "Python SDK for reserving and querying Git Dibs commits"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = "Unlicense"
12
+ license-files = ["LICENSE"]
13
+ classifiers = [
14
+ "Development Status :: 3 - Alpha",
15
+ "Intended Audience :: Developers",
16
+ "License :: Public Domain",
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3.10",
19
+ "Programming Language :: Python :: 3.11",
20
+ "Programming Language :: Python :: 3.12",
21
+ "Programming Language :: Python :: 3.13",
22
+ ]
23
+
24
+ [project.urls]
25
+ Homepage = "https://gitdibs.com"
26
+ Github = "https://github.com/gieseanw/git-dibs-sdk"
27
+ Repository = "https://github.com/gieseanw/git-dibs-sdk.git"
28
+
29
+ [build-system]
30
+ requires = ["hatchling", "hatch-vcs"]
31
+ build-backend = "hatchling.build"
32
+
33
+ [tool.hatch.version]
34
+ # Release versions come from Git tags like v0.2.0.
35
+ # Untagged builds fall back to a placeholder-based development version rooted at 0.0.0.
36
+ source = "vcs"
37
+ fallback-version = "0.0.0"
38
+
39
+ [tool.hatch.version.raw-options]
40
+ version_scheme = "no-guess-dev"
41
+
42
+ [tool.hatch.build.targets.wheel]
43
+ packages = ["src/git_dibs_sdk"]
44
+
45
+ [tool.hatch.build.hooks.vcs]
46
+ version-file = "src/git_dibs_sdk/_version.py"
@@ -0,0 +1,21 @@
1
+ """Client for Git Dibs"""
2
+
3
+ from .client import (
4
+ Dibs,
5
+ DibsAlreadyCalledError,
6
+ DibsSearchResult,
7
+ GitDibsClient,
8
+ GitDibsError,
9
+ GitDibsHttpError,
10
+ UpvoteResult,
11
+ )
12
+
13
+ __all__ = [
14
+ "Dibs",
15
+ "DibsAlreadyCalledError",
16
+ "DibsSearchResult",
17
+ "GitDibsClient",
18
+ "GitDibsError",
19
+ "GitDibsHttpError",
20
+ "UpvoteResult",
21
+ ]
@@ -0,0 +1,24 @@
1
+ # file generated by vcs-versioning
2
+ # don't change, don't track in version control
3
+ from __future__ import annotations
4
+
5
+ __all__ = [
6
+ "__version__",
7
+ "__version_tuple__",
8
+ "version",
9
+ "version_tuple",
10
+ "__commit_id__",
11
+ "commit_id",
12
+ ]
13
+
14
+ version: str
15
+ __version__: str
16
+ __version_tuple__: tuple[int | str, ...]
17
+ version_tuple: tuple[int | str, ...]
18
+ commit_id: str | None
19
+ __commit_id__: str | None
20
+
21
+ __version__ = version = '0.1.0'
22
+ __version_tuple__ = version_tuple = (0, 1, 0)
23
+
24
+ __commit_id__ = commit_id = None
@@ -0,0 +1,375 @@
1
+ #!/usr/bin/env python3
2
+ """Python client for Git Dibs."""
3
+
4
+ from __future__ import annotations
5
+
6
+ from collections.abc import Mapping
7
+ from dataclasses import dataclass
8
+ import json
9
+ from json import JSONDecodeError
10
+ from typing import Any, Literal, overload
11
+ import urllib.error
12
+ from urllib.parse import urlencode
13
+ import urllib.request
14
+
15
+
16
+ DEFAULT_BASE_URL = "https://gitdibs.com"
17
+ DEFAULT_TIMEOUT_SECONDS = 10.0
18
+
19
+
20
+ @dataclass(frozen=True, slots=True)
21
+ class Dibs:
22
+ hash: str
23
+ reserved_at_utc: str
24
+ reserved_by: str
25
+ upvote_count: int = 0
26
+
27
+
28
+ @dataclass(frozen=True, slots=True)
29
+ class DibsSearchResult:
30
+ dibs: list[Dibs]
31
+ query: str | None
32
+ after: str | None
33
+ limit: int
34
+ has_more: bool
35
+ next_after: str | None
36
+
37
+
38
+ @dataclass(frozen=True, slots=True)
39
+ class UpvoteResult:
40
+ applied: bool
41
+ upvote_count: int
42
+
43
+
44
+ class GitDibsError(Exception):
45
+ """Base exception for Git Dibs SDK failures."""
46
+
47
+
48
+ class GitDibsHttpError(GitDibsError):
49
+ """Raised when Git Dibs returns a non-success HTTP response."""
50
+
51
+ def __init__(
52
+ self,
53
+ status_code: int,
54
+ message: str,
55
+ *,
56
+ payload: Mapping[str, Any] | None = None,
57
+ ) -> None:
58
+ self.status_code = status_code
59
+ self.payload = dict(payload or {})
60
+ super().__init__(message)
61
+
62
+
63
+ class DibsAlreadyCalledError(GitDibsError):
64
+ def __init__(self, commit_hash: str, reserved_by: str | None) -> None:
65
+ self.commit_hash = commit_hash
66
+ self.reserved_by = reserved_by
67
+ super().__init__(self._build_message())
68
+
69
+ def _build_message(self) -> str:
70
+ if self.reserved_by:
71
+ return f"{self.commit_hash} is already reserved by {self.reserved_by}"
72
+ return f"{self.commit_hash} is already reserved"
73
+
74
+
75
+ class GitDibsClient:
76
+ def __init__(
77
+ self,
78
+ base_url: str = DEFAULT_BASE_URL,
79
+ *,
80
+ timeout: float = DEFAULT_TIMEOUT_SECONDS,
81
+ ) -> None:
82
+ if not base_url.strip():
83
+ raise ValueError("base_url must not be empty")
84
+ if timeout <= 0:
85
+ raise ValueError("timeout must be greater than zero")
86
+
87
+ self._base_url = base_url.rstrip("/")
88
+ self._timeout = timeout
89
+
90
+ def lookup_commit(self, commit_hash: str) -> Dibs | None:
91
+ payload = self._request_json(
92
+ f"/api/commits/{commit_hash}",
93
+ method="GET",
94
+ allow_no_content=True,
95
+ )
96
+ if payload is None:
97
+ return None
98
+ return _deserialize_dibs_from_container(payload)
99
+
100
+ def reserve_commit(self, commit_hash: str, reserved_by: str) -> Dibs:
101
+ try:
102
+ payload = self._request_json(
103
+ "/api/dibs",
104
+ method="POST",
105
+ payload={
106
+ "commit": commit_hash,
107
+ "calledBy": reserved_by,
108
+ },
109
+ )
110
+ except GitDibsHttpError as error:
111
+ if error.status_code == 400 and _is_conflict_payload(error.payload):
112
+ dibs = _try_deserialize_embedded_dibs(error.payload)
113
+ if dibs is None:
114
+ try:
115
+ dibs = self.lookup_commit(commit_hash)
116
+ except GitDibsError:
117
+ dibs = None
118
+
119
+ raise DibsAlreadyCalledError(
120
+ commit_hash=commit_hash,
121
+ reserved_by=dibs.reserved_by if dibs else None,
122
+ ) from error
123
+ raise
124
+
125
+ return _deserialize_dibs_from_container(payload)
126
+
127
+ def list_recent_reservations(self) -> list[Dibs]:
128
+ payload = self._request_json("/api/dibs/recent", method="GET")
129
+ return _deserialize_dibs_list_from_container(payload)
130
+
131
+ def list_popular_reservations(self) -> list[Dibs]:
132
+ payload = self._request_json("/api/dibs/popular", method="GET")
133
+ return _deserialize_dibs_list_from_container(payload)
134
+
135
+ def search_reservations(
136
+ self,
137
+ *,
138
+ query: str | None = None,
139
+ after: str | None = None,
140
+ limit: int | None = None,
141
+ ) -> DibsSearchResult:
142
+ params: dict[str, str | int] = {}
143
+ if query is not None:
144
+ params["q"] = query
145
+ if after is not None:
146
+ params["after"] = after
147
+ if limit is not None:
148
+ if limit <= 0:
149
+ raise ValueError("limit must be greater than zero")
150
+ params["limit"] = limit
151
+
152
+ payload = self._request_json(
153
+ "/api/dibs/search", method="GET", params=params or None
154
+ )
155
+ return _deserialize_search_result(payload)
156
+
157
+ def upvote_commit(self, commit_hash: str, voter_fingerprint: str) -> UpvoteResult:
158
+ payload = self._request_json(
159
+ "/api/upvotes",
160
+ method="POST",
161
+ payload={
162
+ "commit": commit_hash,
163
+ "voterFingerprint": voter_fingerprint,
164
+ },
165
+ )
166
+ return _deserialize_upvote_result(payload)
167
+
168
+ @overload
169
+ def _request_json(
170
+ self,
171
+ path: str,
172
+ *,
173
+ method: str,
174
+ payload: dict[str, str] | None = None,
175
+ params: dict[str, str | int] | None = None,
176
+ allow_no_content: Literal[True],
177
+ ) -> dict[str, object] | None: ...
178
+
179
+ @overload
180
+ def _request_json(
181
+ self,
182
+ path: str,
183
+ *,
184
+ method: str,
185
+ payload: dict[str, str] | None = None,
186
+ params: dict[str, str | int] | None = None,
187
+ allow_no_content: Literal[False] = False,
188
+ ) -> dict[str, object]: ...
189
+
190
+ def _request_json(
191
+ self,
192
+ path: str,
193
+ *,
194
+ method: str,
195
+ payload: dict[str, str] | None = None,
196
+ params: dict[str, str | int] | None = None,
197
+ allow_no_content: bool = False,
198
+ ) -> dict[str, object] | None:
199
+ body = None
200
+ headers = {
201
+ "Accept": "application/json",
202
+ "User-Agent": "git-dibs-sdk",
203
+ }
204
+
205
+ if payload is not None:
206
+ body = json.dumps(payload).encode("utf-8")
207
+ headers["Content-Type"] = "application/json; charset=utf-8"
208
+
209
+ url = f"{self._base_url}{path}"
210
+ if params:
211
+ url = f"{url}?{urlencode(params)}"
212
+
213
+ request = urllib.request.Request(
214
+ url,
215
+ data=body,
216
+ headers=headers,
217
+ method=method,
218
+ )
219
+
220
+ try:
221
+ with urllib.request.urlopen(request, timeout=self._timeout) as response:
222
+ if allow_no_content and response.status == 204:
223
+ return None
224
+ try:
225
+ payload_value = json.load(response)
226
+ except JSONDecodeError as error:
227
+ raise GitDibsError("Git Dibs returned invalid JSON.") from error
228
+ except urllib.error.HTTPError as error:
229
+ if allow_no_content and error.code == 204:
230
+ return None
231
+ error_payload = _read_error_payload(error)
232
+ message = _build_http_error_message(error.code, error_payload)
233
+ raise GitDibsHttpError(
234
+ error.code, message, payload=error_payload
235
+ ) from error
236
+ except urllib.error.URLError as error:
237
+ reason = getattr(error, "reason", error)
238
+ raise GitDibsError(f"Git Dibs request failed: {reason}") from error
239
+ except OSError as error:
240
+ raise GitDibsError(f"Git Dibs request failed: {error}") from error
241
+
242
+ if not isinstance(payload_value, dict):
243
+ raise GitDibsError("Git Dibs returned an unexpected JSON payload.")
244
+
245
+ return payload_value
246
+
247
+
248
+ def _deserialize_dibs_from_container(payload: Mapping[str, Any]) -> Dibs:
249
+ dibs_payload = payload.get("dibs")
250
+ if not isinstance(dibs_payload, Mapping):
251
+ raise GitDibsError("Git Dibs response did not include a valid dibs object.")
252
+ return _deserialize_dibs(dibs_payload)
253
+
254
+
255
+ def _deserialize_dibs_list_from_container(payload: Mapping[str, Any]) -> list[Dibs]:
256
+ dibs_payload = payload.get("dibs")
257
+ if not isinstance(dibs_payload, list):
258
+ raise GitDibsError("Git Dibs response did not include a valid dibs list.")
259
+ return [_deserialize_dibs(entry) for entry in dibs_payload]
260
+
261
+
262
+ def _deserialize_dibs(payload: Mapping[str, Any]) -> Dibs:
263
+ if not isinstance(payload, Mapping):
264
+ raise GitDibsError("Git Dibs dibs payload was not an object.")
265
+
266
+ try:
267
+ hash_value = str(payload["hash"])
268
+ reserved_at_utc = str(payload["reservedAtUtc"])
269
+ reserved_by = str(payload["reservedBy"])
270
+ except KeyError as error:
271
+ raise GitDibsError(
272
+ f"Git Dibs dibs payload was missing {error.args[0]!r}."
273
+ ) from error
274
+
275
+ try:
276
+ upvote_count = int(payload.get("upvoteCount", 0))
277
+ except (TypeError, ValueError) as error:
278
+ raise GitDibsError(
279
+ "Git Dibs dibs payload included an invalid upvoteCount."
280
+ ) from error
281
+
282
+ return Dibs(
283
+ hash=hash_value,
284
+ reserved_at_utc=reserved_at_utc,
285
+ reserved_by=reserved_by,
286
+ upvote_count=upvote_count,
287
+ )
288
+
289
+
290
+ def _deserialize_search_result(payload: Mapping[str, Any]) -> DibsSearchResult:
291
+ dibs = _deserialize_dibs_list_from_container(payload)
292
+ try:
293
+ limit = int(payload["limit"])
294
+ except KeyError as error:
295
+ raise GitDibsError("Git Dibs search payload was missing 'limit'.") from error
296
+ except (TypeError, ValueError) as error:
297
+ raise GitDibsError(
298
+ "Git Dibs search payload included an invalid 'limit'."
299
+ ) from error
300
+
301
+ has_more = payload.get("hasMore")
302
+ if not isinstance(has_more, bool):
303
+ raise GitDibsError("Git Dibs search payload included an invalid 'hasMore'.")
304
+
305
+ return DibsSearchResult(
306
+ dibs=dibs,
307
+ query=_optional_string(payload.get("query")),
308
+ after=_optional_string(payload.get("after")),
309
+ limit=limit,
310
+ has_more=has_more,
311
+ next_after=_optional_string(payload.get("nextAfter")),
312
+ )
313
+
314
+
315
+ def _deserialize_upvote_result(payload: Mapping[str, Any]) -> UpvoteResult:
316
+ applied = payload.get("applied")
317
+ if not isinstance(applied, bool):
318
+ raise GitDibsError("Git Dibs upvote payload included an invalid 'applied'.")
319
+
320
+ try:
321
+ upvote_count = int(payload["upvoteCount"])
322
+ except KeyError as error:
323
+ raise GitDibsError(
324
+ "Git Dibs upvote payload was missing 'upvoteCount'."
325
+ ) from error
326
+ except (TypeError, ValueError) as error:
327
+ raise GitDibsError(
328
+ "Git Dibs upvote payload included an invalid 'upvoteCount'."
329
+ ) from error
330
+
331
+ return UpvoteResult(applied=applied, upvote_count=upvote_count)
332
+
333
+
334
+ def _read_error_payload(error: urllib.error.HTTPError) -> dict[str, object]:
335
+ try:
336
+ body = error.read().decode("utf-8")
337
+ if not body.strip():
338
+ return {}
339
+ payload = json.loads(body)
340
+ except Exception:
341
+ return {}
342
+
343
+ return payload if isinstance(payload, dict) else {}
344
+
345
+
346
+ def _build_http_error_message(status_code: int, payload: Mapping[str, Any]) -> str:
347
+ message = payload.get("message")
348
+ if isinstance(message, str) and message.strip():
349
+ return message
350
+ return f"Git Dibs request failed with status {status_code}."
351
+
352
+
353
+ def _is_conflict_payload(payload: Mapping[str, Any]) -> bool:
354
+ message = str(payload.get("message", "")).lower()
355
+ details = payload.get("details")
356
+ field = details.get("field") if isinstance(details, Mapping) else None
357
+ return field == "commit" and "already reserved" in message
358
+
359
+
360
+ def _optional_string(value: object) -> str | None:
361
+ if value is None:
362
+ return None
363
+ if isinstance(value, str):
364
+ return value
365
+ raise GitDibsError("Git Dibs returned an unexpected JSON payload.")
366
+
367
+
368
+ def _try_deserialize_embedded_dibs(payload: Mapping[str, Any]) -> Dibs | None:
369
+ dibs_payload = payload.get("dibs")
370
+ if not isinstance(dibs_payload, Mapping):
371
+ return None
372
+ try:
373
+ return _deserialize_dibs(dibs_payload)
374
+ except GitDibsError:
375
+ return None
@@ -0,0 +1,226 @@
1
+ from __future__ import annotations
2
+
3
+ from email.message import Message
4
+ import io
5
+ import json
6
+ from pathlib import Path
7
+ import sys
8
+ import unittest
9
+ from unittest import mock
10
+ import urllib.error
11
+
12
+
13
+ sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
14
+
15
+ from git_dibs_sdk import ( # noqa: E402
16
+ DibsAlreadyCalledError,
17
+ GitDibsClient,
18
+ GitDibsError,
19
+ GitDibsHttpError,
20
+ )
21
+
22
+
23
+ class MockJsonResponse(io.BytesIO):
24
+ def __init__(self, payload: object, *, status: int = 200) -> None:
25
+ super().__init__(json.dumps(payload).encode("utf-8"))
26
+ self.status = status
27
+
28
+
29
+ class MockNoContentResponse(io.BytesIO):
30
+ def __init__(self) -> None:
31
+ super().__init__(b"")
32
+ self.status = 204
33
+
34
+
35
+ def make_http_error(status_code: int, payload: object) -> urllib.error.HTTPError:
36
+ return urllib.error.HTTPError(
37
+ url="https://example.com/api/test",
38
+ code=status_code,
39
+ msg="error",
40
+ hdrs=Message(),
41
+ fp=io.BytesIO(json.dumps(payload).encode("utf-8")),
42
+ )
43
+
44
+
45
+ class GitDibsClientTests(unittest.TestCase):
46
+ def test_lookup_commit_returns_none_on_no_content(self) -> None:
47
+ client = GitDibsClient("https://example.com")
48
+
49
+ with mock.patch("urllib.request.urlopen", return_value=MockNoContentResponse()):
50
+ self.assertIsNone(client.lookup_commit("a" * 40))
51
+
52
+ def test_lookup_commit_uses_configured_timeout(self) -> None:
53
+ client = GitDibsClient("https://example.com", timeout=3.5)
54
+
55
+ with mock.patch(
56
+ "urllib.request.urlopen",
57
+ return_value=MockJsonResponse(
58
+ {
59
+ "dibs": {
60
+ "hash": "a" * 40,
61
+ "reservedAtUtc": "2026-03-28T00:00:00.000Z",
62
+ "reservedBy": "Alice",
63
+ "upvoteCount": 4,
64
+ }
65
+ }
66
+ ),
67
+ ) as mocked_urlopen:
68
+ client.lookup_commit("a" * 40)
69
+
70
+ self.assertEqual(mocked_urlopen.call_args.kwargs["timeout"], 3.5)
71
+
72
+ def test_lookup_commit_wraps_url_errors(self) -> None:
73
+ client = GitDibsClient("https://example.com")
74
+
75
+ with mock.patch(
76
+ "urllib.request.urlopen",
77
+ side_effect=urllib.error.URLError("offline"),
78
+ ):
79
+ with self.assertRaisesRegex(GitDibsError, "offline"):
80
+ client.lookup_commit("a" * 40)
81
+
82
+ def test_lookup_commit_wraps_invalid_response_shapes(self) -> None:
83
+ client = GitDibsClient("https://example.com")
84
+
85
+ with mock.patch(
86
+ "urllib.request.urlopen",
87
+ return_value=MockJsonResponse({"unexpected": {}}),
88
+ ):
89
+ with self.assertRaisesRegex(GitDibsError, "dibs object"):
90
+ client.lookup_commit("a" * 40)
91
+
92
+ def test_lookup_commit_raises_http_error_with_payload(self) -> None:
93
+ client = GitDibsClient("https://example.com")
94
+
95
+ with mock.patch(
96
+ "urllib.request.urlopen",
97
+ side_effect=make_http_error(
98
+ 400,
99
+ {
100
+ "message": "Commit lookup requires a full 40-character hexadecimal commit ID.",
101
+ "details": {"field": "commit"},
102
+ },
103
+ ),
104
+ ):
105
+ with self.assertRaises(GitDibsHttpError) as raised:
106
+ client.lookup_commit("not-a-hash")
107
+
108
+ self.assertEqual(raised.exception.status_code, 400)
109
+ self.assertEqual(raised.exception.payload["details"], {"field": "commit"})
110
+
111
+ def test_reserve_commit_conflict_survives_lookup_failure(self) -> None:
112
+ client = GitDibsClient("https://example.com")
113
+
114
+ with mock.patch(
115
+ "urllib.request.urlopen",
116
+ side_effect=[
117
+ make_http_error(
118
+ 400,
119
+ {
120
+ "message": "That commit is already reserved.",
121
+ "details": {"field": "commit"},
122
+ },
123
+ ),
124
+ urllib.error.URLError("lookup offline"),
125
+ ],
126
+ ):
127
+ with self.assertRaises(DibsAlreadyCalledError) as raised:
128
+ client.reserve_commit("a" * 40, "Alice")
129
+
130
+ self.assertEqual(raised.exception.commit_hash, "a" * 40)
131
+ self.assertIsNone(raised.exception.reserved_by)
132
+
133
+ def test_reserve_commit_prefers_embedded_dibs_on_conflict(self) -> None:
134
+ client = GitDibsClient("https://example.com")
135
+
136
+ with mock.patch(
137
+ "urllib.request.urlopen",
138
+ side_effect=make_http_error(
139
+ 400,
140
+ {
141
+ "message": "That commit is already reserved.",
142
+ "details": {"field": "commit"},
143
+ "dibs": {
144
+ "hash": "a" * 40,
145
+ "reservedAtUtc": "2026-03-28T00:00:00.000Z",
146
+ "reservedBy": "Alice",
147
+ "upvoteCount": 9,
148
+ },
149
+ },
150
+ ),
151
+ ) as mocked_urlopen:
152
+ with self.assertRaises(DibsAlreadyCalledError) as raised:
153
+ client.reserve_commit("a" * 40, "Bob")
154
+
155
+ self.assertEqual(raised.exception.reserved_by, "Alice")
156
+ self.assertEqual(mocked_urlopen.call_count, 1)
157
+
158
+ def test_list_recent_reservations_parses_dibs(self) -> None:
159
+ client = GitDibsClient("https://example.com")
160
+
161
+ with mock.patch(
162
+ "urllib.request.urlopen",
163
+ return_value=MockJsonResponse(
164
+ {
165
+ "dibs": [
166
+ {
167
+ "hash": "a" * 40,
168
+ "reservedAtUtc": "2026-03-28T00:00:00.000Z",
169
+ "reservedBy": "Alice",
170
+ "upvoteCount": 2,
171
+ }
172
+ ]
173
+ }
174
+ ),
175
+ ):
176
+ dibs = client.list_recent_reservations()
177
+
178
+ self.assertEqual(len(dibs), 1)
179
+ self.assertEqual(dibs[0].upvote_count, 2)
180
+
181
+ def test_search_reservations_parses_page(self) -> None:
182
+ client = GitDibsClient("https://example.com")
183
+
184
+ with mock.patch(
185
+ "urllib.request.urlopen",
186
+ return_value=MockJsonResponse(
187
+ {
188
+ "dibs": [
189
+ {
190
+ "hash": "a" * 40,
191
+ "reservedAtUtc": "2026-03-28T00:00:00.000Z",
192
+ "reservedBy": "Alice",
193
+ "upvoteCount": 5,
194
+ }
195
+ ],
196
+ "query": "aaaa",
197
+ "after": "a" * 40,
198
+ "limit": 10,
199
+ "hasMore": True,
200
+ "nextAfter": "b" * 40,
201
+ }
202
+ ),
203
+ ) as mocked_urlopen:
204
+ page = client.search_reservations(query="aaaa", after="a" * 40, limit=10)
205
+
206
+ request = mocked_urlopen.call_args.args[0]
207
+ self.assertIn("q=aaaa", request.full_url)
208
+ self.assertIn("limit=10", request.full_url)
209
+ self.assertTrue(page.has_more)
210
+ self.assertEqual(page.next_after, "b" * 40)
211
+
212
+ def test_upvote_commit_parses_result(self) -> None:
213
+ client = GitDibsClient("https://example.com")
214
+
215
+ with mock.patch(
216
+ "urllib.request.urlopen",
217
+ return_value=MockJsonResponse({"applied": True, "upvoteCount": 7}),
218
+ ):
219
+ result = client.upvote_commit("a" * 40, "browser-fingerprint")
220
+
221
+ self.assertTrue(result.applied)
222
+ self.assertEqual(result.upvote_count, 7)
223
+
224
+
225
+ if __name__ == "__main__":
226
+ unittest.main()