|
| 1 | +package openapi3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | +) |
| 9 | + |
| 10 | +// ReadFromURIFunc defines a function which reads the contents of a resource |
| 11 | +// located at a URI. |
| 12 | +type ReadFromURIFunc func(loader *Loader, url *url.URL) ([]byte, error) |
| 13 | + |
| 14 | +// ErrURINotSupported indicates the ReadFromURIFunc does not know how to handle a |
| 15 | +// given URI. |
| 16 | +var ErrURINotSupported = fmt.Errorf("unsupported URI") |
| 17 | + |
| 18 | +// ReadFromURIs returns a ReadFromURIFunc which tries to read a URI using the |
| 19 | +// given reader functions, in the same order. If a reader function does not |
| 20 | +// support the URI and returns ErrURINotSupported, the next function is checked |
| 21 | +// until a match is found, or the URI is not supported by any. |
| 22 | +func ReadFromURIs(readers ...ReadFromURIFunc) ReadFromURIFunc { |
| 23 | + return func(loader *Loader, url *url.URL) ([]byte, error) { |
| 24 | + for i := range readers { |
| 25 | + buf, err := readers[i](loader, url) |
| 26 | + if err == ErrURINotSupported { |
| 27 | + continue |
| 28 | + } else if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + return buf, nil |
| 32 | + } |
| 33 | + return nil, ErrURINotSupported |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// DefaultReadFromURI returns a ReadFromURIFunc which can read remote HTTP URIs and |
| 38 | +// local file URIs. |
| 39 | +var DefaultReadFromURI = ReadFromURIs(ReadFromHTTP(http.DefaultClient), ReadFromFile) |
| 40 | + |
| 41 | +// ReadFromHTTP returns a ReadFromURIFunc which uses the given http.Client to |
| 42 | +// read the contents from a remote HTTP URI. This client may be customized to |
| 43 | +// implement timeouts, RFC 7234 caching, etc. |
| 44 | +func ReadFromHTTP(cl *http.Client) ReadFromURIFunc { |
| 45 | + return func(loader *Loader, location *url.URL) ([]byte, error) { |
| 46 | + if location.Scheme == "" || location.Host == "" { |
| 47 | + return nil, ErrURINotSupported |
| 48 | + } |
| 49 | + req, err := http.NewRequest("GET", location.String(), nil) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + resp, err := cl.Do(req) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + defer resp.Body.Close() |
| 58 | + if resp.StatusCode > 399 { |
| 59 | + return nil, fmt.Errorf("error loading %q: request returned status code %d", location.String(), resp.StatusCode) |
| 60 | + } |
| 61 | + return ioutil.ReadAll(resp.Body) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// ReadFromFile is a ReadFromURIFunc which reads local file URIs. |
| 66 | +func ReadFromFile(loader *Loader, location *url.URL) ([]byte, error) { |
| 67 | + if location.Host != "" { |
| 68 | + return nil, ErrURINotSupported |
| 69 | + } |
| 70 | + if location.Scheme != "" && location.Scheme != "file" { |
| 71 | + return nil, ErrURINotSupported |
| 72 | + } |
| 73 | + return ioutil.ReadFile(location.Path) |
| 74 | +} |
| 75 | + |
| 76 | +// URIReadCache defines a cache for the contents read from URI references. |
| 77 | +type URIReadCache interface { |
| 78 | + Get(location *url.URL) ([]byte, bool) |
| 79 | + Set(location *url.URL, contents []byte) |
| 80 | +} |
| 81 | + |
| 82 | +// MapURIReadCache implements URIReadCache with a simple map. |
| 83 | +type MapURIReadCache map[string][]byte |
| 84 | + |
| 85 | +// Get implements URIReadCache. |
| 86 | +func (m MapURIReadCache) Get(location *url.URL) ([]byte, bool) { |
| 87 | + contents, ok := m[location.String()] |
| 88 | + return contents, ok |
| 89 | +} |
| 90 | + |
| 91 | +// Set implements URIReadCache. |
| 92 | +func (m MapURIReadCache) Set(location *url.URL, contents []byte) { |
| 93 | + m[location.String()] = contents |
| 94 | +} |
| 95 | + |
| 96 | +// ReadFromCache returns a cached ReadFromURIFunc. If cache is nil, a new |
| 97 | +// internal cache is allocated, scoped to the given reader. |
| 98 | +func ReadFromCache(cache URIReadCache, r ReadFromURIFunc) ReadFromURIFunc { |
| 99 | + if cache == nil { |
| 100 | + cache = MapURIReadCache{} |
| 101 | + } |
| 102 | + return func(loader *Loader, location *url.URL) ([]byte, error) { |
| 103 | + var err error |
| 104 | + cached, ok := cache.Get(location) |
| 105 | + if ok { |
| 106 | + return cached, nil |
| 107 | + } |
| 108 | + cached, err = r(loader, location) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + cache.Set(location, cached) |
| 113 | + return cached, nil |
| 114 | + } |
| 115 | +} |
0 commit comments