...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package discovery_v1
16
17 import (
18 "encoding/json"
19 "errors"
20 "strings"
21
22 "github.com/google/gnostic-models/compiler"
23 )
24
25
26 const APIsListServiceURL = "https://www.googleapis.com/discovery/v1/apis"
27
28
29
30 type List struct {
31 Kind string `json:"kind"`
32 DiscoveryVersion string `json:"discoveryVersion"`
33 APIs []*API `json:"items"`
34 }
35
36 func FetchListBytes() ([]byte, error) {
37 return compiler.FetchFile(APIsListServiceURL)
38 }
39
40
41 func FetchList() (*List, error) {
42 bytes, err := FetchListBytes()
43 if err != nil {
44 return nil, err
45 }
46 return ParseList(bytes)
47 }
48
49
50 func ParseList(bytes []byte) (*List, error) {
51 var listResponse List
52 err := json.Unmarshal(bytes, &listResponse)
53 return &listResponse, err
54 }
55
56
57 type API struct {
58 Kind string `json:"kind"`
59 ID string `json:"id"`
60 Name string `json:"name"`
61 Version string `json:"version"`
62 Title string `json:"title"`
63 Description string `json:"description"`
64 DiscoveryRestURL string `json:"discoveryRestUrl"`
65 DiscoveryLink string `json:"discoveryLink"`
66 Icons map[string]string `json:"icons"`
67 DocumentationLink string `json:"documentationLink"`
68 Labels []string `json:"labels"`
69 Preferred bool `json:"preferred"`
70 }
71
72
73
74 func (a *List) APIWithNameAndVersion(name string, version string) (*API, error) {
75 var api *API
76 versions := make([]string, 0)
77
78 for _, item := range a.APIs {
79 if item.Name == name {
80 if version == "" || version == item.Version {
81 api = item
82 versions = append(versions, item.Version)
83 }
84 }
85 }
86 switch {
87 case len(versions) == 0:
88 return nil, errors.New(name + " was not found.")
89 case len(versions) > 1:
90 return nil, errors.New(name + " has multiple versions: " + strings.Join(versions, ", "))
91 default:
92 return api, nil
93 }
94 }
95
View as plain text