-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathlessons.go
175 lines (147 loc) · 3.49 KB
/
lessons.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package api
import (
"encoding/json"
"fmt"
)
type Lesson struct {
Lesson struct {
Type string
LessonDataCLI *LessonDataCLI
}
}
type LessonDataCLI struct {
// Readme string
CLIData CLIData
}
const BaseURLOverrideRequired = "override"
type CLIData struct {
// ContainsCompleteDir bool
BaseURLDefault string
Steps []CLIStep
}
type CLIStep struct {
CLICommand *CLIStepCLICommand
HTTPRequest *CLIStepHTTPRequest
}
type CLIStepCLICommand struct {
Command string
Tests []CLICommandTest
}
type CLICommandTest struct {
ExitCode *int
StdoutContainsAll []string
StdoutContainsNone []string
StdoutLinesGt *int
}
type CLIStepHTTPRequest struct {
ResponseVariables []HTTPRequestResponseVariable
Tests []HTTPRequestTest
Request HTTPRequest
}
const BaseURLPlaceholder = "${baseURL}"
type HTTPRequest struct {
Method string
FullURL string
Headers map[string]string
BodyJSON map[string]any
BasicAuth *HTTPBasicAuth
Actions HTTPActions
}
type HTTPBasicAuth struct {
Username string
Password string
}
type HTTPActions struct {
DelayRequestByMs *int
}
type HTTPRequestResponseVariable struct {
Name string
Path string
}
// Only one of these fields should be set
type HTTPRequestTest struct {
StatusCode *int
BodyContains *string
BodyContainsNone *string
HeadersContain *HTTPRequestTestHeader
TrailersContain *HTTPRequestTestHeader
JSONValue *HTTPRequestTestJSONValue
}
type HTTPRequestTestHeader struct {
Key string
Value string
}
type HTTPRequestTestJSONValue struct {
Path string
Operator OperatorType
IntValue *int
StringValue *string
BoolValue *bool
}
type OperatorType string
const (
OpEquals OperatorType = "eq"
OpGreaterThan OperatorType = "gt"
OpContains OperatorType = "contains"
OpNotContains OperatorType = "not_contains"
)
func FetchLesson(uuid string) (*Lesson, error) {
resp, err := fetchWithAuth("GET", "/v1/static/lessons/"+uuid)
if err != nil {
return nil, err
}
var data Lesson
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return &data, nil
}
type CLIStepResult struct {
CLICommandResult *CLICommandResult
HTTPRequestResult *HTTPRequestResult
}
type CLICommandResult struct {
ExitCode int
FinalCommand string `json:"-"`
Stdout string
Variables map[string]string
}
type HTTPRequestResult struct {
Err string `json:"-"`
StatusCode int
ResponseHeaders map[string]string
ResponseTrailers map[string]string
BodyString string
Variables map[string]string
Request CLIStepHTTPRequest
}
type lessonSubmissionCLI struct {
CLIResults []CLIStepResult
}
type StructuredErrCLI struct {
ErrorMessage string `json:"Error"`
FailedStepIndex int `json:"FailedStepIndex"`
FailedTestIndex int `json:"FailedTestIndex"`
}
func SubmitCLILesson(uuid string, results []CLIStepResult) (*StructuredErrCLI, error) {
bytes, err := json.Marshal(lessonSubmissionCLI{CLIResults: results})
if err != nil {
return nil, err
}
endpoint := fmt.Sprintf("/v1/lessons/%v/", uuid)
resp, code, err := fetchWithAuthAndPayload("POST", endpoint, bytes)
if err != nil {
return nil, err
}
if code != 200 {
return nil, fmt.Errorf("failed to submit CLI lesson (code: %v): %s", code, string(resp))
}
var failure StructuredErrCLI
err = json.Unmarshal(resp, &failure)
if err != nil || failure.ErrorMessage == "" {
// this is ok - it means we had success
return nil, nil
}
return &failure, nil
}