package jdSdk import ( "encoding/json" ) //SDK is sdk for jdSdk 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 jdSdk 精选列表 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 } //转链 func (s *SDK) ConventUrl(r *RequestJD) { d, err := ConventUrl(r) if err != nil { s.err = err } s.response = d } func (s *SDK) OrderFollow(r *RequestJD) { d, err := OrderFollow(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) sdk.Register("convent_url", sdk.ConventUrl) sdk.Register("order_follow", sdk.OrderFollow) return sdk }