aboutsummaryrefslogtreecommitdiff
path: root/http_run_handler.go
diff options
context:
space:
mode:
Diffstat (limited to 'http_run_handler.go')
-rw-r--r--http_run_handler.go86
1 files changed, 86 insertions, 0 deletions
diff --git a/http_run_handler.go b/http_run_handler.go
new file mode 100644
index 0000000..00212ec
--- /dev/null
+++ b/http_run_handler.go
@@ -0,0 +1,86 @@
+// SPDX-FileCopyrightText: 2024 M. Shulhan <ms@kilabit.info>
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package gorankusu
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+ "net/http"
+
+ libhttp "github.com/shuLhan/share/lib/http"
+)
+
+// HTTPRunHandler define the function type that will be called when client
+// send request to run the HTTP target.
+type HTTPRunHandler func(rr *RunRequest) (runres *RunResponse, err error)
+
+// DefaultHTTPRun default [HTTPTarget.Run] handler that generate
+// [http.Request], send it to the target, and store and dump them into
+// [RunResponse].
+func DefaultHTTPRun() HTTPRunHandler {
+ return func(rr *RunRequest) (res *RunResponse, err error) {
+ var (
+ logp = `DefaultHTTPRun`
+ httpcOpts = &libhttp.ClientOptions{
+ ServerUrl: rr.Target.BaseURL,
+ AllowInsecure: true,
+ }
+ httpc = libhttp.NewClient(httpcOpts)
+ params any
+ )
+
+ if !rr.HTTPTarget.WithRawBody {
+ rr.HTTPTarget.paramsToPath()
+
+ params, err = rr.HTTPTarget.ParamsConverter(&rr.HTTPTarget)
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
+ }
+ }
+
+ var (
+ headers = rr.HTTPTarget.Headers.ToHTTPHeader()
+
+ httpRequest *http.Request
+ )
+
+ httpRequest, err = httpc.GenerateHttpRequest(
+ rr.HTTPTarget.Method,
+ rr.HTTPTarget.Path,
+ rr.HTTPTarget.RequestType,
+ headers,
+ params,
+ )
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
+ }
+
+ if rr.HTTPTarget.WithRawBody {
+ httpRequest.Body = io.NopCloser(bytes.NewReader(rr.HTTPTarget.RawBody))
+ httpRequest.ContentLength = int64(len(rr.HTTPTarget.RawBody))
+ }
+
+ res = &RunResponse{}
+
+ err = res.SetHTTPRequest(rr.HTTPTarget.RequestDumper, httpRequest)
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
+ }
+
+ var httpResponse *http.Response
+
+ httpResponse, _, err = httpc.Do(httpRequest)
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
+ }
+
+ err = res.SetHTTPResponse(rr.HTTPTarget.ResponseDumper, httpResponse)
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
+ }
+
+ return res, nil
+ }
+}