api.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "time"
  8. )
  9. // Client represents the client for the Porter API
  10. type Client struct {
  11. BaseURL string
  12. HTTPClient *http.Client
  13. Cookie *http.Cookie
  14. CookieFilename string
  15. }
  16. // HTTPError is the Porter error response returned if a request fails
  17. type HTTPError struct {
  18. Code uint `json:"code"`
  19. Errors []string `json:"errors"`
  20. }
  21. // NewClient constructs a new client based on a set of options
  22. func NewClient(baseURL string, cookieFileName string) *Client {
  23. client := &Client{
  24. BaseURL: baseURL,
  25. CookieFilename: cookieFileName,
  26. HTTPClient: &http.Client{
  27. Timeout: time.Minute,
  28. },
  29. }
  30. cookie, _ := client.getCookie()
  31. if cookie != nil {
  32. client.Cookie = cookie
  33. }
  34. return client
  35. }
  36. func (c *Client) sendRequest(req *http.Request, v interface{}) (*HTTPError, error) {
  37. req.Header.Set("Content-Type", "application/json; charset=utf-8")
  38. req.Header.Set("Accept", "application/json; charset=utf-8")
  39. if cookie, _ := c.getCookie(); cookie != nil {
  40. c.Cookie = cookie
  41. req.AddCookie(c.Cookie)
  42. }
  43. res, err := c.HTTPClient.Do(req)
  44. if err != nil {
  45. return nil, err
  46. }
  47. defer res.Body.Close()
  48. if cookies := res.Cookies(); len(cookies) == 1 {
  49. c.saveCookie(cookies[0])
  50. }
  51. if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
  52. var errRes HTTPError
  53. if err = json.NewDecoder(res.Body).Decode(&errRes); err == nil {
  54. return &errRes, nil
  55. }
  56. return nil, fmt.Errorf("unknown error, status code: %d", res.StatusCode)
  57. }
  58. if v != nil {
  59. if err = json.NewDecoder(res.Body).Decode(v); err != nil {
  60. return nil, err
  61. }
  62. }
  63. return nil, nil
  64. }
  65. // CookieStorage for temporary fs-based cookie storage before jwt tokens
  66. type CookieStorage struct {
  67. Cookie *http.Cookie `json:"cookie"`
  68. }
  69. // saves single cookie to file
  70. func (c *Client) saveCookie(cookie *http.Cookie) error {
  71. data, err := json.Marshal(&CookieStorage{
  72. Cookie: cookie,
  73. })
  74. if err != nil {
  75. return err
  76. }
  77. return ioutil.WriteFile(c.CookieFilename, data, 0644)
  78. }
  79. // retrieves single cookie from file
  80. func (c *Client) getCookie() (*http.Cookie, error) {
  81. data, err := ioutil.ReadFile(c.CookieFilename)
  82. if err != nil {
  83. return nil, err
  84. }
  85. cookie := &CookieStorage{}
  86. err = json.Unmarshal(data, cookie)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return cookie.Cookie, nil
  91. }