Skip to content

Commit 1267582

Browse files
committed
feat!: switch transformResponse expression language to Expr
1 parent 2c47590 commit 1267582

File tree

4 files changed

+44
-39
lines changed

4 files changed

+44
-39
lines changed

internal/runner/request/request.go

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66
"os"
77
"path/filepath"
88

9-
"github.com/itchyny/gojq"
109
"github.com/pkg/errors"
1110
"gopkg.in/yaml.v3"
1211

1312
"github.com/jahvon/flow/internal/context"
1413
"github.com/jahvon/flow/internal/runner"
1514
"github.com/jahvon/flow/internal/runner/engine"
15+
"github.com/jahvon/flow/internal/services/expr"
1616
"github.com/jahvon/flow/internal/services/rest"
1717
"github.com/jahvon/flow/types/executable"
1818
)
@@ -63,16 +63,17 @@ func (r *requestRunner) Exec(
6363
return errors.Wrap(err, "request failed")
6464
}
6565

66+
respStr := resp.Body
6667
if requestSpec.TransformResponse != "" {
67-
resp, err = executeJQQuery(requestSpec.TransformResponse, resp)
68+
respStr, err = expr.EvaluateString(requestSpec.TransformResponse, resp)
6869
if err != nil {
6970
return errors.Wrap(err, "unable to transform response")
7071
}
7172
}
7273

7374
logger := ctx.Logger
7475
if requestSpec.LogResponse {
75-
logger.Infox(fmt.Sprintf("Successfully sent request to %s", requestSpec.URL), "response", resp)
76+
logger.Infox(fmt.Sprintf("Successfully sent request to %s", requestSpec.URL), "response", respStr)
7677
} else {
7778
logger.Infof("Successfully sent request to %s", requestSpec.URL)
7879
}
@@ -92,7 +93,7 @@ func (r *requestRunner) Exec(
9293
}
9394

9495
err = writeResponseToFile(
95-
resp,
96+
respStr,
9697
filepath.Join(targetDir, requestSpec.ResponseFile.Filename),
9798
requestSpec.ResponseFile.SaveAs,
9899
)
@@ -106,30 +107,6 @@ func (r *requestRunner) Exec(
106107
return nil
107108
}
108109

109-
func executeJQQuery(query, resp string) (string, error) {
110-
var respMap map[string]interface{}
111-
err := json.Unmarshal([]byte(resp), &respMap)
112-
if err != nil {
113-
return "", errors.New("response is not a valid JSON string")
114-
}
115-
116-
jqQuery, err := gojq.Parse(query)
117-
if err != nil {
118-
return "", err
119-
}
120-
121-
iter := jqQuery.Run(respMap)
122-
result, ok := iter.Next()
123-
if !ok {
124-
return "", errors.New("unable to execute jq query")
125-
}
126-
if err, isErr := result.(error); isErr {
127-
return "", err
128-
}
129-
130-
return fmt.Sprintf("%v", result), nil
131-
}
132-
133110
func writeResponseToFile(resp, responseFile string, format executable.RequestResponseFileSaveAs) error {
134111
var formattedResp string
135112
switch format {

internal/runner/request/request_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ var _ = Describe("Request Runner", func() {
8585
},
8686
}
8787

88-
ctx.Logger.EXPECT().Infox(gomock.Any(), gomock.Any(), gomock.Any()).Times(1)
88+
ctx.Logger.EXPECT().Infox(gomock.Any(), gomock.Any(), gomock.Regex("value")).Times(1)
8989
err := requestRnr.Exec(ctx.Ctx, exec, mockEngine, make(map[string]string))
9090
Expect(err).NotTo(HaveOccurred())
9191
})
@@ -111,5 +111,20 @@ var _ = Describe("Request Runner", func() {
111111
_, err = os.Stat(filepath.Clean(filepath.Join(ctx.Ctx.CurrentWorkspace.Location(), "response.json")))
112112
Expect(err).NotTo(HaveOccurred())
113113
})
114+
115+
It("should transform the response when specified", func() {
116+
exec := &executable.Executable{
117+
Request: &executable.RequestExecutableType{
118+
URL: "https://httpbin.org/get",
119+
Method: executable.RequestExecutableTypeMethodGET,
120+
TransformResponse: `upper(body)`,
121+
LogResponse: true,
122+
},
123+
}
124+
125+
ctx.Logger.EXPECT().Infox(gomock.Any(), gomock.Any(), gomock.Regex("HTTPS://HTTPBIN.ORG")).Times(1)
126+
err := requestRnr.Exec(ctx.Ctx, exec, mockEngine, make(map[string]string))
127+
Expect(err).NotTo(HaveOccurred())
128+
})
114129
})
115130
})

internal/services/rest/rest.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ type Request struct {
2121
Timeout time.Duration
2222
}
2323

24-
func SendRequest(reqSpec *Request, validStatusCodes []int) (string, error) {
24+
type Response struct {
25+
Status string `expr:"status"`
26+
Code int `expr:"code"`
27+
Body string `expr:"body"`
28+
Headers http.Header `expr:"headers"`
29+
}
30+
31+
func SendRequest(reqSpec *Request, validStatusCodes []int) (*Response, error) {
2532
setRequestDefaults(reqSpec)
2633
client := http.Client{Timeout: reqSpec.Timeout}
2734
reqURL, err := url.Parse(reqSpec.URL)
2835
if err != nil {
29-
return "", err
36+
return nil, err
3037
}
3138

3239
headers := make(http.Header)
@@ -44,19 +51,25 @@ func SendRequest(reqSpec *Request, validStatusCodes []int) (string, error) {
4451

4552
httpResp, err := client.Do(&req)
4653
if err != nil {
47-
return "", err
54+
return nil, err
4855
}
4956
defer httpResp.Body.Close()
5057

5158
if !isStatusCodeAccepted(httpResp.StatusCode, validStatusCodes) {
52-
return "", ErrUnexpectedStatusCode
59+
return nil, ErrUnexpectedStatusCode
5360
}
5461

5562
respBody, err := io.ReadAll(httpResp.Body)
5663
if err != nil {
57-
return "", err
64+
return nil, err
65+
}
66+
resp := &Response{
67+
Status: httpResp.Status,
68+
Code: httpResp.StatusCode,
69+
Body: string(respBody),
70+
Headers: httpResp.Header,
5871
}
59-
return string(respBody), nil
72+
return resp, nil
6073
}
6174

6275
func setRequestDefaults(req *Request) {

internal/services/rest/rest_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ var _ = Describe("Rest", func() {
4444
Method: "GET",
4545
Timeout: 30 * time.Second,
4646
}
47-
body, err := rest.SendRequest(req, []int{http.StatusOK})
47+
resp, err := rest.SendRequest(req, []int{http.StatusOK})
4848
Expect(err).NotTo(HaveOccurred())
49-
Expect(body).To(ContainSubstring("\"url\": \"https://httpbin.org/get\""))
49+
Expect(resp.Body).To(ContainSubstring("\"url\": \"https://httpbin.org/get\""))
5050
})
5151

5252
It("should timeout when the request takes longer than the specified timeout", func() {
@@ -67,9 +67,9 @@ var _ = Describe("Rest", func() {
6767
Headers: map[string]string{"Test-Header": "Test-Value"},
6868
Timeout: 30 * time.Second,
6969
}
70-
body, err := rest.SendRequest(req, []int{http.StatusOK})
70+
resp, err := rest.SendRequest(req, []int{http.StatusOK})
7171
Expect(err).NotTo(HaveOccurred())
72-
Expect(body).To(ContainSubstring("\"Test-Header\": \"Test-Value\""))
72+
Expect(resp.Body).To(ContainSubstring("\"Test-Header\": \"Test-Value\""))
7373
})
7474
})
7575
})

0 commit comments

Comments
 (0)