Skip to content

Commit

Permalink
优化 batch 处理方式
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoMengXinX committed Oct 30, 2021
1 parent a8d2c61 commit 6d384cc
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions api/Batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
type Batch struct {
API map[string]interface{}
Result string
Header http.Header
Error error
}

// BatchAPI 被批处理的 API
Expand All @@ -26,35 +28,35 @@ func (b *Batch) Add(apis ...BatchAPI) {
}

// Do 请求批处理 API
func (b *Batch) Do(data utils.RequestData) (bodyJson string, heeader http.Header, err error) {
func (b *Batch) Do(data utils.RequestData) *Batch {
reqBodyJson, err := json.Marshal(b.API)
if err != nil {
return bodyJson, heeader, err
b.Error = err
return b
}
var options utils.EapiOption
options.Path = "/api/batch"
options.Url = "https://music.163.com/eapi/batch"
options.Json = string(reqBodyJson)
bodyJson, heeader, err = utils.EapiRequest(options, data)
b.Result = bodyJson
return bodyJson, heeader, err
b.Result, b.Header, b.Error = utils.EapiRequest(options, data)
return b
}

// Parse 解析 Batch 的 Json 数据
func (b *Batch) Parse() map[string]string {
func (b *Batch) Parse() (*Batch, map[string]string) {
jsonData := make(map[string]interface{})
jsonMap := make(map[string]string)
_ = json.Unmarshal([]byte(b.Result), &jsonData)
for k, v := range jsonData {
jsonStr, _ := json.Marshal(v)
jsonMap[k] = string(jsonStr)
}
return jsonMap
return b, jsonMap
}

// NewBatch 新建 Batch 对象
func NewBatch(apis ...BatchAPI) Batch {
b := Batch{}
func NewBatch(apis ...BatchAPI) *Batch {
b := &Batch{}
b.API = make(map[string]interface{})
b.API["e_r"] = "true"
b.API["header"] = "{}"
Expand Down

0 comments on commit 6d384cc

Please sign in to comment.