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.

63 satır
1.3KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. )
  9. type errorHTTPResponse struct {
  10. ErrorMsg string
  11. HTTPStatusCode int
  12. HTTPHeader http.Header
  13. }
  14. func (e errorHTTPResponse) Error() string {
  15. return e.ErrorMsg
  16. }
  17. func errorHTTPResponseNew(r *http.Response, msg string) (e errorHTTPResponse) {
  18. e.ErrorMsg = msg
  19. e.HTTPStatusCode = r.StatusCode
  20. e.HTTPHeader = r.Header
  21. return e
  22. }
  23. type crmdReason struct {
  24. Reason string `json:"reason"`
  25. Data map[string]*json.RawMessage `json:"data"`
  26. }
  27. func (e errorHTTPResponse) XStatusReason() (r crmdReason) {
  28. jsonStr := e.HTTPHeader.Get("X-Status-Reason")
  29. json.Unmarshal([]byte(jsonStr), &r)
  30. return
  31. }
  32. func (m crmdReason) Data2Entity(entityType string) (r interface{}, err error) {
  33. if len(m.Data) > 1 {
  34. log.Println("Warning: Multiple data object found, only choose first one")
  35. log.Println(m)
  36. }
  37. //only take one object
  38. for _, v := range m.Data {
  39. switch entityType {
  40. case "Lead":
  41. e := crmdLead{}
  42. err = json.Unmarshal(*v, &e)
  43. r = e
  44. case "Account":
  45. //r = crmdAccount{}
  46. default:
  47. msg := fmt.Sprintf("(crmdReason) Data2Entity: Unknown EntityType %s", entityType)
  48. err = errors.New(msg)
  49. log.Fatalln(err)
  50. }
  51. break //after procesing first ojbect, we quite, neglect all the rest
  52. }
  53. return
  54. }