0 Star 1 Fork 0

天高/es-client-wails

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
app.go 2.69 KB
一键复制 编辑 原始数据 按行查看 历史
tg.shi 提交于 2024-10-16 20:09 . 修复高级查询查不到的问题
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tiangao/es-client-wails.git
[email protected]:tiangao/es-client-wails.git
tiangao
es-client-wails
es-client-wails
main

搜索帮助