Skip to content

Commit 45636c3

Browse files
Add single fetch GET metadata endpoint
1 parent 76bc280 commit 45636c3

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

cloudsmith_cli/core/api/metadata.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,20 @@ def list_metadata(
177177
return results, page_info
178178

179179

180+
def get_metadata(package_slug_perm: str, metadata_slug_perm: str):
181+
"""Retrieve a single metadata entry attached to a package."""
182+
client = get_metadata_api()
183+
response = _request(
184+
client,
185+
"GET",
186+
"packages",
187+
package_slug_perm,
188+
"metadata",
189+
metadata_slug_perm,
190+
)
191+
return _response_json(response)
192+
193+
180194
def create_metadata(
181195
package_slug_perm: str,
182196
*,

cloudsmith_cli/core/tests/test_metadata.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,74 @@ def test_422_raises_with_fields(self):
191191
assert exc_info.value.fields == {"source_kind": ["Not a valid choice."]}
192192

193193

194+
class TestGetMetadata:
195+
@httpretty.activate(allow_net_connect=False)
196+
def test_success_returns_metadata_entry(self):
197+
body = {
198+
"slug_perm": META,
199+
"content": {"foo": "bar"},
200+
"content_type": "application/json",
201+
"classification": "generic",
202+
"source_kind": "customer",
203+
"source_identity": "cloudsmith-cli@1.16.0",
204+
"is_canonical": True,
205+
"source_table": "package_metadata",
206+
"created_at": "2026-04-30T12:34:56Z",
207+
}
208+
httpretty.register_uri(
209+
httpretty.GET,
210+
DETAIL_URL,
211+
body=json.dumps(body),
212+
status=200,
213+
content_type="application/json",
214+
)
215+
216+
result = metadata.get_metadata(PKG, META)
217+
218+
assert result == body
219+
sent = _last_request()
220+
assert sent.method == "GET"
221+
assert sent.headers.get("X-Api-Key") == "test-api-key"
222+
223+
@httpretty.activate(allow_net_connect=False)
224+
def test_404_on_unknown_metadata(self):
225+
httpretty.register_uri(
226+
httpretty.GET,
227+
DETAIL_URL,
228+
body=json.dumps({"detail": "Not found."}),
229+
status=404,
230+
content_type="application/json",
231+
)
232+
233+
with pytest.raises(ApiException) as exc_info:
234+
metadata.get_metadata(PKG, META)
235+
236+
assert exc_info.value.status == 404
237+
assert exc_info.value.detail == "Not found."
238+
239+
@httpretty.activate(allow_net_connect=False)
240+
def test_422_raises_with_fields(self):
241+
body = {
242+
"detail": "Validation failed.",
243+
"fields": {"metadata_slug_perm": ["Invalid metadata slug."]},
244+
}
245+
httpretty.register_uri(
246+
httpretty.GET,
247+
DETAIL_URL,
248+
body=json.dumps(body),
249+
status=422,
250+
content_type="application/json",
251+
)
252+
253+
with pytest.raises(ApiException) as exc_info:
254+
metadata.get_metadata(PKG, META)
255+
256+
assert exc_info.value.status == 422
257+
assert exc_info.value.fields == {
258+
"metadata_slug_perm": ["Invalid metadata slug."]
259+
}
260+
261+
194262
class TestCreateMetadata:
195263
@httpretty.activate(allow_net_connect=False)
196264
def test_success_posts_required_fields(self):

0 commit comments

Comments
 (0)