Skip to content

Commit

Permalink
Add ability to get JSON schema for index entry
Browse files Browse the repository at this point in the history
  • Loading branch information
pkosiec committed Feb 15, 2024
1 parent b5bfe38 commit 33bc3e1
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/plugin/index.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package plugin

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"

"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -154,3 +157,32 @@ func (in Index) Validate() error {

return issues.ErrorOrNil()
}

// Get returns the JSON schema.
// When RefURL is not empty, Get tries to fetch the schema from the URL.
func (s *JSONSchema) Get(ctx context.Context, httpCli *http.Client) (json.RawMessage, error) {
if s.Value != "" {
return json.RawMessage(s.Value), nil
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.RefURL, http.NoBody)
if err != nil {
return nil, fmt.Errorf("while creating request: %w", err)
}

res, err := httpCli.Do(req)
if err != nil {
return nil, fmt.Errorf("while fetching JSON schema by RefURL %q: %w", s.RefURL, err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("while getting JSON schema: got status code: %d", res.StatusCode)
}

var out json.RawMessage
if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
return nil, err
}
return out, nil
}

0 comments on commit 33bc3e1

Please sign in to comment.