restclient.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright (c) 2016 VMware, Inc. All Rights Reserved.
  2. //
  3. // This product is licensed to you under the Apache License, Version 2.0 (the "License").
  4. // You may not use this product except in compliance with the License.
  5. //
  6. // This product may include a number of subcomponents with separate copyright notices and
  7. // license terms. Your use of these subcomponents is subject to the terms and conditions
  8. // of the subcomponent's license, as noted in the LICENSE file.
  9. package photon
  10. import (
  11. "bytes"
  12. "crypto/rand"
  13. "encoding/json"
  14. "fmt"
  15. "io"
  16. "io/ioutil"
  17. "log"
  18. "net/http"
  19. "os"
  20. "path/filepath"
  21. "strings"
  22. )
  23. type restClient struct {
  24. httpClient *http.Client
  25. logger *log.Logger
  26. Auth *AuthAPI
  27. UpdateAccessTokenCallback TokenCallback
  28. }
  29. type request struct {
  30. Method string
  31. URL string
  32. ContentType string
  33. Body io.Reader
  34. Tokens *TokenOptions
  35. }
  36. type page struct {
  37. Items []interface{} `json:"items"`
  38. NextPageLink string `json:"nextPageLink"`
  39. PreviousPageLink string `json:"previousPageLink"`
  40. }
  41. type documentList struct {
  42. Items []interface{}
  43. }
  44. type bodyRewinder func() io.Reader
  45. const appJson string = "application/json"
  46. // Root URL specifies the API version.
  47. const rootUrl string = "/v1"
  48. // From https://golang.org/src/mime/multipart/writer.go
  49. var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
  50. func (client *restClient) AppendSlice(origSlice []interface{}, dataToAppend []interface{}) []interface{} {
  51. origLen := len(origSlice)
  52. newLen := origLen + len(dataToAppend)
  53. if newLen > cap(origSlice) {
  54. newSlice := make([]interface{}, (newLen+1)*2)
  55. copy(newSlice, origSlice)
  56. origSlice = newSlice
  57. }
  58. origSlice = origSlice[0:newLen]
  59. copy(origSlice[origLen:newLen], dataToAppend)
  60. return origSlice
  61. }
  62. func (client *restClient) Get(url string, tokens *TokenOptions) (res *http.Response, err error) {
  63. req := request{"GET", url, "", nil, tokens}
  64. res, err = client.SendRequest(&req, nil)
  65. return
  66. }
  67. func (client *restClient) GetList(endpoint string, url string, tokens *TokenOptions) (result []byte, err error) {
  68. req := request{"GET", url, "", nil, tokens}
  69. res, err := client.SendRequest(&req, nil)
  70. if err != nil {
  71. return
  72. }
  73. res, err = getError(res)
  74. if err != nil {
  75. return
  76. }
  77. decoder := json.NewDecoder(res.Body)
  78. decoder.UseNumber()
  79. page := &page{}
  80. err = decoder.Decode(page)
  81. if err != nil {
  82. return
  83. }
  84. documentList := &documentList{}
  85. documentList.Items = client.AppendSlice(documentList.Items, page.Items)
  86. for page.NextPageLink != "" {
  87. req = request{"GET", endpoint + page.NextPageLink, "", nil, tokens}
  88. res, err = client.SendRequest(&req, nil)
  89. if err != nil {
  90. return
  91. }
  92. res, err = getError(res)
  93. if err != nil {
  94. return
  95. }
  96. decoder = json.NewDecoder(res.Body)
  97. decoder.UseNumber()
  98. page.NextPageLink = ""
  99. page.PreviousPageLink = ""
  100. err = decoder.Decode(page)
  101. if err != nil {
  102. return
  103. }
  104. documentList.Items = client.AppendSlice(documentList.Items, page.Items)
  105. }
  106. result, err = json.Marshal(documentList)
  107. return
  108. }
  109. func (client *restClient) Post(url string, contentType string, body io.ReadSeeker, tokens *TokenOptions) (res *http.Response, err error) {
  110. if contentType == "" {
  111. contentType = appJson
  112. }
  113. req := request{"POST", url, contentType, body, tokens}
  114. rewinder := func() io.Reader {
  115. body.Seek(0, 0)
  116. return body
  117. }
  118. res, err = client.SendRequest(&req, rewinder)
  119. return
  120. }
  121. func (client *restClient) Patch(url string, contentType string, body io.ReadSeeker, tokens *TokenOptions) (res *http.Response, err error) {
  122. if contentType == "" {
  123. contentType = appJson
  124. }
  125. req := request{"PATCH", url, contentType, body, tokens}
  126. rewinder := func() io.Reader {
  127. body.Seek(0, 0)
  128. return body
  129. }
  130. res, err = client.SendRequest(&req, rewinder)
  131. return
  132. }
  133. func (client *restClient) Put(url string, contentType string, body io.ReadSeeker, tokens *TokenOptions) (res *http.Response, err error) {
  134. if contentType == "" {
  135. contentType = appJson
  136. }
  137. req := request{"PUT", url, contentType, body, tokens}
  138. rewinder := func() io.Reader {
  139. body.Seek(0, 0)
  140. return body
  141. }
  142. res, err = client.SendRequest(&req, rewinder)
  143. return
  144. }
  145. func (client *restClient) Delete(url string, tokens *TokenOptions) (res *http.Response, err error) {
  146. req := request{"DELETE", url, "", nil, tokens}
  147. res, err = client.SendRequest(&req, nil)
  148. return
  149. }
  150. func (client *restClient) SendRequest(req *request, bodyRewinder bodyRewinder) (res *http.Response, err error) {
  151. res, err = client.sendRequestHelper(req)
  152. // In most cases, we'll return immediately
  153. // If the operation succeeded, but we got a 401 response and if we're using
  154. // authentication, then we'll look into the body to see if the token expired
  155. if err != nil {
  156. return res, err
  157. }
  158. if res.StatusCode != 401 {
  159. // It's not a 401, so the token didn't expire
  160. return res, err
  161. }
  162. if req.Tokens == nil || req.Tokens.AccessToken == "" {
  163. // We don't have a token, so we can't renew the token, no need to proceed
  164. return res, err
  165. }
  166. // We're going to look in the body to see if it failed because the token expired
  167. // This means we need to read the body, but the functions that call us also
  168. // expect to read the body. So we read the body, then create a new reader
  169. // so they can read the body as normal.
  170. body, err := ioutil.ReadAll(res.Body)
  171. if err != nil {
  172. return res, err
  173. }
  174. res.Body = ioutil.NopCloser(bytes.NewReader(body))
  175. // Now see if we had an expired token or not
  176. var apiError ApiError
  177. err = json.Unmarshal(body, &apiError)
  178. if err != nil {
  179. return res, err
  180. }
  181. if apiError.Code != "ExpiredAuthToken" {
  182. return res, nil
  183. }
  184. // We were told that the access token expired, so try to renew it.
  185. // Note that this looks recursive because GetTokensByRefreshToken() will
  186. // call the /auth API, and therefore SendRequest(). However, it calls
  187. // without a token, so we avoid having a loop
  188. newTokens, err := client.Auth.GetTokensByRefreshToken(req.Tokens.RefreshToken)
  189. if err != nil {
  190. return res, err
  191. }
  192. req.Tokens.AccessToken = newTokens.AccessToken
  193. if client.UpdateAccessTokenCallback != nil {
  194. client.UpdateAccessTokenCallback(newTokens.AccessToken)
  195. }
  196. if req.Body != nil && bodyRewinder != nil {
  197. req.Body = bodyRewinder()
  198. }
  199. res, err = client.sendRequestHelper(req)
  200. return res, nil
  201. }
  202. func (client *restClient) sendRequestHelper(req *request) (res *http.Response, err error) {
  203. r, err := http.NewRequest(req.Method, req.URL, req.Body)
  204. if err != nil {
  205. client.logger.Printf("An error occured creating request %s on %s. Error: %s", req.Method, req.URL, err)
  206. return
  207. }
  208. if req.ContentType != "" {
  209. r.Header.Add("Content-Type", req.ContentType)
  210. }
  211. if req.Tokens != nil && req.Tokens.AccessToken != "" {
  212. r.Header.Add("Authorization", "Bearer "+req.Tokens.AccessToken)
  213. }
  214. res, err = client.httpClient.Do(r)
  215. if err != nil {
  216. client.logger.Printf("An error occured when calling %s on %s. Error: %s", req.Method, req.URL, err)
  217. return
  218. }
  219. client.logger.Printf("[%s] %s - %s %s", res.Header.Get("request-id"), res.Status, req.Method, req.URL)
  220. return
  221. }
  222. func (client *restClient) MultipartUploadFile(url, filePath string, params map[string]string, tokens *TokenOptions) (res *http.Response, err error) {
  223. file, err := os.Open(filePath)
  224. if err != nil {
  225. return
  226. }
  227. defer file.Close()
  228. return client.MultipartUpload(url, file, filepath.Base(filePath), params, tokens)
  229. }
  230. func (client *restClient) MultipartUpload(url string, reader io.ReadSeeker, filename string, params map[string]string, tokens *TokenOptions) (res *http.Response, err error) {
  231. boundary := client.randomBoundary()
  232. multiReader, contentType := client.createMultiReader(reader, filename, params, boundary)
  233. rewinder := func() io.Reader {
  234. reader.Seek(0, 0)
  235. multiReader, _ := client.createMultiReader(reader, filename, params, boundary)
  236. return multiReader
  237. }
  238. res, err = client.SendRequest(&request{"POST", url, contentType, multiReader, tokens}, rewinder)
  239. return
  240. }
  241. func (client *restClient) createMultiReader(reader io.ReadSeeker, filename string, params map[string]string, boundary string) (io.Reader, string) {
  242. // The mime/multipart package does not support streaming multipart data from disk,
  243. // at least not without complicated, problematic goroutines that simultaneously read/write into a buffer.
  244. // A much easier approach is to just construct the multipart request by hand, using io.MultiPart to
  245. // concatenate the parts of the request together into a single io.Reader.
  246. parts := []io.Reader{}
  247. // Create a part for each key, val pair in params
  248. for k, v := range params {
  249. parts = append(parts, client.createFieldPart(k, v, boundary))
  250. }
  251. start := fmt.Sprintf("\r\n--%s\r\n", boundary)
  252. start += fmt.Sprintf("Content-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n", quoteEscaper.Replace(filename))
  253. start += fmt.Sprintf("Content-Type: application/octet-stream\r\n\r\n")
  254. end := fmt.Sprintf("\r\n--%s--", boundary)
  255. // The request will consist of a reader to begin the request, a reader which points
  256. // to the file data on disk, and a reader containing the closing boundary of the request.
  257. parts = append(parts, strings.NewReader(start), reader, strings.NewReader(end))
  258. contentType := fmt.Sprintf("multipart/form-data; boundary=%s", boundary)
  259. return io.MultiReader(parts...), contentType
  260. }
  261. // From https://golang.org/src/mime/multipart/writer.go
  262. func (client *restClient) randomBoundary() string {
  263. var buf [30]byte
  264. _, err := io.ReadFull(rand.Reader, buf[:])
  265. if err != nil {
  266. panic(err)
  267. }
  268. return fmt.Sprintf("%x", buf[:])
  269. }
  270. // Creates a reader that encapsulates a single multipart form part
  271. func (client *restClient) createFieldPart(fieldname, value, boundary string) io.Reader {
  272. str := fmt.Sprintf("\r\n--%s\r\n", boundary)
  273. str += fmt.Sprintf("Content-Disposition: form-data; name=\"%s\"\r\n\r\n", quoteEscaper.Replace(fieldname))
  274. str += value
  275. return strings.NewReader(str)
  276. }