request.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released
  2. // under the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for
  3. // details. Resist intellectual serfdom - the ownership of ideas is akin to
  4. // slavery.
  5. package napping
  6. import (
  7. "bytes"
  8. "encoding/json"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. "time"
  13. )
  14. // A Params is a map containing URL parameters.
  15. type Params map[string]string
  16. // AsUrlValues converts Params to url.Values
  17. func (p Params) AsUrlValues() url.Values {
  18. result := url.Values{}
  19. for key, value := range p {
  20. result.Set(key, value)
  21. }
  22. return result
  23. }
  24. // A Request describes an HTTP request to be executed, data structures into
  25. // which the result will be unmarshaled, and the server's response. By using
  26. // a single object for both the request and the response we allow easy access
  27. // to Result and Error objects without needing type assertions.
  28. type Request struct {
  29. Url string // Raw URL string
  30. Method string // HTTP method to use
  31. Params *url.Values // URL query parameters
  32. Payload interface{} // Data to JSON-encode and POST
  33. // Can be set to true if Payload is of type *bytes.Buffer and client wants
  34. // to send it as-is
  35. RawPayload bool
  36. // Result is a pointer to a data structure. On success (HTTP status < 300),
  37. // response from server is unmarshaled into Result.
  38. Result interface{}
  39. // CaptureResponseBody can be set to capture the response body for external use.
  40. CaptureResponseBody bool
  41. // ResponseBody exports the raw response body if CaptureResponseBody is true.
  42. ResponseBody *bytes.Buffer
  43. // Error is a pointer to a data structure. On error (HTTP status >= 300),
  44. // response from server is unmarshaled into Error.
  45. Error interface{}
  46. // Optional
  47. Userinfo *url.Userinfo
  48. Header *http.Header
  49. // Custom Transport if needed.
  50. Transport *http.Transport
  51. // The following fields are populated by Send().
  52. timestamp time.Time // Time when HTTP request was sent
  53. status int // HTTP status for executed request
  54. response *http.Response // Response object from http package
  55. body []byte // Body of server's response (JSON or otherwise)
  56. }
  57. // A Response is a Request object that has been executed.
  58. type Response Request
  59. // Timestamp returns the time when HTTP request was sent.
  60. func (r *Response) Timestamp() time.Time {
  61. return r.timestamp
  62. }
  63. // RawText returns the body of the server's response as raw text.
  64. func (r *Response) RawText() string {
  65. return strings.TrimSpace(string(r.body))
  66. }
  67. // Status returns the HTTP status for the executed request, or 0 if request has
  68. // not yet been sent.
  69. func (r *Response) Status() int {
  70. return r.status
  71. }
  72. // HttpResponse returns the underlying Response object from http package.
  73. func (r *Response) HttpResponse() *http.Response {
  74. return r.response
  75. }
  76. // Unmarshal parses the JSON-encoded data in the server's response, and stores
  77. // the result in the value pointed to by v.
  78. func (r *Response) Unmarshal(v interface{}) error {
  79. return json.Unmarshal(r.body, v)
  80. }