add tcgplayer get category api , get category groups api

This commit is contained in:
Jay 2019-01-15 15:56:01 +08:00
parent 37592a43fc
commit 97af1343d4
3 changed files with 195 additions and 11 deletions

View File

@ -15,3 +15,7 @@ redis:
host: localhost
port: 6379
prefix:
tcgplayer:
public_key:
private_key:

View File

@ -1,9 +1,17 @@
package apiact
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"git.trj.tw/golang/mtgbot/modules/config"
)
// TCGPlayer -
@ -15,7 +23,14 @@ type TCGPlayer struct {
Expire time.Time
}
func (p *TCGPlayer) getAPIURL(urlPath string) (apiURL string, err error) {
// TCGPlayerAPIRes -
type TCGPlayerAPIRes struct {
Success bool `json:"success"`
Errors []string `json:"errors"`
Results []map[string]interface{} `json:"results"`
}
func (p *TCGPlayer) getAPIURL(urlPath string, requireVersion bool) (apiURL string, err error) {
u, err := url.Parse(p.URL)
if err != nil {
return "", err
@ -36,7 +51,9 @@ func (p *TCGPlayer) getHeader() map[string]string {
m := make(map[string]string)
m["Content-Type"] = "application/json"
if len(p.AccessToken) > 0 {
m["Authorization"] = fmt.Sprintf("Bearer %s", p.AccessToken)
}
return m
}
@ -54,8 +71,52 @@ func NewTCGApi(apiVersion string) (api *TCGPlayer, err error) {
// GetToken -
func (p *TCGPlayer) GetToken() (err error) {
apiURL, err := p.getAPIURL("/token")
_ = apiURL
conf := config.GetConf()
apiURL, err := p.getAPIURL("/token", false)
dataReader := strings.NewReader(fmt.Sprintf("grant_type=client_credentials&client_id=%s&client_secret=%s", conf.TCGPlayer.PublicKey, conf.TCGPlayer.PrivateKey))
reqObj := RequestObject{}
reqObj.Method = "POST"
reqObj.URL = apiURL
reqObj.Body = dataReader
reqObj.Headers = p.getHeader()
req, err := GetRequest(reqObj)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
tokenStruct := struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
UserName string `json:"userName"`
Issued string `json:".issued"`
Exipres string `json:"expires"`
}{}
respByte, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(respByte, &tokenStruct)
if err != nil {
return err
}
if len(tokenStruct.AccessToken) == 0 {
return errors.New("get access token fail")
}
p.AccessToken = tokenStruct.AccessToken
return
}
@ -75,16 +136,135 @@ type TCGPlayerCategory struct {
// ListCategory -
func (p *TCGPlayer) ListCategory(limit, offset int) (category []*TCGPlayerCategory, err error) {
apiURL, err := p.getAPIURL("/catalog/categories")
apiURL, err := p.getAPIURL("/catalog/categories", true)
if err != nil {
return nil, err
}
_ = apiURL
tmpStruct := struct {
Success string `json:"success"`
Errors []string `json:"errors"`
Results []TCGPlayerCategory `json:"results"`
TCGPlayerAPIRes
Results []*TCGPlayerCategory `json:"results"`
}{}
if limit < 1 {
limit = 1
}
if offset < 0 {
offset = 0
}
qsVal := url.Values{}
qsVal.Add("limit", strconv.Itoa(limit))
qsVal.Add("offset", strconv.Itoa(offset))
apiURL += fmt.Sprintf("?%s", qsVal.Encode())
reqObj := RequestObject{
Method: "GET",
Headers: p.getHeader(),
URL: apiURL,
}
req, err := GetRequest(reqObj)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respByte, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(respByte, &tmpStruct)
if err != nil {
return nil, err
}
if tmpStruct.Success != true {
if len(tmpStruct.Errors) > 0 {
return nil, errors.New(tmpStruct.Errors[0])
}
return nil, errors.New("get category fail")
}
return tmpStruct.Results, nil
}
// TCGPlayerCategoryGroup -
type TCGPlayerCategoryGroup struct {
GroupID int `json:"groupId"`
Name string `json:"name"`
Abbreviation string `json:"abbreviation"`
Supplemental bool `json:"supplemental"`
PublishedOn string `json:"publishedOn"`
ModifiedOn time.Time `json:"modifiedOn"`
Category TCGPlayerCategory `json:"category"`
}
// ListCategoryGroups -
func (p *TCGPlayer) ListCategoryGroups(categoryID, limit, offset int) (groups []*TCGPlayerCategoryGroup, err error) {
apiURL, err := p.getAPIURL(fmt.Sprintf("/catalog/categories/%d", categoryID), true)
if err != nil {
return nil, err
}
if limit < 1 {
limit = 1
}
if offset < 0 {
offset = 0
}
qsVal := url.Values{}
qsVal.Add("limit", strconv.Itoa(limit))
qsVal.Add("offset", strconv.Itoa(offset))
apiURL += fmt.Sprintf("?%s", qsVal.Encode())
tmpStruct := struct {
TCGPlayerAPIRes
TotalItems int `json:"totalItems"`
Results []*TCGPlayerCategoryGroup `json:"results"`
}{}
_ = tmpStruct
return
reqObj := RequestObject{
Method: "GET",
Headers: p.getHeader(),
URL: apiURL,
}
req, err := GetRequest(reqObj)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respByte, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(respByte, &tmpStruct)
if err != nil {
return nil, err
}
if tmpStruct.Success != true {
if len(tmpStruct.Errors) > 0 {
return nil, errors.New(tmpStruct.Errors[0])
}
return nil, errors.New("get category groups fail")
}
return tmpStruct.Results, nil
}

View File

@ -32,7 +32,7 @@ type Config struct {
TCGPlayer struct {
PublicKey string `yaml:"public_key"`
PrivateKey string `yaml:"private_key"`
}
} `yaml:"tcgplayer"`
}
var conf *Config