From 97af1343d451892267bded39ea6145040d934e1d Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 15 Jan 2019 15:56:01 +0800 Subject: [PATCH] add tcgplayer get category api , get category groups api --- config.sample.yml | 4 + modules/apiact/tcgplayer.go | 200 ++++++++++++++++++++++++++++++++++-- modules/config/config.go | 2 +- 3 files changed, 195 insertions(+), 11 deletions(-) diff --git a/config.sample.yml b/config.sample.yml index 412a73b..069a964 100644 --- a/config.sample.yml +++ b/config.sample.yml @@ -15,3 +15,7 @@ redis: host: localhost port: 6379 prefix: + +tcgplayer: + public_key: + private_key: diff --git a/modules/apiact/tcgplayer.go b/modules/apiact/tcgplayer.go index 6b130f9..e4027da 100644 --- a/modules/apiact/tcgplayer.go +++ b/modules/apiact/tcgplayer.go @@ -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" - m["Authorization"] = fmt.Sprintf("Bearer %s", p.AccessToken) + 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 } diff --git a/modules/config/config.go b/modules/config/config.go index cf4724f..226dcff 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -32,7 +32,7 @@ type Config struct { TCGPlayer struct { PublicKey string `yaml:"public_key"` PrivateKey string `yaml:"private_key"` - } + } `yaml:"tcgplayer"` } var conf *Config