-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.go
More file actions
76 lines (68 loc) · 2.29 KB
/
schema.go
File metadata and controls
76 lines (68 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package ojs
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"
)
// Schema represents a registered schema summary returned by list endpoints.
type Schema struct {
URI string `json:"uri"`
Type string `json:"type"`
Version string `json:"version"`
CreatedAt time.Time `json:"created_at"`
}
// SchemaDetail represents the full schema object returned by the get endpoint,
// including the JSON Schema definition itself.
type SchemaDetail struct {
URI string `json:"uri"`
Type string `json:"type"`
Version string `json:"version"`
Schema json.RawMessage `json:"schema"`
CreatedAt time.Time `json:"created_at"`
}
// SchemaDefinition is the request body for registering a new schema.
type SchemaDefinition struct {
URI string `json:"uri"`
Type string `json:"type"`
Version string `json:"version"`
Schema json.RawMessage `json:"schema"`
}
// ListSchemas returns all registered schemas with pagination metadata.
func (c *Client) ListSchemas(ctx context.Context) ([]Schema, *Pagination, error) {
var resp struct {
Schemas []Schema `json:"schemas"`
Pagination Pagination `json:"pagination"`
}
if err := c.transport.get(ctx, basePath+"/schemas", &resp); err != nil {
return nil, nil, err
}
return resp.Schemas, &resp.Pagination, nil
}
// RegisterSchema registers a new schema definition with the server.
func (c *Client) RegisterSchema(ctx context.Context, schema SchemaDefinition) (*Schema, error) {
var resp struct {
Schema Schema `json:"schema"`
}
if err := c.transport.post(ctx, basePath+"/schemas", schema, &resp); err != nil {
return nil, err
}
return &resp.Schema, nil
}
// GetSchema retrieves the full details of a schema by URI.
func (c *Client) GetSchema(ctx context.Context, uri string) (*SchemaDetail, error) {
var resp struct {
Schema SchemaDetail `json:"schema"`
}
path := fmt.Sprintf("%s/schemas/%s", basePath, url.PathEscape(uri))
if err := c.transport.get(ctx, path, &resp); err != nil {
return nil, err
}
return &resp.Schema, nil
}
// DeleteSchema removes a registered schema by URI.
func (c *Client) DeleteSchema(ctx context.Context, uri string) error {
path := fmt.Sprintf("%s/schemas/%s", basePath, url.PathEscape(uri))
return c.transport.delete(ctx, path, nil)
}