-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
schema.go
87 lines (77 loc) · 2.33 KB
/
schema.go
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
77
78
79
80
81
82
83
84
85
86
87
package acapy
import (
"errors"
"fmt"
"strings"
)
type Schema struct {
Ver string `json:"ver"`
ID string `json:"id"`
Name string `json:"name"`
Version string `json:"version"`
AttributeNames []string `json:"attrNames"`
SeqNo int `json:"seqNo"`
}
type schemaRequest struct {
Version string `json:"schema_version"`
Name string `json:"schema_name"`
Attributes []string `json:"attributes"`
}
type schemaResponse struct {
SchemaID string `json:"schema_id"`
Schema Schema `json:"schema"`
}
var ErrInvalidSchemaID = errors.New("invalid schema ID")
// SchemaIDToParts takes a schemaID, for example 6qnvgJtqwK44D8LFYnV5Yf:2:registration.dflow:1.0.0
// and returns the schema's issuer DID, the `ver`, the schema name and the schema version.
func SchemaIDToParts(schemaID string) (string, string, string, string, error) {
parts := strings.Split(schemaID, ":")
if len(parts) != 4 {
return "", "", "", "", ErrInvalidSchemaID
}
return parts[0], parts[1], parts[2], parts[3], nil
}
func (c *Client) RegisterSchema(name string, version string, attributes []string) (Schema, error) {
var request = schemaRequest{
Name: name,
Version: version,
Attributes: attributes,
}
var response schemaResponse
err := c.post("/schemas", nil, request, &response)
if err != nil {
return Schema{}, err
}
return response.Schema, err
}
type QuerySchemasParams struct {
SchemaID string `json:"schema_id"`
SchemaIssuerDID string `json:"schema_issuer_did"`
SchemaName string `json:"schema_name"`
SchemaVersion string `json:"schema_version"`
}
func (c *Client) QuerySchemas(params QuerySchemasParams) ([]string, error) {
type result struct {
SchemaIDs []string `json:"schema_ids"`
}
var r result
queryParams := map[string]string{
"schema_id": params.SchemaID,
"schema_issuer_did": params.SchemaIssuerDID,
"schema_name": params.SchemaName,
"schema_version": params.SchemaVersion,
}
err := c.get("/schemas/created", queryParams, &r)
if err != nil {
return nil, err
}
return r.SchemaIDs, nil
}
func (c *Client) GetSchema(schemaID string) (Schema, error) {
var schemaResponse schemaResponse
err := c.get(fmt.Sprintf("/schemas/%s", schemaID), nil, &schemaResponse)
if err != nil {
return Schema{}, err
}
return schemaResponse.Schema, nil
}