-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
105 lines (95 loc) · 2.69 KB
/
app.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
package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
"time"
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *App) DoHttp(config AxiosRequestConfig) AxiosResponse {
resp, _ := doRequest(config)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
bodyStr := string(body)
headers := make(map[string]string)
for key, value := range resp.Header {
headers[key] = strings.Join(value, " ")
}
return AxiosResponse{Status: resp.StatusCode, StatusText: resp.Status, Headers: headers, Config: config, Data: bodyStr}
}
type AxiosRequestConfig struct {
URL string `json:"url,omitempty"`
Method string `json:"method,omitempty"`
BaseURL string `json:"baseURL,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Auth map[string]string `json:"auth,omitempty"`
Params interface{} `json:"params,omitempty"`
Data interface{} `json:"data,omitempty"`
}
type AxiosResponse struct {
Status int `json:"status"`
StatusText string `json:"statusText"`
Headers map[string]string `json:"headers"`
Config AxiosRequestConfig `json:"config"`
Data interface{} `json:"data"`
}
func doRequest(config AxiosRequestConfig) (*http.Response, error) {
client := &http.Client{Timeout: 10 * time.Second}
// Build URL with parameters if any
fullUrl := config.BaseURL + config.URL
if config.Params != nil {
params := url.Values{}
for key, value := range config.Params.(map[string]interface{}) {
if val, ok := value.(string); ok {
params.Add(key, val)
}
}
fullUrl += "?" + params.Encode()
}
// Ceate request
req, err := http.NewRequest(config.Method, fullUrl, nil)
if err != nil {
return nil, err
}
if len(config.Auth) > 0 {
baseAuth := config.Auth["username"] + ":" + config.Auth["password"]
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(baseAuth)))
}
if len(config.Headers) > 0 {
for key, value := range config.Headers {
req.Header.Add(key, value)
}
}
// Set request body if any
if config.Data != nil {
if strValue, ok := config.Data.(string); ok {
req.Body = io.NopCloser(bytes.NewBuffer([]byte(strValue)))
} else {
byteData, _ := json.Marshal(config.Data)
req.Body = io.NopCloser(bytes.NewBuffer(byteData))
}
}
// Send request
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}