Skip to content

Commit c00e058

Browse files
committed
Bumped v1
Signed-off-by: Vishal Rana <[email protected]>
1 parent 4771e61 commit c00e058

18 files changed

+407
-400
lines changed

.idea/workspace.xml

+55-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
type (
88
Client struct {
9+
key string
910
resty *resty.Client
1011
}
1112

@@ -21,11 +22,42 @@ const (
2122

2223
func New(key string) *Client {
2324
return &Client{
25+
key: key,
2426
resty: resty.New().SetHostURL(url).SetAuthToken(key),
2527
}
2628
}
2729

28-
func IsError(status int) bool {
30+
func (c *Client) Currency() *CurrencyService {
31+
return &CurrencyService{
32+
resty: resty.New().SetHostURL("https://currency.labstack.com/api/v1").SetAuthToken(c.key),
33+
}
34+
}
35+
36+
func (c *Client) Domain() *DomainService {
37+
return &DomainService{
38+
resty: resty.New().SetHostURL("https://domain.labstack.com/api/v1").SetAuthToken(c.key),
39+
}
40+
}
41+
42+
func (c *Client) Email() *EmailService {
43+
return &EmailService{
44+
resty: resty.New().SetHostURL("https://email.labstack.com/api/v1").SetAuthToken(c.key),
45+
}
46+
}
47+
48+
func (c *Client) IP() *IPService {
49+
return &IPService{
50+
resty: resty.New().SetHostURL("https://ip.labstack.com/api/v1").SetAuthToken(c.key),
51+
}
52+
}
53+
54+
func (c *Client) Webpage() *WebpageService {
55+
return &WebpageService{
56+
resty: resty.New().SetHostURL("https://webpage.labstack.com/api/v1").SetAuthToken(c.key),
57+
}
58+
}
59+
60+
func isError(status int) bool {
2961
return status < 200 || status >= 300
3062
}
3163

client_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ import "os"
44

55
var (
66
client = New(os.Getenv("KEY"))
7+
cs = client.Currency()
8+
ds = client.Domain()
9+
es = client.Email()
10+
is = client.IP()
11+
ws = client.Webpage()
712
)

currency.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package labstack
2+
3+
import (
4+
"github.com/go-resty/resty/v2"
5+
"github.com/labstack/labstack-go/currency"
6+
"strconv"
7+
)
8+
9+
type (
10+
CurrencyService struct {
11+
resty *resty.Client
12+
}
13+
)
14+
15+
func (c *CurrencyService) Convert(req *currency.ConvertRequest) (*currency.ConvertResponse, error) {
16+
res := new(currency.ConvertResponse)
17+
err := new(Error)
18+
r, e := c.resty.R().
19+
SetPathParams(map[string]string{
20+
"amount": strconv.FormatFloat(req.Amount, 'f', -1, 64),
21+
"from": req.From,
22+
"to": req.To,
23+
}).
24+
SetResult(res).
25+
SetError(err).
26+
Get("/convert/{amount}/{from}/{to}")
27+
if e != nil {
28+
return nil, &Error{
29+
Message: e.Error(),
30+
}
31+
}
32+
if isError(r.StatusCode()) {
33+
return nil, err
34+
}
35+
return res, nil
36+
}
37+
38+
func (c *CurrencyService) List(req *currency.ListRequest) (*currency.ListResponse, error) {
39+
res := new(currency.ListResponse)
40+
err := new(Error)
41+
r, e := c.resty.R().
42+
SetResult(res).
43+
SetError(err).
44+
Get("/list")
45+
if e != nil {
46+
return nil, &Error{
47+
Message: e.Error(),
48+
}
49+
}
50+
if isError(r.StatusCode()) {
51+
return nil, err
52+
}
53+
return res, nil
54+
}

currency/currency.go

-58
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
package currency
22

33
import (
4-
"github.com/go-resty/resty/v2"
5-
"github.com/labstack/labstack-go"
6-
"strconv"
74
"time"
85
)
96

107
type (
11-
Client struct {
12-
resty *resty.Client
13-
}
14-
158
Currency struct {
169
Name string `json:"name"`
1710
Code string `json:"code"`
@@ -36,54 +29,3 @@ type (
3629
Currencies []*Currency `json:"currencies"`
3730
}
3831
)
39-
40-
const (
41-
url = "https://currency.labstack.com/api/v1"
42-
)
43-
44-
func New(key string) *Client {
45-
return &Client{
46-
resty: resty.New().SetHostURL(url).SetAuthToken(key),
47-
}
48-
}
49-
50-
func (c *Client) Convert(req *ConvertRequest) (*ConvertResponse, error) {
51-
res := new(ConvertResponse)
52-
err := new(labstack.Error)
53-
r, e := c.resty.R().
54-
SetPathParams(map[string]string{
55-
"amount": strconv.FormatFloat(req.Amount, 'f', -1, 64),
56-
"from": req.From,
57-
"to": req.To,
58-
}).
59-
SetResult(res).
60-
SetError(err).
61-
Get("/convert/{amount}/{from}/{to}")
62-
if e != nil {
63-
return nil, &labstack.Error{
64-
Message: e.Error(),
65-
}
66-
}
67-
if labstack.IsError(r.StatusCode()) {
68-
return nil, err
69-
}
70-
return res, nil
71-
}
72-
73-
func (c *Client) List(req *ListRequest) (*ListResponse, error) {
74-
res := new(ListResponse)
75-
err := new(labstack.Error)
76-
r, e := c.resty.R().
77-
SetResult(res).
78-
SetError(err).
79-
Get("/list")
80-
if e != nil {
81-
return nil, &labstack.Error{
82-
Message: e.Error(),
83-
}
84-
}
85-
if labstack.IsError(r.StatusCode()) {
86-
return nil, err
87-
}
88-
return res, nil
89-
}

currency/currency_test.go renamed to currency_test.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
package currency
1+
package labstack
22

33
import (
4+
"github.com/labstack/labstack-go/currency"
45
"github.com/stretchr/testify/assert"
5-
"os"
66
"testing"
77
)
88

9-
var (
10-
client = New(os.Getenv("KEY"))
11-
)
12-
139
func TestClient_Convert(t *testing.T) {
14-
res, err := client.Convert(&ConvertRequest{
10+
res, err := cs.Convert(&currency.ConvertRequest{
1511
Amount: 10,
1612
From: "USD",
1713
To: "INR",
@@ -22,7 +18,7 @@ func TestClient_Convert(t *testing.T) {
2218
}
2319

2420
func TestClient_List(t *testing.T) {
25-
res, err := client.List(&ListRequest{})
21+
res, err := cs.List(&currency.ListRequest{})
2622
if assert.Nil(t, err) {
2723
assert.NotZero(t, len(res.Currencies))
2824
}

0 commit comments

Comments
 (0)