添加mock数据的EPData接口的实现,在main中实现读取

master
kuiki 2019-02-18 18:02:03 +08:00
parent a92b7890b2
commit a74d1c6a12
3 changed files with 64 additions and 1 deletions

31
datasource.go Normal file
View File

@ -0,0 +1,31 @@
package main
// EPData 视频信息
type EPData struct {
Cid int64 `json:"cid"`
Dimension struct {
Height int64 `json:"height"`
Rotate int64 `json:"rotate"`
Width int64 `json:"width"`
} `json:"dimension"`
Duration int64 `json:"duration"`
From string `json:"from"`
Page int64 `json:"page"`
Part string `json:"part"`
Vid string `json:"vid"`
Weblink string `json:"weblink"`
}
// EPResponse 视频信息请求接口返回数据
type EPResponse struct {
Code int64 `json:"code"`
Data []EPData `json:"data"`
Message string `json:"message"`
TTL int64 `json:"ttl"`
}
// EPDataProvider 定义数据来源接口
type EPDataProvider interface {
// GetEPData 根据视频号获取视频的集数据
GetEPData(videoNo string) (epdatas []EPData, err error)
}

13
main.go
View File

@ -1,5 +1,16 @@
package main
func main() {
import (
"fmt"
)
func main() {
epProvider := new(mockProvider)
epdatas, err := epProvider.GetEPData("av1742161")
if err != nil {
panic(err)
}
for i, epdata := range epdatas {
fmt.Printf("ep %d Cid: %v, Part: %s\n", i, epdata.Cid, epdata.Part)
}
}

21
mock.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"encoding/json"
"os"
)
type mockProvider struct{}
func (mock *mockProvider) GetEPData(videoNo string) (epdatas []EPData, err error) {
file, err := os.Open("mock/" + videoNo + ".json")
if err != nil {
return epdatas, err
}
var resp EPResponse
err = json.NewDecoder(file).Decode(&resp)
if err != nil {
return epdatas, err
}
return resp.Data, nil
}