| 19 | } |
| 20 | |
| 21 | func (l *HTTPURLLoader) Load(url string) (any, error) { |
| 22 | if l.cache != nil { |
| 23 | if cached, err := l.cache.Get(url); err == nil { |
| 24 | return jsonschema.UnmarshalJSON(bytes.NewReader(cached.([]byte))) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | resp, err := l.client.Get(url) |
| 29 | if err != nil { |
| 30 | msg := fmt.Sprintf("failed downloading schema at %s: %s", url, err) |
| 31 | return nil, errors.New(msg) |
| 32 | } |
| 33 | defer resp.Body.Close() |
| 34 | |
| 35 | if resp.StatusCode == http.StatusNotFound { |
| 36 | msg := fmt.Sprintf("could not find schema at %s", url) |
| 37 | return nil, NewNotFoundError(errors.New(msg)) |
| 38 | } |
| 39 | |
| 40 | if resp.StatusCode != http.StatusOK { |
| 41 | msg := fmt.Sprintf("error while downloading schema at %s - received HTTP status %d", url, resp.StatusCode) |
| 42 | return nil, fmt.Errorf("%s", msg) |
| 43 | } |
| 44 | |
| 45 | body, err := io.ReadAll(resp.Body) |
| 46 | if err != nil { |
| 47 | msg := fmt.Sprintf("failed parsing schema from %s: %s", url, err) |
| 48 | return nil, errors.New(msg) |
| 49 | } |
| 50 | |
| 51 | if l.cache != nil { |
| 52 | if err = l.cache.Set(url, body); err != nil { |
| 53 | return nil, fmt.Errorf("failed to write cache to disk: %s", err) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | s, err := jsonschema.UnmarshalJSON(bytes.NewReader(body)) |
| 58 | if err != nil { |
| 59 | return nil, NewNonJSONResponseError(err) |
| 60 | } |
| 61 | |
| 62 | return s, nil |
| 63 | } |
| 64 | |
| 65 | func NewHTTPURLLoader(skipTLS bool, cache cache.Cache) (*HTTPURLLoader, error) { |
| 66 | transport := &http.Transport{ |