package apiproductrecommendationslat /** * Generated by API Builder - https://www.apibuilder.io * Service version: 1.1.0 * User agent: apibuilder app.apibuilder.io/hbc/api-product-recommendations-lat/1.1.0/go_1_5_client */ import ( "bytes" "encoding/json" "errors" "fmt" "html" "io" "net/http" "net/url" "strconv" "strings" ) const BaseUrl = "https://m.lordandtaylor.com/api-product-suggestions" const UserAgent = "apibuilder app.apibuilder.io/hbc/api-product-recommendations-lat/1.1.0/go_1_5_client" const Version = "1.1.0" const VersionMajor = 1 type Client struct { HttpClient *http.Client Username string Password string BaseUrl string Headers http.Header } type SimilarProductContext string const ( SimilarProductContextPdp SimilarProductContext = "pdp" // PDP page. (Strict category match) SimilarProductContextUNDEFINED = "UNDEFINED" ) func SimilarProductContextFromString(value string) SimilarProductContext { switch strings.TrimSpace(strings.ToLower(value)) { case "pdp": return SimilarProductContextPdp default: return SimilarProductContextUNDEFINED } } /** * Product price in USD */ type Price struct { Was string `json:"was"` Now string `json:"now"` OnSale string `json:"onSale"` } func PriceFromMap(data interface{}) Price { b, err := json.Marshal(data) if err == nil { return PriceFromJson(bytes.NewReader(b)) } else { panic(err) } } func PriceFromJson(bytes io.Reader) Price { var price Price json.NewDecoder(bytes).Decode(&price) return price } type Recommendation struct { ProductId string `json:"productId"` ProductName string `json:"productName"` ProductUrl string `json:"productUrl"` ProductImageUrl string `json:"productImageUrl"` BrandName string `json:"brandName"` Price Price `json:"price"` Rating float64 `json:"rating,omitempty"` NumReviews int64 `json:"numReviews,omitempty"` } func RecommendationFromMap(data interface{}) Recommendation { b, err := json.Marshal(data) if err == nil { return RecommendationFromJson(bytes.NewReader(b)) } else { panic(err) } } func RecommendationFromJson(bytes io.Reader) Recommendation { var recommendation Recommendation json.NewDecoder(bytes).Decode(&recommendation) return recommendation } type SimilarProductRecommendation struct { Recommendations []Recommendation `json:"recommendations"` } func SimilarProductRecommendationFromMap(data interface{}) SimilarProductRecommendation { b, err := json.Marshal(data) if err == nil { return SimilarProductRecommendationFromJson(bytes.NewReader(b)) } else { panic(err) } } func SimilarProductRecommendationFromJson(bytes io.Reader) SimilarProductRecommendation { var similarProductRecommendation SimilarProductRecommendation json.NewDecoder(bytes).Decode(&similarProductRecommendation) return similarProductRecommendation } type SimilarProductRecommendationsGetByContextAndProductIdParams struct { Color string Limit int32 AbTestName string AbTestVariant string } type SimilarProductRecommendationsGetByContextAndProductIdResponse struct { StatusCode int Response *http.Response Error error SimilarProductRecommendation SimilarProductRecommendation } func SimilarProductRecommendationsGetByContextAndProductId(client Client, context SimilarProductContext, productId string, params SimilarProductRecommendationsGetByContextAndProductIdParams) SimilarProductRecommendationsGetByContextAndProductIdResponse { requestUrl := fmt.Sprintf("%s/similarProductRecommendations/%s/%s", client.BaseUrl, string(context), html.EscapeString(productId)) urlValues := url.Values{} if "" != params.Color { urlValues.Add("color", params.Color) } if 0 == params.Limit { urlValues.Add("limit", strconv.FormatInt(10, 10)) } else { urlValues.Add("limit", strconv.FormatInt(params.Limit, 10)) } if "" != params.AbTestName { urlValues.Add("abTestName", params.AbTestName) } if "" != params.AbTestVariant { urlValues.Add("abTestVariant", params.AbTestVariant) } encodedValues := urlValues.Encode() if encodedValues != "" { requestUrl += "?" + encodedValues } request, err := buildRequest(client, "GET", requestUrl) if err != nil { return SimilarProductRecommendationsGetByContextAndProductIdResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return SimilarProductRecommendationsGetByContextAndProductIdResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return SimilarProductRecommendationsGetByContextAndProductIdResponse{StatusCode: resp.StatusCode, Response: resp, SimilarProductRecommendation: SimilarProductRecommendationFromJson(resp.Body)} default: return SimilarProductRecommendationsGetByContextAndProductIdResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } func buildRequest(client Client, method, urlStr string) (*http.Request, error) { request, err := http.NewRequest(method, urlStr, nil) if err != nil { return nil, err } request.Header.Add("User-Agent", UserAgent) request.Header.Add("X-Apidoc-Version", Version) request.Header.Add("X-Apidoc-Version-Major", strconv.Itoa(VersionMajor)) if client.Headers != nil { for key, values := range client.Headers { for _, value := range values { request.Header.Add(key, value) } } } if client.Username != "" { request.SetBasicAuth(client.Username, client.Password) } return request, nil }