/
api_registry.go
90 lines (77 loc) · 3.01 KB
/
api_registry.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
88
89
90
package readme
import (
"fmt"
)
// APIRegistryEndpoint is the ReadMe API URL endpoint for the API registry.
const APIRegistryEndpoint = "/api-registry"
// APIRegistryService is an interface for interacting with the API Registry endpoints of the
// ReadMe.com API.
type APIRegistryService interface {
// Create a new API registry on ReadMe.
//
// The response returns the UUID and the specification definition.
//
// NOTE: This is an undocumented endpoint on ReadMe and was discovered by inspecting the ReadMe
// CLI application. The registry UUID is required for retrieving the remote specification.
// A typical workflow will be to create the registry with this method and follow-up with a call
// to APISpecification.Create() with the UUID returned from this response.
Create(definition string, version ...string) (APIRegistrySaved, *APIResponse, error)
// Get retrieves an API definition from the ReadMe.com API registry with a provided UUID and
// returns it as a string.
//
// API Reference: https://docs.readme.com/main/reference/getapiregistry
Get(uuid string) (string, *APIResponse, error)
}
// APIRegistryClient handles communication with the Registry related methods of the ReadMe.com API.
type APIRegistryClient struct {
client *Client
}
// APIRegistrySaved represents the API response when an API Registry is created.
type APIRegistrySaved struct {
Definition map[string]interface{} `json:"definition"`
RegistryUUID string `json:"registryUUID"`
}
// Ensure the implementation satisfies the expected interfaces.
var _ APIRegistryService = &APIRegistryClient{}
// Get retrieves an API definition from the ReadMe.com API registry with a provided UUID and returns
// it as a string.
//
// API Reference: https://docs.readme.com/main/reference/getapiregistry
func (c APIRegistryClient) Get(uuid string) (string, *APIResponse, error) {
apiResponse, err := c.client.APIRequest(&APIRequest{
Method: "GET",
Endpoint: fmt.Sprintf("%s/%s", APIRegistryEndpoint, uuid),
UseAuth: true,
OkStatusCode: []int{200},
})
if err != nil {
return "", apiResponse, err
}
spec := string(apiResponse.Body)
return spec, apiResponse, nil
}
// Create a new API registry on ReadMe.
//
// The response returns the UUID and the specification definition.
//
// NOTE: This is an undocumented endpoint on ReadMe and was discovered by inspecting the ReadMe CLI
// application.
//
// The registry UUID is required for retrieving the remote specification.
// A typical workflow will be to create the registry with this method and follow-up with a call to
// APISpecification.Create() with the UUID returned from this response.
func (c APIRegistryClient) Create(definition string, version ...string) (APIRegistrySaved, *APIResponse, error) {
var vers string
if len(version) > 0 {
vers = version[0]
}
response := APIRegistrySaved{}
_, apiResponse, err := c.client.APISpecification.UploadDefinition(
"POST",
definition,
APIRegistryEndpoint,
vers,
&response,
)
return response, apiResponse, err // nolint:wrapcheck
}