From 2e40ca75c99b0f73c2479b584eaf89f5e165f6f1 Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Sun, 25 Dec 2022 22:59:04 +0100 Subject: [PATCH] API fix --- api/basket.go | 8 ++++---- api/http.go | 13 +++++-------- api/pricing.go | 6 +++--- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/api/basket.go b/api/basket.go index c8a422d..a3c4fe5 100644 --- a/api/basket.go +++ b/api/basket.go @@ -7,8 +7,8 @@ import ( "github.com/go-redis/redis/v8" ) -func NewBasketAPI(redis *redis.Client) *BasketAPI { - return &BasketAPI{NewHttpClient(redis)} +func NewBasketAPI(ua string, redis *redis.Client) *BasketAPI { + return &BasketAPI{NewHttpClient(ua, redis)} } type BasketAPI struct { @@ -18,7 +18,7 @@ type BasketAPI struct { func (a *BasketAPI) GetBasket(basketID string) (*def.GetBasketResponse, error) { req := &def.GetBasketRequest{BasketID: basketID} res := new(def.GetBasketResponse) - if err := a.httpClient.SendGet("basket", "/api/v1/basket", req, res); err != nil { + if err := a.httpClient.SendGet("basket-svc", "/api/v1/basket", req, res); err != nil { return nil, err } @@ -28,7 +28,7 @@ func (a *BasketAPI) GetBasket(basketID string) (*def.GetBasketResponse, error) { func (a *BasketAPI) GetBasketItems(basketID string) ([]*def.GetBasketItemsResponse, error) { url := fmt.Sprintf("/api/v1/basket/%s/items", basketID) var res []*def.GetBasketItemsResponse - if err := a.httpClient.SendGet("basket", url, nil, &res); err != nil { + if err := a.httpClient.SendGet("basket-svc", url, nil, &res); err != nil { return nil, err } diff --git a/api/http.go b/api/http.go index c5ab4c1..8bb3d77 100644 --- a/api/http.go +++ b/api/http.go @@ -10,16 +10,13 @@ import ( "github.com/go-redis/redis/v8" ) -const ( - HEADER_USER_AGENT = "order-httpclient" -) - type HttpClient struct { + ua string redis *redis.Client } -func NewHttpClient(redis *redis.Client) *HttpClient { - return &HttpClient{redis} +func NewHttpClient(ua string, redis *redis.Client) *HttpClient { + return &HttpClient{ua, redis} } func (c *HttpClient) SendGet(api, url string, data, out any) error { @@ -67,7 +64,7 @@ func (c *HttpClient) sendRequest(api, url, method string, data any) (*http.Respo } req.Header.Set("Content-Type", "application/json") - req.Header.Set("User-Agent", HEADER_USER_AGENT) + req.Header.Set("User-Agent", c.ua) res, err := client.Do(req) if err != nil { return nil, err @@ -78,7 +75,7 @@ func (c *HttpClient) sendRequest(api, url, method string, data any) (*http.Respo } func (c *HttpClient) getApiUrl(api string) string { - ctx, key, apiAddr := context.Background(), "internal__"+api+"_ips", api + ctx, key, apiAddr := context.Background(), "internal__"+api+"__ips", api // FIXME: key name ^^ cmd := c.redis.LLen(ctx, key) if cmd.Err() == nil { diff --git a/api/pricing.go b/api/pricing.go index 0d577bc..1e3cf46 100644 --- a/api/pricing.go +++ b/api/pricing.go @@ -7,8 +7,8 @@ import ( "github.com/go-redis/redis/v8" ) -func NewPricingAPI(redis *redis.Client) *PricingAPI { - return &PricingAPI{NewHttpClient(redis)} +func NewPricingAPI(ua string, redis *redis.Client) *PricingAPI { + return &PricingAPI{NewHttpClient(ua, redis)} } type PricingAPI struct { @@ -18,7 +18,7 @@ type PricingAPI struct { func (a *PricingAPI) GetProductPrice(productID int) (*def.ProductPriceResponse, error) { url := fmt.Sprintf("/api/v1/product/%d", productID) res := new(def.ProductPriceResponse) - if err := a.httpClient.SendGet("pricing", url, nil, res); err != nil { + if err := a.httpClient.SendGet("pricing-svc", url, nil, res); err != nil { return nil, err }