|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/url" |
| 7 | + |
| 8 | + "github.com/iancoleman/orderedmap" |
| 9 | +) |
| 10 | + |
| 11 | +type StepTypesVersions struct { |
| 12 | + Name string |
| 13 | + Versions []StepTypesVersion |
| 14 | +} |
| 15 | +type StepTypesVersion struct { |
| 16 | + VersionNumber string |
| 17 | + StepTypes StepTypes |
| 18 | +} |
| 19 | + |
| 20 | +type StepTypes struct { |
| 21 | + Version string `json:"version,omitempty"` |
| 22 | + Kind string `json:"kind,omitempty"` |
| 23 | + Metadata map[string]interface{} `json:"metadata,omitempty"` |
| 24 | + Spec SpecStepTypes `json:"spec,omitempty"` |
| 25 | +} |
| 26 | + |
| 27 | +type SpecStepTypes struct { |
| 28 | + Arguments string `json:"arguments,omitempty"` |
| 29 | + Delimiters map[string]interface{} `json:"delimiters,omitempty"` |
| 30 | + Returns string `json:"returns,omitempty"` |
| 31 | + Steps *orderedmap.OrderedMap `json:"steps,omitempty"` |
| 32 | + StepsTemplate string `json:"stepsTemplate,omitempty"` |
| 33 | +} |
| 34 | + |
| 35 | +func (stepTypes *StepTypes) GetID() string { |
| 36 | + return stepTypes.Metadata["name"].(string) |
| 37 | +} |
| 38 | + |
| 39 | +func (client *Client) GetStepTypesVersions(name string) ([]string, error) { |
| 40 | + fullPath := fmt.Sprintf("/step-types/%s/versions", url.PathEscape(name)) |
| 41 | + opts := RequestOptions{ |
| 42 | + Path: fullPath, |
| 43 | + Method: "GET", |
| 44 | + } |
| 45 | + |
| 46 | + resp, err := client.RequestAPI(&opts) |
| 47 | + |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + var respStepTypesVersions []string |
| 52 | + err = DecodeResponseInto(resp, &respStepTypesVersions) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + return respStepTypesVersions, nil |
| 57 | +} |
| 58 | + |
| 59 | +func (client *Client) GetStepTypes(identifier string) (*StepTypes, error) { |
| 60 | + fullPath := fmt.Sprintf("/step-types/%s", url.PathEscape(identifier)) |
| 61 | + opts := RequestOptions{ |
| 62 | + Path: fullPath, |
| 63 | + Method: "GET", |
| 64 | + } |
| 65 | + |
| 66 | + resp, err := client.RequestAPI(&opts) |
| 67 | + |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + var respStepTypes StepTypes |
| 72 | + err = DecodeResponseInto(resp, &respStepTypes) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + return &respStepTypes, nil |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | +func (client *Client) CreateStepTypes(stepTypes *StepTypes) (*StepTypes, error) { |
| 82 | + |
| 83 | + body, err := EncodeToJSON(stepTypes) |
| 84 | + |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + opts := RequestOptions{ |
| 89 | + Path: "/step-types", |
| 90 | + Method: "POST", |
| 91 | + Body: body, |
| 92 | + } |
| 93 | + |
| 94 | + resp, err := client.RequestAPI(&opts) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + var respStepTypes StepTypes |
| 100 | + err = DecodeResponseInto(resp, &respStepTypes) |
| 101 | + if err != nil { |
| 102 | + log.Printf("[DEBUG] Error while decoding step types. Error = %v, Response: %v", err, respStepTypes) |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + return &respStepTypes, nil |
| 106 | + |
| 107 | +} |
| 108 | + |
| 109 | +func (client *Client) UpdateStepTypes(stepTypes *StepTypes) (*StepTypes, error) { |
| 110 | + |
| 111 | + body, err := EncodeToJSON(stepTypes) |
| 112 | + |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + fullPath := fmt.Sprintf("/step-types/%s", url.PathEscape(stepTypes.Metadata["name"].(string)+":"+stepTypes.Metadata["version"].(string))) |
| 118 | + opts := RequestOptions{ |
| 119 | + Path: fullPath, |
| 120 | + Method: "PUT", |
| 121 | + Body: body, |
| 122 | + } |
| 123 | + |
| 124 | + resp, err := client.RequestAPI(&opts) |
| 125 | + |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + |
| 130 | + var respStepTypes StepTypes |
| 131 | + err = DecodeResponseInto(resp, &respStepTypes) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + |
| 136 | + return &respStepTypes, nil |
| 137 | + |
| 138 | +} |
| 139 | + |
| 140 | +func (client *Client) DeleteStepTypes(name string) error { |
| 141 | + |
| 142 | + fullPath := fmt.Sprintf("/step-types/%s", url.PathEscape(name)) |
| 143 | + opts := RequestOptions{ |
| 144 | + Path: fullPath, |
| 145 | + Method: "DELETE", |
| 146 | + } |
| 147 | + |
| 148 | + _, err := client.RequestAPI(&opts) |
| 149 | + |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + return nil |
| 155 | +} |
0 commit comments