This commit is contained in:
Piotr Biernat 2022-12-25 22:59:04 +01:00
parent f141f6c34f
commit 2e40ca75c9
3 changed files with 12 additions and 15 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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
}