diff --git a/README.md b/README.md index 1f7cffb..4e10bec 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@
@@ -119,14 +119,14 @@ import ( ) func main() { - awinClient := awin.NewAwinClient(&http.Client{}) + awinClient := awin.NewAwinClient("apiKey", &http.Client{}) fetchDataFeedList(awinClient) fetchDataFeed(awinClient) } func fetchDataFeedList(awinClient *awin.AwinClient) { - feedList, err := awinClient.FetchDataFeedList("apiKey") + feedList, err := awinClient.FetchDataFeedList() if err != nil { panic(err) @@ -137,7 +137,6 @@ func fetchDataFeedList(awinClient *awin.AwinClient) { func fetchDataFeed(awinClient *awin.AwinClient) { feed, err := awinClient.FetchDataFeed(&awin.DataFeedOptions{ - ApiKey: "apiKey", FeedIds: []string{"feedId1", "feedId2"}, Language: "en", ShowAdultContent: false, @@ -150,6 +149,7 @@ func fetchDataFeed(awinClient *awin.AwinClient) { fmt.Println(feed) } + ``` @@ -205,7 +205,7 @@ Use this space to list resources you find helpful and would like to give credit -[build-shield]: https://img.shields.io/github/workflow/status/matthiasbruns/awin-go/Go/main?style=for-the-badge +[build-shield]: https://img.shields.io/github/actions/workflow/status/matthiasbruns/awin-go/go.yml/main&style=for-the-badge [build-url]: https://github.com/matthiasbruns/awin-go/actions/workflows/go.yml [contributors-shield]: https://img.shields.io/github/contributors/matthiasbruns/awin-go.svg?style=for-the-badge [contributors-url]: https://github.com/matthiasbruns/awin-go/graphs/contributors diff --git a/awin/client.go b/awin/client.go index 23446d3..5455a63 100644 --- a/awin/client.go +++ b/awin/client.go @@ -52,26 +52,26 @@ func init() { } // DataFeedOptions -/// ApiKey You can get the download API key from a standard feed download as given by Create-a-Feed. You can also get the full download link including the relevant API key to access this file from the Create-a-Feed section in the interface (Awin interface --> Toolbox --> Create-a-Feed). -/// FeedIds The string slice of all publisher feed ids -/// Language ISO 3166-1 alpha-2 – two-letter country codes e.g. de, en -/// ShowAdultContent true to include adult content +// / FeedIds The string slice of all publisher feed ids +// / Language ISO 3166-1 alpha-2 – two-letter country codes e.g. de, en +// / ShowAdultContent true to include adult content type DataFeedOptions struct { - ApiKey string FeedIds []string Language string ShowAdultContent bool } // AwinClient -/// Client that takes over the communication with the Awin endpoints as well as parsing the response csv data into structs. +// / client that takes over the communication with the Awin endpoints as well as parsing the response csv data into structs. +// / apiKey You can get the download API key from a standard feed download as given by Create-a-Feed. You can also get the full download link including the relevant API key to access this file from the Create-a-Feed section in the interface (Awin interface --> Toolbox --> Create-a-Feed). type AwinClient struct { client *http.Client + apiKey string } -func (c AwinClient) FetchDataFeedList(apiKey string) (*[]DataFeedListRow, error) { +func (c AwinClient) FetchDataFeedList() (*[]DataFeedListRow, error) { // Get list of joined and not joined publishers - resp, err := c.client.Get(fmt.Sprintf(dataFeedListUrl, baseUrl, apiKey)) + resp, err := c.client.Get(fmt.Sprintf(dataFeedListUrl, baseUrl, c.apiKey)) if err != nil { return nil, err } @@ -86,7 +86,7 @@ func (c AwinClient) FetchDataFeed(options *DataFeedOptions) (*[]DataFeedEntry, e showAdult = 1 } - url := fmt.Sprintf(dataFeedUrl, baseUrl, options.ApiKey, options.Language, strings.Join(options.FeedIds, ","), defaultDataFeedColumnsParam, ",", showAdult) + url := fmt.Sprintf(dataFeedUrl, baseUrl, c.apiKey, options.Language, strings.Join(options.FeedIds, ","), defaultDataFeedColumnsParam, ",", showAdult) return c.FetchDataFeedFromUrl(url) } @@ -146,10 +146,14 @@ func parseCSVToDataFeedEntry(r io.Reader) (*[]DataFeedEntry, error) { return &entries, nil } -// NewAwinClient -/// Returns a new AwinClient. Needs a http.Client passed from outside. -/// client Required to be passed from the caller -/// returns a new instance of AwinClient -func NewAwinClient(client *http.Client) *AwinClient { - return &AwinClient{client: client} +func NewAwinClient(apiKey string, client *http.Client) *AwinClient { + return &AwinClient{client: client, apiKey: apiKey} +} + +// NewAwinClientWithHzzp +// / Returns a new NewAwinClientWithHzzp. Needs a http.Client passed from outside. +// / client Required to be passed from the caller +// / returns a new instance of AwinClient +func NewAwinClientWithHttp(apiKey string, client *http.Client) *AwinClient { + return &AwinClient{client: client, apiKey: apiKey} } diff --git a/example/cli/main.go b/example/cli/main.go index 71ddbc0..1881af6 100644 --- a/example/cli/main.go +++ b/example/cli/main.go @@ -15,7 +15,6 @@ const feedListUsage = "./awin-go feedlist -apikey=API_KEY" const feedUsage = "./awin-go feed -apikey=API_KEY -ids id1 id2 -lang en -adult true" func main() { - awinClient := awin.NewAwinClient(&http.Client{}) feedListCmd := flag.NewFlagSet("feedlist", flag.ExitOnError) feedCmd := flag.NewFlagSet("feed", flag.ExitOnError) @@ -27,17 +26,18 @@ func main() { switch os.Args[1] { case "feedlist": - handleFeedListCmd(feedListCmd, awinClient) + handleFeedListCmd(feedListCmd) case "feed": - handleFeedCmd(feedCmd, awinClient) + handleFeedCmd(feedCmd) default: fmt.Println(cliUsage) os.Exit(1) } } -func handleFeedListCmd(feedListCmd *flag.FlagSet, awinClient *awin.AwinClient) { +func handleFeedListCmd(feedListCmd *flag.FlagSet) { feedListApiKey := feedListCmd.String("apikey", "", "apikey") + awinClient := awin.NewAwinClient(*feedListApiKey, &http.Client{}) if err := feedListCmd.Parse(os.Args[2:]); err != nil { fmt.Print(feedListUsage) @@ -46,7 +46,7 @@ func handleFeedListCmd(feedListCmd *flag.FlagSet, awinClient *awin.AwinClient) { fmt.Println("loading datafeed list from Awin") - results, err := awinClient.FetchDataFeedList(*feedListApiKey) + results, err := awinClient.FetchDataFeedList() if err != nil { fmt.Print(err) os.Exit(1) @@ -60,12 +60,14 @@ func handleFeedListCmd(feedListCmd *flag.FlagSet, awinClient *awin.AwinClient) { } } -func handleFeedCmd(feedListCmd *flag.FlagSet, awinClient *awin.AwinClient) { +func handleFeedCmd(feedListCmd *flag.FlagSet) { feedListApiKey := feedListCmd.String("apikey", "", "-apikey API_KEY") feedIds := feedListCmd.String("ids", "", "-ids fleedId1 fleedId2") language := feedListCmd.String("lang", "en", "-lang en") showAdult := feedListCmd.Bool("adult", false, "-adult true") + awinClient := awin.NewAwinClient(*feedListApiKey, &http.Client{}) + if err := feedListCmd.Parse(os.Args[2:]); err != nil { fmt.Print(feedUsage) os.Exit(1) @@ -76,7 +78,6 @@ func handleFeedCmd(feedListCmd *flag.FlagSet, awinClient *awin.AwinClient) { fmt.Println("loading datafeed from Awin") results, err := awinClient.FetchDataFeed(&awin.DataFeedOptions{ - ApiKey: *feedListApiKey, FeedIds: ids, Language: *language, ShowAdultContent: *showAdult, diff --git a/example/minimal/main.go b/example/minimal/main.go index a9b4c75..742429f 100644 --- a/example/minimal/main.go +++ b/example/minimal/main.go @@ -7,14 +7,14 @@ import ( ) func main() { - awinClient := awin.NewAwinClient(&http.Client{}) + awinClient := awin.NewAwinClient("apiKey", &http.Client{}) fetchDataFeedList(awinClient) fetchDataFeed(awinClient) } func fetchDataFeedList(awinClient *awin.AwinClient) { - feedList, err := awinClient.FetchDataFeedList("apiKey") + feedList, err := awinClient.FetchDataFeedList() if err != nil { panic(err) @@ -25,7 +25,6 @@ func fetchDataFeedList(awinClient *awin.AwinClient) { func fetchDataFeed(awinClient *awin.AwinClient) { feed, err := awinClient.FetchDataFeed(&awin.DataFeedOptions{ - ApiKey: "apiKey", FeedIds: []string{"feedId1", "feedId2"}, Language: "en", ShowAdultContent: false, diff --git a/test/client_test.go b/test/client_test.go index 7914f33..1232cf9 100644 --- a/test/client_test.go +++ b/test/client_test.go @@ -205,7 +205,7 @@ func TestFetchDataFeedList(t *testing.T) { } // Create test client to run tests on - awinClient := awin.NewAwinClient(&http.Client{Transport: mockRoundTripper{response: response, requestTestFunc: func(r *http.Request) error { + awinClient := awin.NewAwinClient("apiKey", &http.Client{Transport: mockRoundTripper{response: response, requestTestFunc: func(r *http.Request) error { expectedUrl := "https://productdata.awin.com/datafeed/list/apikey/apiKey" if r.URL.String() != expectedUrl { err := errors.New(fmt.Sprintf("invalid url found in test\nexpected '%s'\nfound '%s'", expectedUrl, r.URL.String())) @@ -223,7 +223,7 @@ func TestFetchDataFeedList(t *testing.T) { return nil }}}) - result, err := awinClient.FetchDataFeedList("apiKey") + result, err := awinClient.FetchDataFeedList() if err != nil { t.Fatalf("err is not null '%v'", err) } @@ -268,7 +268,7 @@ func TestFetchDataFeed(t *testing.T) { } // Create test client to run tests on - awinClient := awin.NewAwinClient(&http.Client{Transport: mockRoundTripper{response: response, requestTestFunc: func(r *http.Request) error { + awinClient := awin.NewAwinClient("apiKey", &http.Client{Transport: mockRoundTripper{response: response, requestTestFunc: func(r *http.Request) error { expectedUrl := "https://productdata.awin.com/datafeed/download/apikey/apiKey/language/en/fid/fid1,fid2/columns/aw_deep_link,product_name,aw_product_id,merchant_product_id,merchant_image_url,description,merchant_category,search_price,merchant_name,merchant_id,category_name,category_id,aw_image_url,currency,store_price,delivery_cost,merchant_deep_link,language,last_updated,display_price,data_feed_id,brand_name,brand_id,colour,product_short_description,specifications,condition,product_model,model_number,dimensions,keywords,promotional_text,product_type,commission_group,merchant_product_category_path,merchant_product_second_category,merchant_product_third_category,rrp_price,saving,savings_percent,base_price,base_price_amount,base_price_text,product_price_old,delivery_restrictions,delivery_weight,warranty,terms_of_contract,delivery_time,in_stock,stock_quantity,valid_from,valid_to,is_for_sale,web_offer,pre_order,stock_status,size_stock_status,size_stock_amount,merchant_thumb_url,large_image,alternate_image,aw_thumb_url,alternate_image_two,alternate_image_three,alternate_image_four,reviews,average_rating,rating,number_available,custom_1,custom_2,custom_3,custom_4,custom_5,custom_6,custom_7,custom_8,custom_9,ean,isbn,upc,mpn,parent_product_id,product_GTIN,basket_link/format/csv/delimiter/,/compression/gzip/adultcontent/1/" if r.URL.String() != expectedUrl { err := errors.New(fmt.Sprintf("invalid url found in test\nexpected '%s'\nfound '%s'", expectedUrl, r.URL.String())) @@ -287,7 +287,6 @@ func TestFetchDataFeed(t *testing.T) { }}}) result, err := awinClient.FetchDataFeed(&awin.DataFeedOptions{ - ApiKey: "apiKey", FeedIds: []string{"fid1", "fid2"}, Language: "en", ShowAdultContent: true, @@ -339,7 +338,7 @@ func TestFetchDataFeedFromUrl(t *testing.T) { } // Create test client to run tests on - awinClient := awin.NewAwinClient(&http.Client{Transport: mockRoundTripper{response: response, requestTestFunc: func(r *http.Request) error { + awinClient := awin.NewAwinClient("apiKey", &http.Client{Transport: mockRoundTripper{response: response, requestTestFunc: func(r *http.Request) error { fmt.Println(r.URL.String()) expectedUrl := "https://productdata.awin.com/datafeed/download/apikey/apiKey/language/en/fid/fid1,fid2/columns/aw_deep_link,product_name,aw_product_id,merchant_product_id,merchant_image_url,description,merchant_category,search_price,merchant_name,merchant_id,category_name,category_id,aw_image_url,currency,store_price,delivery_cost,merchant_deep_link,language,last_updated,display_price,data_feed_id,brand_name,brand_id,colour,product_short_description,specifications,condition,product_model,model_number,dimensions,keywords,promotional_text,product_type,commission_group,merchant_product_category_path,merchant_product_second_category,merchant_product_third_category,rrp_price,saving,savings_percent,base_price,base_price_amount,base_price_text,product_price_old,delivery_restrictions,delivery_weight,warranty,terms_of_contract,delivery_time,in_stock,stock_quantity,valid_from,valid_to,is_for_sale,web_offer,pre_order,stock_status,size_stock_status,size_stock_amount,merchant_thumb_url,large_image,alternate_image,aw_thumb_url,alternate_image_two,alternate_image_three,alternate_image_four,reviews,average_rating,rating,number_available,custom_1,custom_2,custom_3,custom_4,custom_5,custom_6,custom_7,custom_8,custom_9,ean,isbn,upc,mpn,parent_product_id,product_GTIN,basket_link/format/csv/delimiter/,/compression/gzip/adultcontent/1/" if r.URL.String() != expectedUrl {