add tcgplayer api action

This commit is contained in:
Jay 2019-01-14 18:06:10 +08:00
parent 2d18bb2eec
commit 37592a43fc
3 changed files with 103 additions and 0 deletions

View File

@ -15,5 +15,14 @@ type RequestObject struct {
// GetRequest - // GetRequest -
func GetRequest(r RequestObject) (req *http.Request, err error) { func GetRequest(r RequestObject) (req *http.Request, err error) {
req, err = http.NewRequest(r.Method, r.URL, r.Body)
if err != nil {
return
}
if len(r.Headers) > 0 {
for k, v := range r.Headers {
req.Header.Set(k, v)
}
}
return return
} }

View File

@ -0,0 +1,90 @@
package apiact
import (
"fmt"
"net/url"
"time"
)
// TCGPlayer -
type TCGPlayer struct {
URL string
APIVersion string
AccessToken string
UserName string
Expire time.Time
}
func (p *TCGPlayer) getAPIURL(urlPath string) (apiURL string, err error) {
u, err := url.Parse(p.URL)
if err != nil {
return "", err
}
u, err = u.Parse(p.APIVersion)
if err != nil {
return "", err
}
u, err = u.Parse(urlPath)
if err != nil {
return "", err
}
return u.String(), nil
}
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)
return m
}
// NewTCGApi -
func NewTCGApi(apiVersion string) (api *TCGPlayer, err error) {
if len(apiVersion) == 0 {
apiVersion = "1.19.0"
}
api = &TCGPlayer{}
api.APIVersion = apiVersion
api.URL = "https://api.tcgplayer.com"
return
}
// GetToken -
func (p *TCGPlayer) GetToken() (err error) {
apiURL, err := p.getAPIURL("/token")
_ = apiURL
return
}
// TCGPlayerCategory -
type TCGPlayerCategory struct {
CaegoryID int `json:"categoryId"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
ModifiedOn time.Time `json:"modifiedOn"`
SEOCategoryName string `json:"seoCategoryName"`
SealedLabel string `json:"sealedLabel"`
NonSealedLabel string `json:"nonSealedLabel"`
ConditionGuideURL string `json:"conditionGuideUrl"`
IsScannable bool `json:"isScannable"`
Popularity int `json:"popularity"`
}
// ListCategory -
func (p *TCGPlayer) ListCategory(limit, offset int) (category []*TCGPlayerCategory, err error) {
apiURL, err := p.getAPIURL("/catalog/categories")
if err != nil {
return nil, err
}
_ = apiURL
tmpStruct := struct {
Success string `json:"success"`
Errors []string `json:"errors"`
Results []TCGPlayerCategory `json:"results"`
}{}
_ = tmpStruct
return
}

View File

@ -29,6 +29,10 @@ type Config struct {
Port int `yaml:"port"` Port int `yaml:"port"`
Prefix string `yaml:"prefix"` Prefix string `yaml:"prefix"`
} }
TCGPlayer struct {
PublicKey string `yaml:"public_key"`
PrivateKey string `yaml:"private_key"`
}
} }
var conf *Config var conf *Config