diff --git a/Day29/apiclient/client/books.go b/Day29/apiclient/client/books.go new file mode 100644 index 0000000..e147533 --- /dev/null +++ b/Day29/apiclient/client/books.go @@ -0,0 +1,65 @@ +package client + +import ( + "bytes" + "encoding/json" + "fmt" +) + +type BookService struct { + client *Client +} + +type Book struct { + Title string `json:"name"` + Author string `json:"author"` + Rating int `json:"rating"` +} + +func (b *BookService) Post(title, author string, rating int) (*Book, error) { + input := Book{Title: title, Author: author, Rating: rating} + data, err := json.Marshal(input) + if err != nil { + return nil, err + } + + // Notice you would have to set up a ApiKey header. + // For example: + // req, err := http.NewRequest("GET", "http://example.com", nil) + // req.Header.Add("X-API-KEY", c.ApiKey) + // resp, err := c.Client.Do(req) + + url := fmt.Sprintf("http://%s/api/v1/Book", b.client.BaseUrl) + resp, err := b.client.Client.Post(url, "application/json", bytes.NewBuffer(data)) + if err != nil { + return nil, err + } + + defer resp.Body.Close() + + var book Book + err = json.NewDecoder(resp.Body).Decode(&book) + if err != nil { + return nil, err + } + + return &book, nil +} + +func (b *BookService) GetAll() ([]Book, error) { + url := fmt.Sprintf("http://%s/api/v1/Book", b.client.BaseUrl) + resp, err := b.client.Client.Get(url) + if err != nil { + return nil, err + } + + defer resp.Body.Close() + + var books []Book + err = json.NewDecoder(resp.Body).Decode(&books) + if err != nil { + return nil, err + } + + return books, nil +} \ No newline at end of file diff --git a/Day29/apiclient/client/client.go b/Day29/apiclient/client/client.go index 189eb2c..7c3e424 100644 --- a/Day29/apiclient/client/client.go +++ b/Day29/apiclient/client/client.go @@ -1,10 +1,7 @@ package client import ( - "fmt" "time" - "bytes" - "encoding/json" "net/http" ) @@ -12,12 +9,8 @@ type Client struct { ApiKey string BaseUrl string Client *http.Client -} -type Book struct { - Title string `json:"name"` - Author string `json:"author"` - Rating int `json:"rating"` + Books *BookService } func NewClient(apiKey string, opts ...func(*Client) error) (*Client, error) { @@ -32,6 +25,9 @@ func NewClient(apiKey string, opts ...func(*Client) error) (*Client, error) { return nil, err } } + + client.Books = &BookService{client: client} + return client, nil } @@ -40,52 +36,4 @@ func WithHttpClient(client *http.Client) (func(*Client) error) { c.Client = client return nil } -} - -func (c *Client) CreateBook(title, author string, rating int) (*Book, error) { - input := Book{Title: title, Author: author, Rating: rating} - data, err := json.Marshal(input) - if err != nil { - return nil, err - } - - // Notice you would have to set up a ApiKey header. - // For example: - // req, err := http.NewRequest("GET", "http://example.com", nil) - // req.Header.Add("X-API-KEY", c.ApiKey) - // resp, err := c.Client.Do(req) - - url := fmt.Sprintf("http://%s/api/v1/Book", c.BaseUrl) - resp, err := c.Client.Post(url, "application/json", bytes.NewBuffer(data)) - if err != nil { - return nil, err - } - - defer resp.Body.Close() - - var book Book - err = json.NewDecoder(resp.Body).Decode(&book) - if err != nil { - return nil, err - } - - return &book, nil -} - -func (c *Client) GetAllBooks() ([]Book, error) { - url := fmt.Sprintf("http://%s/api/v1/Book", c.BaseUrl) - resp, err := c.Client.Get(url) - if err != nil { - return nil, err - } - - defer resp.Body.Close() - - var books []Book - err = json.NewDecoder(resp.Body).Decode(&books) - if err != nil { - return nil, err - } - - return books, nil -} +} \ No newline at end of file diff --git a/Day29/apiclient/main.go b/Day29/apiclient/main.go index 0193f12..7949206 100644 --- a/Day29/apiclient/main.go +++ b/Day29/apiclient/main.go @@ -1,10 +1,10 @@ package main import ( - "strings" "fmt" "github.com/canro91/30DaysOfGo/Day29/apiclient/client" "log" + "strings" ) func main() { @@ -14,13 +14,13 @@ func main() { // myClient, _ := client.NewClient("XYZ", client.WithHttpClient(&http.Client{})) myClient, _ := client.NewClient("XYZ") - _, err := myClient.CreateBook("The Art of War", "Sun Tzu", 5) + _, err := myClient.Books.Post("The Art of War", "Sun Tzu", 5) if err != nil { log.Fatal(err) } fmt.Println("Querying all the books") - books, err := myClient.GetAllBooks() + books, err := myClient.Books.GetAll() if err != nil { log.Fatal(err) }