Golang
Updated 26/4/2024
by
lei_jiang
Parametric class package entity type CreateCloudPhoneReq struct { Quantity int `json:"quantity" validate:"required,min=1"` EnvRemark *string `json:"envRemark"` ProxyId *int32 `json:"proxyId"` Country *string `json:"country"` Language *string `json:"language"` Timezone *string `json:"timezone"` Latitude *string `json:"latitude"` Longitude *string `json:"longitude"` Altitude *string `json:"altitude"` AutomaticGeo *string `json:"automaticGeo"` AutomaticLanguage *string `json:"automaticLanguage"` AutomaticLocation *string `json:"automaticLocation"` }
package entity type Response[T any] struct { Code int `json:"code"` Data T `json:"data"` Error *Error `json:"error"` Msg string `json:"msg"` StackTrace *string `json:"stackTrace"` Status *string `json:"status"` Success bool `json:"success"` Time *string `json:"time"` } type Error struct { Info []Info `json:"info"` Type string `json:"type"` } type Info struct { Code string `json:"code"` Msg string `json:"msg"` Nested Nested `json:"nested"` Subject string `json:"subject"` } type Nested struct { // Continuing to define the fields based on the fields in the nested }
package common import ( "crypto/md5" "encoding/hex" "fmt" "math/rand" "strconv" "time" ) type ApiInfo struct { AppId string `json:"appId" validate:"required,min=1"` SecretKey string `json:"secretKey"` } func generateRandom(length int) string { b := make([]byte, length) _, err := rand.Read(b) if err != nil { return "" } return fmt.Sprintf("%x", b) } func generateNonceId() string { return strconv.FormatInt(time.Now().UnixNano(), 10) + generateRandom(6) } func md5Encode(appId string, nonceId string, secretKey string) string { h := md5.New() h.Write([]byte(appId + nonceId + secretKey)) return hex.EncodeToString(h.Sum(nil)) } func (api ApiInfo) GetRequestHeader() map[string]string { noncId := generateNonceId() headers := make(map[string]string) headers["Content-Type"] = "application/json" headers["X-Api-Id"] = api.AppId headers["Authorization"] = md5Encode(api.AppId, noncId, api.SecretKey) headers["X-Nonce-Id"] = noncId return headers }
Creating a Cloud Phone Profile package main import ( "bytes" "encoding/json" "fmt" "git.ziniao.com/A26-ZBox/a26-zbox-crawler/test/common" "git.ziniao.com/A26-ZBox/a26-zbox-crawler/test/entity" "io" "io/ioutil" "log" "mime/multipart" "net/http" "os" "path/filepath" ) const ( APPID = "xxxxxxx" SECRETKEY = "xxxxxxx" BASEURL = "http://local.morelogin.com:40000" ) func createCloudPhone() { requestPath := "/api/cloudphone/create" // todo data := entity.CreateCloudPhoneReq{ Quantity: 1, } jsonData, err := json.Marshal(data) if err != nil { log.Fatalln(err) } request, err := http.NewRequest("POST", BASEURL+requestPath, bytes.NewBuffer(jsonData)) if err != nil { return } request.Header.Add("Content-Type", "application/json") apiInfo := common.ApiInfo{ AppId: APPID, SecretKey: SECRETKEY, } for k, v := range apiInfo.GetRequestHeader() { request.Header.Add(k, v) } client := &http.Client{} response, err := client.Do(request) if err != nil { return } defer response.Body.Close() responseData, err := ioutil.ReadAll(response.Body) if err != nil { return } var result entity.Response[interface{}] json.Unmarshal(responseData, &result) println(responseData) if result.Code == 0 { fmt.Println("success") } } func main() { createCloudPhone() }
Uploading files package main import ( "bytes" "encoding/json" "fmt" "git.ziniao.com/A26-ZBox/a26-zbox-crawler/test/common" "git.ziniao.com/A26-ZBox/a26-zbox-crawler/test/entity" "io" "io/ioutil" "log" "mime/multipart" "net/http" "os" "path/filepath" ) const ( APPID = "1563710884577281" SECRETKEY = "c041f28d3f6047f4884a84b8620e359a" BASEURL = "http://local.morelogin.com:40000" ) func uploadFile() { requestPath := "/api/cloudphone/uploadFile" // todo filePath := "D:\\tmp\\untitled.xlsx" file, err := os.Open(filePath) if err != nil { return } defer file.Close() body := &bytes.Buffer{} writer := multipart.NewWriter(body) part, err := writer.CreateFormFile("file", filepath.Base(file.Name())) if err != nil { return } io.Copy(part, file) writer.WriteField("id", "1563663106187706") writer.Close() request, err := http.NewRequest("POST", BASEURL+requestPath, body) if err != nil { return } request.Header.Add("Content-Type", writer.FormDataContentType()) apiInfo := common.ApiInfo{ AppId: APPID, SecretKey: SECRETKEY, } for k, v := range apiInfo.GetRequestHeader() { request.Header.Add(k, v) } client := &http.Client{} response, err := client.Do(request) if err != nil { return } defer response.Body.Close() responseData, err := ioutil.ReadAll(response.Body) if err != nil { return } var result entity.Response[interface{}] json.Unmarshal(responseData, &result) if result.Code == 0 { fmt.Println("success") } } func main() { uploadFile() }