diff options
author | Julien Dessaux | 2023-06-18 17:41:38 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-06-18 17:43:53 +0200 |
commit | 6844355a928b60ffb1006e6b1d9f8af7b44647d5 (patch) | |
tree | 57355ad7bf98b870cf96ceec2a2eae64b94d3276 /external/evcli/api.go | |
parent | Implemented project resource (diff) | |
download | terraform-provider-eventline-6844355a928b60ffb1006e6b1d9f8af7b44647d5.tar.gz terraform-provider-eventline-6844355a928b60ffb1006e6b1d9f8af7b44647d5.tar.bz2 terraform-provider-eventline-6844355a928b60ffb1006e6b1d9f8af7b44647d5.zip |
Imported and modified evcli's api client code
Diffstat (limited to '')
-rw-r--r-- | external/evcli/api.go | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/external/evcli/api.go b/external/evcli/api.go new file mode 100644 index 0000000..40fe38c --- /dev/null +++ b/external/evcli/api.go @@ -0,0 +1,86 @@ +package evcli + +import ( + "encoding/json" + "errors" + "fmt" + + "github.com/exograd/eventline/pkg/eventline" + "github.com/exograd/go-daemon/check" +) + +type APIError struct { + Message string `json:"error"` + Code string `json:"code,omitempty"` + RawData json.RawMessage `json:"data,omitempty"` + Data interface{} `json:"-"` +} + +type InvalidRequestBodyError struct { + ValidationErrors check.ValidationErrors `json:"validation_errors"` +} + +func (err APIError) Error() string { + return err.Message +} + +func (err *APIError) UnmarshalJSON(data []byte) error { + type APIError2 APIError + + err2 := APIError2(*err) + if jsonErr := json.Unmarshal(data, &err2); jsonErr != nil { + return jsonErr + } + + switch err2.Code { + case "invalid_request_body": + var errData InvalidRequestBodyError + + if err2.RawData != nil { + if err := json.Unmarshal(err2.RawData, &errData); err != nil { + return fmt.Errorf("invalid jsv errors: %w", err) + } + + err2.Data = &errData + } + } + + *err = APIError(err2) + return nil +} + +func IsInvalidRequestBodyError(err error) (bool, check.ValidationErrors) { + var apiError *APIError + + if !errors.As(err, &apiError) { + return false, nil + } + + requestBodyErr, ok := apiError.Data.(*InvalidRequestBodyError) + if !ok { + return false, nil + } + + return true, requestBodyErr.ValidationErrors +} + +type ProjectPage struct { + Elements eventline.Projects `json:"elements"` + Previous *eventline.Cursor `json:"previous,omitempty"` + Next *eventline.Cursor `json:"next,omitempty"` +} + +type Parameter struct { + Name string `json:"name"` + Type string `json:"type"` + Default interface{} `json:"default"` + Description string `json:"description"` +} + +type Parameters []*Parameter + +type JobPage struct { + Elements eventline.Jobs `json:"elements"` + Previous *eventline.Cursor `json:"previous,omitempty"` + Next *eventline.Cursor `json:"next,omitempty"` +} |