第三方api接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
1.6 KiB

  1. package jd
  2. import (
  3. "encoding/json"
  4. )
  5. //SDK is sdk for jd
  6. type SDK struct {
  7. response []byte
  8. fmap map[string]func(*RequestJD)
  9. fname string
  10. err error
  11. }
  12. //Init is init sdk
  13. func (s *SDK) Init() {
  14. s.fmap = make(map[string]func(*RequestJD))
  15. }
  16. //SelectFunction is select func
  17. func (s *SDK) SelectFunction(fname string) *SDK {
  18. s.fname = fname
  19. return s
  20. }
  21. //WithArgs is request args
  22. func (s *SDK) WithArgs(args *RequestJD) *SDK {
  23. f := s.fmap[s.fname]
  24. f(args)
  25. return s
  26. }
  27. // Result is func result
  28. func (s *SDK) Result() (*SDK, error) {
  29. return s, s.err
  30. }
  31. // Register is register func
  32. func (s *SDK) Register(name string, f func(*RequestJD)) {
  33. s.fmap[name] = f
  34. }
  35. //ToBytes is return []byte
  36. func (s *SDK) ToBytes() []byte {
  37. return s.response
  38. }
  39. // ToString is return string
  40. func (s *SDK) ToString() string {
  41. return string(s.response)
  42. }
  43. //ToMapInterface is return map[string]interface{}
  44. func (s *SDK) ToMapInterface() map[string]interface{} {
  45. m := make(map[string]interface{})
  46. json.Unmarshal(s.response, &m)
  47. return m
  48. }
  49. // Featured is jd 精选列表
  50. func (s *SDK) Featured(r *RequestJD) {
  51. d, err := Featured(r)
  52. if err != nil {
  53. s.err = err
  54. }
  55. s.response = d
  56. }
  57. // Guess is 猜你喜欢列表
  58. func (s *SDK) Guess(r *RequestJD) {
  59. d, err := Guess(r)
  60. if err != nil {
  61. s.err = err
  62. }
  63. s.response = d
  64. }
  65. // Recommend is 推荐列表
  66. func (s *SDK) Recommend(r *RequestJD) {
  67. d, err := Recommend(r)
  68. if err != nil {
  69. s.err = err
  70. }
  71. s.response = d
  72. }
  73. //NewJDSDK is jingdong sdk
  74. func NewJDSDK() *SDK {
  75. sdk := new(SDK)
  76. sdk.Init()
  77. sdk.Register("featured", sdk.Featured)
  78. sdk.Register("guess", sdk.Guess)
  79. sdk.Register("recommend", sdk.Recommend)
  80. return sdk
  81. }