client.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2022 gorse Project Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package client
  15. import (
  16. "context"
  17. "encoding/json"
  18. "fmt"
  19. "io"
  20. "net/http"
  21. "strings"
  22. "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
  23. )
  24. type GorseClient struct {
  25. entryPoint string
  26. apiKey string
  27. httpClient http.Client
  28. }
  29. func NewGorseClient(entryPoint, apiKey string) *GorseClient {
  30. return &GorseClient{
  31. entryPoint: entryPoint,
  32. apiKey: apiKey,
  33. httpClient: http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport)},
  34. }
  35. }
  36. func (c *GorseClient) InsertFeedback(ctx context.Context, feedbacks []Feedback) (RowAffected, error) {
  37. return request[RowAffected](ctx, c, "POST", c.entryPoint+"/api/feedback", feedbacks)
  38. }
  39. func (c *GorseClient) PutFeedback(ctx context.Context, feedbacks []Feedback) (RowAffected, error) {
  40. return request[RowAffected](ctx, c, "PUT", c.entryPoint+"/api/feedback", feedbacks)
  41. }
  42. func (c *GorseClient) GetFeedback(ctx context.Context, cursor string, n int) (Feedbacks, error) {
  43. return request[Feedbacks, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/feedback?cursor=%s&n=%d", cursor, n), nil)
  44. }
  45. func (c *GorseClient) GetFeedbacksWithType(ctx context.Context, feedbackType, cursor string, n int) (Feedbacks, error) {
  46. return request[Feedbacks, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/feedback/%s?cursor=%s&n=%d", feedbackType, cursor, n), nil)
  47. }
  48. func (c *GorseClient) GetFeedbackWithUserItem(ctx context.Context, userId, itemId string) ([]Feedback, error) {
  49. return request[[]Feedback, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/feedback/%s/%s", userId, itemId), nil)
  50. }
  51. func (c *GorseClient) GetFeedbackWithTypeUserItem(ctx context.Context, feedbackType, userId, itemId string) (Feedback, error) {
  52. return request[Feedback, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/feedback/%s/%s/%s", feedbackType, userId, itemId), nil)
  53. }
  54. func (c *GorseClient) DelFeedback(ctx context.Context, feedbackType, userId, itemId string) (Feedback, error) {
  55. return request[Feedback, any](ctx, c, "DELETE", c.entryPoint+fmt.Sprintf("/api/feedback/%s/%s/%s", feedbackType, userId, itemId), nil)
  56. }
  57. func (c *GorseClient) DelFeedbackWithUserItem(ctx context.Context, userId, itemId string) ([]Feedback, error) {
  58. return request[[]Feedback, any](ctx, c, "DELETE", c.entryPoint+fmt.Sprintf("/api/feedback/%s/%s", userId, itemId), nil)
  59. }
  60. func (c *GorseClient) GetItemFeedbacks(ctx context.Context, itemId string) ([]Feedback, error) {
  61. return request[[]Feedback, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/item/%s/feedback", itemId), nil)
  62. }
  63. func (c *GorseClient) GetItemFeedbacksWithType(ctx context.Context, itemId, feedbackType string) ([]Feedback, error) {
  64. return request[[]Feedback, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/item/%s/feedback/%s", itemId, feedbackType), nil)
  65. }
  66. func (c *GorseClient) GetUserFeedbacks(ctx context.Context, userId string) ([]Feedback, error) {
  67. return request[[]Feedback, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/user/%s/feedback", userId), nil)
  68. }
  69. func (c *GorseClient) GetUserFeedbacksWithType(ctx context.Context, userId, feedbackType string) ([]Feedback, error) {
  70. return request[[]Feedback, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/user/%s/feedback/%s", userId, feedbackType), nil)
  71. }
  72. // Deprecated: GetUserFeedbacksWithType instead
  73. func (c *GorseClient) ListFeedbacks(ctx context.Context, feedbackType, userId string) ([]Feedback, error) {
  74. return request[[]Feedback, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/user/%s/feedback/%s", userId, feedbackType), nil)
  75. }
  76. func (c *GorseClient) GetItemLatest(ctx context.Context, userid string, n, offset int) ([]Score, error) {
  77. return request[[]Score, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/latest?user-id=%s&n=%d&offset=%d", userid, n, offset), nil)
  78. }
  79. func (c *GorseClient) GetItemLatestWithCategory(ctx context.Context, userid, category string, n, offset int) ([]Score, error) {
  80. return request[[]Score, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/latest/%s?user-id=%s&n=%d&offset=%d", category, userid, n, offset), nil)
  81. }
  82. func (c *GorseClient) GetItemPopular(ctx context.Context, userid string, n, offset int) ([]Score, error) {
  83. return request[[]Score, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/popular?user-id=%s&n=%d&offset=%d", userid, n, offset), nil)
  84. }
  85. func (c *GorseClient) GetItemPopularWithCategory(ctx context.Context, userid, category string, n, offset int) ([]Score, error) {
  86. return request[[]Score, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/popular/%s?user-id=%s&n=%d&offset=%d", category, userid, n, offset), nil)
  87. }
  88. func (c *GorseClient) GetItemRecommend(ctx context.Context, userId string, categories []string, writeBackType, writeBackDelay string, n, offset int) ([]string, error) {
  89. var queryCategories string
  90. if len(categories) > 0 {
  91. queryCategories = "&category=" + strings.Join(categories, "&category=")
  92. }
  93. return request[[]string, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/recommend/%s?write-back-type=%s&write-back-delay=%s&n=%d&offset=%d%s", userId, writeBackType, writeBackDelay, n, offset, queryCategories), nil)
  94. }
  95. func (c *GorseClient) GetItemRecommendWithCategory(ctx context.Context, userId, category, writeBackType, writeBackDelay string, n, offset int) ([]string, error) {
  96. return request[[]string, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/recommend/%s/%s?write-back-type=%s&write-back-delay=%s&n=%d&offset=%d", userId, category, writeBackType, writeBackDelay, n, offset), nil)
  97. }
  98. // Deprecated: GetItemRecommendWithCategory instead
  99. func (c *GorseClient) GetRecommend(ctx context.Context, userId, category string, n int) ([]string, error) {
  100. return request[[]string, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/recommend/%s/%s?n=%d", userId, category, n), nil)
  101. }
  102. func (c *GorseClient) SessionItemRecommend(ctx context.Context, feedbacks []Feedback, n, offset int) ([]Score, error) {
  103. return request[[]Score](ctx, c, "POST", c.entryPoint+fmt.Sprintf("/api/session/recommend?n=%d&offset=%d", n, offset), feedbacks)
  104. }
  105. func (c *GorseClient) SessionItemRecommendWithCategory(ctx context.Context, feedbacks []Feedback, category string, n, offset int) ([]Score, error) {
  106. return request[[]Score](ctx, c, "POST", c.entryPoint+fmt.Sprintf("/api/session/recommend/%s?n=%d&offset=%d", category, n, offset), feedbacks)
  107. }
  108. // Deprecated: SessionItemRecommend instead
  109. func (c *GorseClient) SessionRecommend(ctx context.Context, feedbacks []Feedback, n int) ([]Score, error) {
  110. return request[[]Score](ctx, c, "POST", c.entryPoint+fmt.Sprintf("/api/session/recommend?n=%d", n), feedbacks)
  111. }
  112. func (c *GorseClient) GetUserNeighbors(ctx context.Context, userId string, n, offset int) ([]Score, error) {
  113. return request[[]Score, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/user/%s/neighbors?n=%d&offset=%d", userId, n, offset), nil)
  114. }
  115. func (c *GorseClient) GetItemNeighbors(ctx context.Context, itemId, userId string, n, offset int) ([]Score, error) {
  116. return request[[]Score, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/item/%s/neighbors?n=%d&offset=%d&user-id=%s", itemId, n, offset, userId), nil)
  117. }
  118. func (c *GorseClient) GetItemNeighborsWithCategory(ctx context.Context, itemId, category, userId string, n, offset int) ([]Score, error) {
  119. return request[[]Score, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/item/%s/neighbors/%s?n=%d&offset=%d&user-id=%s", itemId, category, n, offset, userId), nil)
  120. }
  121. // Deprecated: GetItemNeighbors instead
  122. func (c *GorseClient) GetNeighbors(ctx context.Context, itemId string, n int) ([]Score, error) {
  123. return request[[]Score, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/item/%s/neighbors?n=%d", itemId, n), nil)
  124. }
  125. func (c *GorseClient) InsertUser(ctx context.Context, user User) (RowAffected, error) {
  126. return request[RowAffected](ctx, c, "POST", c.entryPoint+"/api/user", user)
  127. }
  128. func (c *GorseClient) InsertUsers(ctx context.Context, users []User) (RowAffected, error) {
  129. return request[RowAffected, any](ctx, c, "POST", c.entryPoint+"/api/users", users)
  130. }
  131. func (c *GorseClient) UpdateUser(ctx context.Context, userId string, user UserPatch) (RowAffected, error) {
  132. return request[RowAffected](ctx, c, "PATCH", c.entryPoint+fmt.Sprintf("/api/user/%s", userId), user)
  133. }
  134. func (c *GorseClient) GetUser(ctx context.Context, userId string) (User, error) {
  135. return request[User, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/user/%s", userId), nil)
  136. }
  137. func (c *GorseClient) GetUsers(ctx context.Context, cursor string, n int) (Users, error) {
  138. return request[Users, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/users?cursor=%s&n=%d", cursor, n), nil)
  139. }
  140. func (c *GorseClient) DeleteUser(ctx context.Context, userId string) (RowAffected, error) {
  141. return request[RowAffected, any](ctx, c, "DELETE", c.entryPoint+fmt.Sprintf("/api/user/%s", userId), nil)
  142. }
  143. func (c *GorseClient) InsertItem(ctx context.Context, item Item) (RowAffected, error) {
  144. return request[RowAffected](ctx, c, "POST", c.entryPoint+"/api/item", item)
  145. }
  146. func (c *GorseClient) InsertItems(ctx context.Context, items []Item) (RowAffected, error) {
  147. return request[RowAffected](ctx, c, "POST", c.entryPoint+"/api/items", items)
  148. }
  149. func (c *GorseClient) UpdateItem(ctx context.Context, itemId string, item ItemPatch) (RowAffected, error) {
  150. return request[RowAffected](ctx, c, "PATCH", c.entryPoint+fmt.Sprintf("/api/item/%s", itemId), item)
  151. }
  152. func (c *GorseClient) GetItem(ctx context.Context, itemId string) (Item, error) {
  153. return request[Item, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/item/%s", itemId), nil)
  154. }
  155. func (c *GorseClient) GetItems(ctx context.Context, cursor string, n int) (Items, error) {
  156. return request[Items, any](ctx, c, "GET", c.entryPoint+fmt.Sprintf("/api/items?cursor=%s&n=%d", cursor, n), nil)
  157. }
  158. func (c *GorseClient) DeleteItem(ctx context.Context, itemId string) (RowAffected, error) {
  159. return request[RowAffected, any](ctx, c, "DELETE", c.entryPoint+fmt.Sprintf("/api/item/%s", itemId), nil)
  160. }
  161. func (c *GorseClient) PutItemCategory(ctx context.Context, itemId string, category string) (RowAffected, error) {
  162. return request[RowAffected, any](ctx, c, "PUT", c.entryPoint+fmt.Sprintf("/api/item/%s/category/%s", itemId, category), nil)
  163. }
  164. func (c *GorseClient) DelItemCategory(ctx context.Context, itemId string, category string) (RowAffected, error) {
  165. return request[RowAffected, any](ctx, c, "DELETE", c.entryPoint+fmt.Sprintf("/api/item/%s/category/%s", itemId, category), nil)
  166. }
  167. func (c *GorseClient) HealthLive(ctx context.Context) (Health, error) {
  168. return request[Health, any](ctx, c, "GET", c.entryPoint+"/api/health/live", nil)
  169. }
  170. func (c *GorseClient) HealthReady(ctx context.Context) (Health, error) {
  171. return request[Health, any](ctx, c, "GET", c.entryPoint+"/api/health/ready", nil)
  172. }
  173. func request[Response any, Body any](ctx context.Context, c *GorseClient, method, url string, body Body) (result Response, err error) {
  174. bodyByte, marshalErr := json.Marshal(body)
  175. if marshalErr != nil {
  176. return result, marshalErr
  177. }
  178. var req *http.Request
  179. req, err = http.NewRequestWithContext(ctx, method, url, strings.NewReader(string(bodyByte)))
  180. if err != nil {
  181. return result, err
  182. }
  183. req.Header.Set("X-API-Key", c.apiKey)
  184. req.Header.Set("Content-Type", "application/json")
  185. resp, err := c.httpClient.Do(req)
  186. if err != nil {
  187. return result, err
  188. }
  189. defer resp.Body.Close()
  190. buf := new(strings.Builder)
  191. _, err = io.Copy(buf, resp.Body)
  192. if err != nil {
  193. return result, err
  194. }
  195. if resp.StatusCode != http.StatusOK {
  196. return result, ErrorMessage(buf.String())
  197. }
  198. err = json.Unmarshal([]byte(buf.String()), &result)
  199. if err != nil {
  200. return result, err
  201. }
  202. return result, err
  203. }