From c070c02f3fcb8c2c8dfa1ffcadf72a4fb827a43a Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期三, 20 十一月 2019 14:15:34 +0800
Subject: [PATCH] back
---
extend/esutil/EsClient.go | 92 +++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 82 insertions(+), 10 deletions(-)
diff --git a/extend/esutil/EsClient.go b/extend/esutil/EsClient.go
index 20f3fa7..0fb6e16 100644
--- a/extend/esutil/EsClient.go
+++ b/extend/esutil/EsClient.go
@@ -9,7 +9,6 @@
"log"
"mime/multipart"
"net/http"
- "strconv"
"strings"
"time"
@@ -45,12 +44,12 @@
sources := []interface{}{}
for _, value := range dat["hits"].([]interface{}) {
source := value.(map[string]interface{})["_source"].(map[string]interface{})
- source["id"] = value.(map[string]interface{})["_id"]
- sdkType := source["sdkType"]
+ //source["id"] = source["id"]
+ /*sdkType := source["sdkType"]
if sdkType != nil {
sdk, _ := strconv.Atoi(sdkType.(string))
source["sdkType"] = sdkTypeToValue(sdk)
- }
+ }*/
sources = append(sources, source)
}
data["datalist"] = sources
@@ -62,10 +61,10 @@
}
//sdk绫诲瀷
-func sdkTypeToValue(i int) string {
+/*func sdkTypeToValue(i int) string {
value := []string{"浜鸿劯", "杞﹁締", "浜轰綋", "鍏ヤ镜", "鎷ユ尋", "闈犲彸琛�", "浜哄憳寮傚父", "涓綋闈欐"}
return value[i-1]
-}
+}*/
func PutEsDataReq(url string, parama string) (map[string]interface{}, error) {
//fmt.Println("es 鏌ヨ璇锋眰璺緞" + url) // 閰嶇疆淇℃伅 鑾峰彇
@@ -99,7 +98,7 @@
return dat, nil
}
-func PostFormData(uri string, filename, paramName string, file multipart.File) (maps map[string]interface{}, err0 error) {
+func PostFormData(uri string, filename, paramName string, fileBytes []byte) (maps map[string]interface{}, err0 error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
_, err := writer.CreateFormFile(paramName, filename)
@@ -107,11 +106,11 @@
return nil, err
}
boundary := writer.Boundary()
- //close_string := fmt.Sprintf("\r\n--%s--\r\n", boundary)
close_buf := bytes.NewBufferString(fmt.Sprintf("\r\n--%s--\r\n", boundary))
+ fmt.Println("PostFormData.fileLen:", len(fileBytes))
+ file := bytes.NewBuffer(fileBytes)
request_reader := io.MultiReader(body, file, close_buf)
- //_, err = io.Copy(part, file)
- //writer.WriteField(key, val)
+
request, err := http.NewRequest("POST", uri, request_reader)
request.Header.Add("Content-Type", writer.FormDataContentType())
timeout := time.Duration(5 * time.Second) //瓒呮椂鏃堕棿50ms
@@ -246,3 +245,76 @@
return dat
}
}
+
+//瑙f瀽http
+func EsReq(method string, url string, parama []byte) (buf []byte, err error) {
+ timeout := time.Duration(10 * time.Second)
+ client := http.Client{
+ Timeout: timeout,
+ }
+ request, err := http.NewRequest(method, url, bytes.NewBuffer(parama))
+ request.Header.Set("Content-type", "application/json")
+
+ if err != nil {
+ fmt.Println("build request fail !")
+ return nil, err
+ }
+
+ resp, err := client.Do(request)
+ if err != nil {
+ fmt.Println("request error: ", err)
+ return nil, err
+ }
+
+ defer resp.Body.Close()
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ fmt.Println(err)
+ return nil, err
+ }
+ return body, nil
+}
+
+//瑙f瀽json
+func Sourcelist(buf []byte) (sources []map[string]interface{}, err error) {
+ var info interface{}
+ json.Unmarshal(buf, &info)
+ out, ok := info.(map[string]interface{})
+ if !ok {
+ return nil, errors.New("http response interface can not change map[string]interface{}")
+ }
+
+ middle, ok := out["hits"].(map[string]interface{})
+ if !ok {
+ return nil, errors.New("first hits change error!")
+ }
+ for _, in := range middle["hits"].([]interface{}) {
+ tmpbuf, ok := in.(map[string]interface{})
+ if !ok {
+ fmt.Println("change to source error!")
+ continue
+ }
+ source, ok := tmpbuf["_source"].(map[string]interface{})
+ if !ok {
+ fmt.Println("change _source error!")
+ continue
+ }
+ sources = append(sources, source)
+ }
+ return sources, nil
+}
+
+
+func HttpGet(str string) ([]byte){
+ resp, err := http.Get(str)
+ if err != nil {
+ // handle error
+ }
+
+ defer resp.Body.Close()
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ // handle error
+ }
+ return body
+}
\ No newline at end of file
--
Gitblit v1.8.0