Skip to content

Commit

Permalink
Introduce Service Objects
Browse files Browse the repository at this point in the history
  • Loading branch information
canro91 committed May 1, 2020
1 parent 4766f57 commit e43373a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 60 deletions.
65 changes: 65 additions & 0 deletions Day29/apiclient/client/books.go
Original file line number Diff line number Diff line change
@@ -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
}
62 changes: 5 additions & 57 deletions Day29/apiclient/client/client.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package client

import (
"fmt"
"time"
"bytes"
"encoding/json"
"net/http"
)

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) {
Expand All @@ -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
}

Expand All @@ -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
}
}
6 changes: 3 additions & 3 deletions Day29/apiclient/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"strings"
"fmt"
"github.com/canro91/30DaysOfGo/Day29/apiclient/client"
"log"
"strings"
)

func main() {
Expand All @@ -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)
}
Expand Down

0 comments on commit e43373a

Please sign in to comment.