Skip to content

Commit 933d67c

Browse files
authored
Extend Http Request Executor Parameters With Response Body Transformer (#10)
1 parent efc09f1 commit 933d67c

File tree

3 files changed

+63
-42
lines changed

3 files changed

+63
-42
lines changed

internal/executors/http/http_executor.go

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func execute(c http.Client, p *HttpRequestParameters, authHeader AuthorizationHe
192192
Content(string(body)),
193193
Headers(resp.Header),
194194
StatusCode(resp.StatusCode),
195+
ResponseBodyTransformer(p.responseBodyTransformer),
195196
IsSuccessfulBasedOnSuccessResponseCodes(resp.StatusCode, p.successResponseCodes),
196197
Time(<-timeCh),
197198
)

internal/executors/http/http_executor_parameters.go

+46-34
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,49 @@ import (
77
)
88

99
const (
10-
METHOD string = "method"
11-
URL string = "url"
12-
TOKEN_URL string = "tokenUrl"
13-
CSRF_URL string = "csrfUrl"
14-
CLIENT_ID string = "clientId"
15-
CLIENT_SECRET string = "clientSecret"
16-
REFRESH_TOKEN string = "refreshToken"
17-
HEADERS string = "headers"
18-
BODY string = "body"
19-
USER string = "user"
20-
PASSWORD string = "password"
21-
TIMEOUT string = "timeout"
22-
SUCCESS_RESPONSE_CODES string = "successResponseCodes"
23-
SUCCEED_ON_TIMEOUT string = "succeedOnTimeout"
24-
TRUSTED_CERTS string = "trustedCerts"
25-
CLIENT_CERT string = "clientCert"
26-
TRUST_ANY_CERT string = "trustAnyCert"
27-
AUTHORIZATION_HEADER string = "authorizationHeader"
10+
METHOD string = "method"
11+
URL string = "url"
12+
TOKEN_URL string = "tokenUrl"
13+
CSRF_URL string = "csrfUrl"
14+
CLIENT_ID string = "clientId"
15+
CLIENT_SECRET string = "clientSecret"
16+
REFRESH_TOKEN string = "refreshToken"
17+
RESPONSE_BODY_TRANSFORMER string = "responseBodyTransformer"
18+
HEADERS string = "headers"
19+
BODY string = "body"
20+
USER string = "user"
21+
PASSWORD string = "password"
22+
TIMEOUT string = "timeout"
23+
SUCCESS_RESPONSE_CODES string = "successResponseCodes"
24+
SUCCEED_ON_TIMEOUT string = "succeedOnTimeout"
25+
TRUSTED_CERTS string = "trustedCerts"
26+
CLIENT_CERT string = "clientCert"
27+
TRUST_ANY_CERT string = "trustAnyCert"
28+
AUTHORIZATION_HEADER string = "authorizationHeader"
2829
)
2930

3031
var (
3132
defaultSuccessResponseCodes [1]string = [...]string{"2xx"}
3233
)
3334

3435
type HttpRequestParameters struct {
35-
method string
36-
url string
37-
tokenUrl string
38-
csrfUrl string
39-
clientId string
40-
clientSecret string
41-
refreshToken string
42-
headers map[string]string
43-
body string
44-
user string
45-
password string
46-
timeout uint64
47-
successResponseCodes []string
48-
succeedOnTimeout bool
49-
certAuthentication *tls.CertificateAuthentication
50-
authorizationHeader string
36+
method string
37+
url string
38+
tokenUrl string
39+
csrfUrl string
40+
clientId string
41+
clientSecret string
42+
refreshToken string
43+
responseBodyTransformer string
44+
headers map[string]string
45+
body string
46+
user string
47+
password string
48+
timeout uint64
49+
successResponseCodes []string
50+
succeedOnTimeout bool
51+
certAuthentication *tls.CertificateAuthentication
52+
authorizationHeader string
5153
}
5254

5355
func NewHttpRequestParametersFromContext(ctx executors.ExecutorContext) *HttpRequestParameters {
@@ -59,6 +61,7 @@ func NewHttpRequestParametersFromContext(ctx executors.ExecutorContext) *HttpReq
5961
withClientIdFromContext(&ctx),
6062
withClientSecretFromContext(&ctx),
6163
withRefreshTokenFromContext(&ctx),
64+
withResponseBodyTransformerFromContext(&ctx),
6265
withHeadersFromContext(&ctx),
6366
withBodyFromContext(&ctx),
6467
withUserFromContext(&ctx),
@@ -318,6 +321,15 @@ func withRefreshTokenFromContext(ctx *executors.ExecutorContext) functional.Opti
318321
}
319322
}
320323

324+
func withResponseBodyTransformerFromContext(ctx *executors.ExecutorContext) functional.OptionWithError[HttpRequestParameters] {
325+
return func(hrp *HttpRequestParameters) error {
326+
t := ctx.GetString(RESPONSE_BODY_TRANSFORMER)
327+
328+
hrp.responseBodyTransformer = t
329+
return nil
330+
}
331+
}
332+
321333
func withHeadersFromContext(ctx *executors.ExecutorContext) functional.OptionWithError[HttpRequestParameters] {
322334
return func(hrp *HttpRequestParameters) error {
323335
h, err := ctx.GetMap(HEADERS)

internal/executors/http/http_response.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ import (
1414
type HttpHeaders map[string]string
1515

1616
type HttpResponse struct {
17-
Url string `json:"url"`
18-
Method string `json:"method"`
19-
Content string `json:"body"`
20-
Headers HttpHeaders `json:"headers"`
21-
StatusCode string `json:"status"`
22-
SizeInBytes uint `json:"size"`
23-
Time int64 `json:"time"`
24-
successful bool
17+
Url string `json:"url"`
18+
Method string `json:"method"`
19+
Content string `json:"body"`
20+
Headers HttpHeaders `json:"headers"`
21+
StatusCode string `json:"status"`
22+
SizeInBytes uint `json:"size"`
23+
Time int64 `json:"time"`
24+
ResponseBodyTransformer string `json:"responseBodyTransformer"`
25+
successful bool
2526
}
2627

2728
func NewHttpResponse(opts ...functional.OptionWithError[HttpResponse]) (*HttpResponse, error) {
@@ -106,6 +107,13 @@ func Time(time int64) functional.OptionWithError[HttpResponse] {
106107
}
107108
}
108109

110+
func ResponseBodyTransformer(transformer string) functional.OptionWithError[HttpResponse] {
111+
return func(hr *HttpResponse) error {
112+
hr.ResponseBodyTransformer = transformer
113+
return nil
114+
}
115+
}
116+
109117
func IsSuccessfulBasedOnSuccessResponseCodes(statusCode int, successResponseCodes []string) functional.OptionWithError[HttpResponse] {
110118
return func(hr *HttpResponse) error {
111119
isSuccessful, err4 := isSuccessfulResponseCode(uint16(statusCode), successResponseCodes...)

0 commit comments

Comments
 (0)