Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature/move api into client #5

Merged
merged 4 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div id="top"></div>
<!--
*** README templace provided by https://github.com/othneildrew/Best-README-Template
*** README template provided by https://github.com/othneildrew/Best-README-Template
-->


Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -150,6 +149,7 @@ func fetchDataFeed(awinClient *awin.AwinClient) {
fmt.Println(feed)
}


```

<!-- CONTRIBUTING -->
Expand Down Expand Up @@ -205,7 +205,7 @@ Use this space to list resources you find helpful and would like to give credit

<!-- MARKDOWN LINKS & IMAGES -->
<!-- https://www.markdownguide.org/basic-syntax/#reference-style-links -->
[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
Expand Down
34 changes: 19 additions & 15 deletions awin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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}
}
15 changes: 8 additions & 7 deletions example/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions example/minimal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions test/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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)
}
Expand Down Expand Up @@ -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()))
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down