|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package jd
-
- import (
- "encoding/json"
- )
-
- //SDK is sdk for jd
- type SDK struct {
- response []byte
- fmap map[string]func(*RequestJD)
- fname string
- err error
- }
-
- //Init is init sdk
- func (s *SDK) Init() {
- s.fmap = make(map[string]func(*RequestJD))
- }
-
- //SelectFunction is select func
- func (s *SDK) SelectFunction(fname string) *SDK {
- s.fname = fname
- return s
- }
-
- //WithArgs is request args
- func (s *SDK) WithArgs(args *RequestJD) *SDK {
- f := s.fmap[s.fname]
- f(args)
- return s
- }
-
- // Result is func result
- func (s *SDK) Result() (*SDK, error) {
- return s, s.err
- }
-
- // Register is register func
- func (s *SDK) Register(name string, f func(*RequestJD)) {
- s.fmap[name] = f
- }
-
- //ToBytes is return []byte
- func (s *SDK) ToBytes() []byte {
- return s.response
- }
-
- // ToString is return string
- func (s *SDK) ToString() string {
- return string(s.response)
- }
-
- //ToMapInterface is return map[string]interface{}
- func (s *SDK) ToMapInterface() map[string]interface{} {
- m := make(map[string]interface{})
- json.Unmarshal(s.response, &m)
- return m
- }
-
- // Featured is jd 精选列表
- func (s *SDK) Featured(r *RequestJD) {
- d, err := Featured(r)
- if err != nil {
- s.err = err
- }
- s.response = d
- }
-
- // Guess is 猜你喜欢列表
- func (s *SDK) Guess(r *RequestJD) {
- d, err := Guess(r)
- if err != nil {
- s.err = err
- }
- s.response = d
- }
-
- // Recommend is 推荐列表
- func (s *SDK) Recommend(r *RequestJD) {
- d, err := Recommend(r)
- if err != nil {
- s.err = err
- }
- s.response = d
- }
-
- //NewJDSDK is jingdong sdk
- func NewJDSDK() *SDK {
- sdk := new(SDK)
- sdk.Init()
- sdk.Register("featured", sdk.Featured)
- sdk.Register("guess", sdk.Guess)
- sdk.Register("recommend", sdk.Recommend)
- return sdk
- }
|