From 880f9efab0afb74567bb764384417e29afb0a265 Mon Sep 17 00:00:00 2001 From: JBP <2850825+jybp@users.noreply.github.com> Date: Sun, 18 Aug 2019 21:56:54 +0200 Subject: [PATCH] Add Search --- browse.go | 141 +++++++++++++++++++++++++++++++++++++++++++++++++ browse_test.go | 43 +++++++++++---- 2 files changed, 175 insertions(+), 9 deletions(-) diff --git a/browse.go b/browse.go index c56731d..be64ff8 100644 --- a/browse.go +++ b/browse.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "net/url" + "strconv" "time" ) @@ -547,3 +548,143 @@ func (s *BrowseService) CheckCompatibility(ctx context.Context, itemID, marketpl var c Compatibility return c, s.client.Do(ctx, req, &c) } + +// Search represents the result of an eBay search. +type Search struct { + Href string `json:"href"` + Total int `json:"total"` + Next string `json:"next"` + Limit int `json:"limit"` + Offset int `json:"offset"` + ItemSummaries []struct { + ItemID string `json:"itemId"` + Title string `json:"title"` + Image struct { + ImageURL string `json:"imageUrl"` + } `json:"image"` + Price struct { + Value string `json:"value"` + Currency string `json:"currency"` + } `json:"price"` + ItemHref string `json:"itemHref"` + Seller struct { + Username string `json:"username"` + FeedbackPercentage string `json:"feedbackPercentage"` + FeedbackScore int `json:"feedbackScore"` + } `json:"seller"` + MarketingPrice struct { + OriginalPrice struct { + Value string `json:"value"` + Currency string `json:"currency"` + } `json:"originalPrice"` + DiscountPercentage string `json:"discountPercentage"` + DiscountAmount struct { + Value string `json:"value"` + Currency string `json:"currency"` + } `json:"discountAmount"` + } `json:"marketingPrice"` + Condition string `json:"condition"` + ConditionID string `json:"conditionId"` + ThumbnailImages []struct { + ImageURL string `json:"imageUrl"` + } `json:"thumbnailImages"` + ShippingOptions []struct { + ShippingCostType string `json:"shippingCostType"` + ShippingCost struct { + Value string `json:"value"` + Currency string `json:"currency"` + } `json:"shippingCost"` + } `json:"shippingOptions"` + BuyingOptions []string `json:"buyingOptions"` + CurrentBidPrice struct { + Value string `json:"value"` + Currency string `json:"currency"` + } `json:"currentBidPrice"` + Epid string `json:"epid"` + ItemWebURL string `json:"itemWebUrl"` + ItemLocation struct { + PostalCode string `json:"postalCode"` + Country string `json:"country"` + } `json:"itemLocation"` + Categories []struct { + CategoryID string `json:"categoryId"` + } `json:"categories"` + AdditionalImages []struct { + ImageURL string `json:"imageUrl"` + } `json:"additionalImages"` + AdultOnly bool `json:"adultOnly"` + } `json:"itemSummaries"` +} + +func optSearch(param string) func(v string) func(*http.Request) { + return func(v string) func(*http.Request) { + return func(req *http.Request) { + query := req.URL.Query() + query.Add(param, v) + req.URL.RawQuery = query.Encode() + } + } +} + +// Several query parameters to use with the Search method. + +func OptBrowseSearch(v string) func(*http.Request) { + return optSearch("q")(v) +} + +func OptBrowseSearchGtin(v string) func(*http.Request) { + return optSearch("gtin")(v) +} + +func OptBrowseSearchCharityIDs(v string) func(*http.Request) { + return optSearch("charity_ids")(v) +} + +func OptBrowseSearchFieldgroups(v string) func(*http.Request) { + return optSearch("fieldgroups")(v) +} + +func OptBrowseSearchCompatibilityFilter(v string) func(*http.Request) { + return optSearch("compatibility_filter")(v) +} + +func OptBrowseSearchCategoryID(v string) func(*http.Request) { + return optSearch("category_ids")(v) +} + +func OptBrowseSearchFilter(v string) func(*http.Request) { + return optSearch("filter")(v) +} + +func OptBrowseSearchSort(v string) func(*http.Request) { + return optSearch("sort")(v) +} + +func OptBrowseSearchLimit(limit int) func(*http.Request) { + return optSearch("limit")(strconv.Itoa(limit)) +} + +func OptBrowseSearchOffset(offset int) func(*http.Request) { + return optSearch("offset")(strconv.Itoa(offset)) +} + +func OptBrowseSearchAspectFilter(v string) func(*http.Request) { + return optSearch("aspect_filter")(v) +} + +func OptBrowseSearchEPID(epid int) func(*http.Request) { + return optSearch("epid")(strconv.Itoa(epid)) +} + +// Search searches for eBay items. +// +// eBay API docs: https://developer.ebay.com/api-docs/buy/browse/resources/item_summary/methods/search +func (s *BrowseService) Search(ctx context.Context, opts ...Opt) (Search, error) { + u := "buy/browse/v1/item_summary/search" + req, err := s.client.NewRequest(http.MethodGet, u, nil, opts...) + if err != nil { + return Search{}, err + } + var search Search + return search, s.client.Do(ctx, req, &search) +} diff --git a/browse_test.go b/browse_test.go index a277894..b9bebf9 100644 --- a/browse_test.go +++ b/browse_test.go @@ -32,12 +32,13 @@ func TestGetLegacyItem(t *testing.T) { if r.Method != "GET" { t.Fatalf("expected GET method, got: %s", r.Method) } - fmt.Fprintf(w, `{"itemId": "v1|%s|0"}`, r.URL.Query().Get("legacy_item_id")) + assert.Equal(t, "202117468662", r.URL.Query().Get("legacy_item_id")) + fmt.Fprintf(w, `{"itemId": "itemId"}`) }) item, err := client.Buy.Browse.GetItemByLegacyID(context.Background(), "202117468662") assert.Nil(t, err) - assert.Equal(t, "v1|202117468662|0", item.ItemID) + assert.Equal(t, "itemId", item.ItemID) } func TestGetCompactItem(t *testing.T) { @@ -48,12 +49,13 @@ func TestGetCompactItem(t *testing.T) { if r.Method != "GET" { t.Fatalf("expected GET method, got: %s", r.Method) } - fmt.Fprintf(w, `{"itemId": "%s"}`, r.URL.Query().Get("fieldgroups")) + assert.Equal(t, "COMPACT", r.URL.Query().Get("fieldgroups")) + fmt.Fprintf(w, `{"itemId": "itemId"}`) }) item, err := client.Buy.Browse.GetCompactItem(context.Background(), "v1|202117468662|0") assert.Nil(t, err) - assert.Equal(t, "COMPACT", item.ItemID) + assert.Equal(t, "itemId", item.ItemID) } func TestGettItem(t *testing.T) { @@ -64,12 +66,13 @@ func TestGettItem(t *testing.T) { if r.Method != "GET" { t.Fatalf("expected GET method, got: %s", r.Method) } - fmt.Fprintf(w, `{"itemId": "%s"}`, r.URL.Query().Get("fieldgroups")) + assert.Equal(t, "PRODUCT", r.URL.Query().Get("fieldgroups")) + fmt.Fprint(w, `{"itemId": "itemId"}`) }) item, err := client.Buy.Browse.GetItem(context.Background(), "v1|202117468662|0") assert.Nil(t, err) - assert.Equal(t, "PRODUCT", item.ItemID) + assert.Equal(t, "itemId", item.ItemID) } func TestGetItemByGroupID(t *testing.T) { @@ -80,12 +83,13 @@ func TestGetItemByGroupID(t *testing.T) { if r.Method != "GET" { t.Fatalf("expected GET method, got: %s", r.Method) } - fmt.Fprintf(w, `{"items": [{"itemId": "%s"}]}`, r.URL.Query().Get("item_group_id")) + assert.Equal(t, "151915076499", r.URL.Query().Get("item_group_id")) + fmt.Fprint(w, `{"items": [{"itemId": "itemId"}]}`) }) it, err := client.Buy.Browse.GetItemByGroupID(context.Background(), "151915076499") assert.Nil(t, err) - assert.Equal(t, "151915076499", it.Items[0].ItemID) + assert.Equal(t, "itemId", it.Items[0].ItemID) } func TestCheckCompatibility(t *testing.T) { @@ -103,7 +107,7 @@ func TestCheckCompatibility(t *testing.T) { } assert.Equal(t, `{"compatibilityProperties":[{"name":"0","value":"1"},{"name":"2","value":"3"}]} `, string(body)) - fmt.Fprintf(w, `{"compatibilityStatus": "NOT_COMPATIBLE", "warnings": [{"category" : "category"}]}`) + fmt.Fprint(w, `{"compatibilityStatus": "NOT_COMPATIBLE", "warnings": [{"category" : "category"}]}`) }) compatibilityProperties := []ebay.CompatibilityProperty{ {Name: "0", Value: "1"}, @@ -114,3 +118,24 @@ func TestCheckCompatibility(t *testing.T) { assert.Equal(t, "NOT_COMPATIBLE", compatibility.CompatibilityStatus) assert.Equal(t, "category", compatibility.Warnings[0].Category) } + +func TestSearch(t *testing.T) { + client, mux, teardown := setup(t) + defer teardown() + + mux.HandleFunc("/buy/browse/v1/item_summary/search", func(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + t.Fatalf("expected GET method, got: %s", r.Method) + } + assert.Equal(t, "search", r.URL.Query().Get("q")) + assert.Equal(t, "2", r.URL.Query().Get("limit")) + + fmt.Fprint(w, `{"href": "href","total":1,"itemSummaries": [{"itemId": "itemId"}]}`) + }) + + search, err := client.Buy.Browse.Search(context.Background(), ebay.OptBrowseSearch("search"), ebay.OptBrowseSearchLimit(2)) + assert.Nil(t, err) + assert.Equal(t, "href", search.Href) + assert.Equal(t, 1, search.Total) + assert.Equal(t, "itemId", search.ItemSummaries[0].ItemID) +}