-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Boonyarit Rousamran <hok@Boonyarits-MacBook-Air.local>
- Loading branch information
Showing
20 changed files
with
592 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
APP_NAME=groceries_api | ||
|
||
APP_MYSQL_HOST=localhost | ||
APP_MYSQL_PORT=3306 | ||
APP_MYSQL_DB_NAME=grocery | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package catapi | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/hokkung/go-groceries/pkg/cnt" | ||
) | ||
|
||
//go:generate mockgen -source ./cat_api.go -destination ./mock/mock_cat_api.go | ||
type Client interface { | ||
Search(ctx context.Context, req SearchRequest) ([]SearchResponse, error) | ||
} | ||
|
||
type CatAPI struct { | ||
client *cnt.Client | ||
} | ||
|
||
// NewCatAPI creates instance | ||
func NewCatAPI() (*CatAPI, error) { | ||
client, err := cnt.NewClient(&cnt.ClientOptions{ | ||
Name: "cat-api", | ||
BaseURL: "https://api.thecatapi.com", | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &CatAPI{ | ||
client: client, | ||
}, nil | ||
} | ||
|
||
// ProvideCatAPI provides instance for di | ||
func ProvideCatAPI() (*CatAPI, error) { | ||
return NewCatAPI() | ||
} | ||
|
||
// Search searches | ||
func (c CatAPI) Search( | ||
ctx context.Context, | ||
req SearchRequest, | ||
) ([]SearchResponse, error) { | ||
var response []SearchResponse | ||
res, err := c.client. | ||
Req(ctx). | ||
SetQueryParams(req.toMapValue()). | ||
SetResult(&response). | ||
Get(SearchURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !res.IsSuccess() { | ||
return nil, errors.New("search cat failed") | ||
} | ||
|
||
return response, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package catapi | ||
|
||
const ( | ||
SearchURL = "/v1/images/search" | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package catapi | ||
|
||
import "fmt" | ||
|
||
type SearchResponse struct { | ||
ID string `json:"id"` | ||
URL string `json:"url"` | ||
Width int `json:"width"` | ||
Height int `json:"height"` | ||
} | ||
|
||
type SearchRequest struct { | ||
Limit int | ||
} | ||
|
||
func (r *SearchRequest) toMapValue() map[string]string { | ||
return map[string]string{ | ||
"limit": fmt.Sprintf("%d", r.Limit), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.