Skip to content

Commit 04d4159

Browse files
committed
Towards v1 (2)
Signed-off-by: Vishal Rana <[email protected]>
1 parent 86b2bd4 commit 04d4159

File tree

10 files changed

+307
-202
lines changed

10 files changed

+307
-202
lines changed

.idea/workspace.xml

Lines changed: 165 additions & 112 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

labstack.go renamed to client.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package labstack
22

3-
import "github.com/go-resty/resty/v2"
3+
import (
4+
"github.com/go-resty/resty/v2"
5+
"github.com/labstack/labstack-go/currency"
6+
)
47

58
type (
69
Client struct {
10+
key string
711
apiResty *resty.Client
812
currencyResty *resty.Client
913
domainResty *resty.Client
@@ -12,14 +16,11 @@ type (
1216
webpageResty *resty.Client
1317
}
1418

15-
Error struct {
16-
Code int `json:"code"`
17-
Message string `json:"message"`
18-
}
1919
)
2020

2121
func New(key string) *Client {
2222
return &Client{
23+
key: key,
2324
apiResty: resty.New().SetHostURL("https://api.labstack.com").SetAuthToken(key),
2425
currencyResty: resty.New().SetHostURL("https://currency.labstack.com/api/v1").SetAuthToken(key),
2526
domainResty: resty.New().SetHostURL("https://domain.labstack.com/api/v1").SetAuthToken(key),
@@ -29,10 +30,7 @@ func New(key string) *Client {
2930
}
3031
}
3132

32-
func isError(r *resty.Response) bool {
33-
return r.StatusCode() < 200 || r.StatusCode() >= 300
33+
func (c *Client) Currency() *currency.Client {
34+
return currency.New(c.key)
3435
}
3536

36-
func (e *Error) Error() string {
37-
return e.Message
38-
}
File renamed without changes.

common/common.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package common
2+
3+
import "github.com/go-resty/resty/v2"
4+
5+
type (
6+
Error struct {
7+
Code int `json:"code"`
8+
Message string `json:"message"`
9+
}
10+
)
11+
12+
func IsError(r *resty.Response) bool {
13+
return r.StatusCode() < 200 || r.StatusCode() >= 300
14+
}
15+
16+
func (e *Error) Error() string {
17+
return e.Message
18+
}

currency.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

currency/currency.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
package currency
22

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

currency_test.go renamed to currency/currency_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
package labstack
1+
package currency
22

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

9+
var (
10+
client = New(os.Getenv("KEY"))
11+
)
12+
913
func TestClient_CurrencyConvert(t *testing.T) {
10-
res, err := client.CurrencyConvert(&currency.ConvertRequest{
14+
res, err := client.Convert(&ConvertRequest{
1115
Amount: 10,
1216
From: "USD",
1317
To: "INR",
@@ -18,7 +22,7 @@ func TestClient_CurrencyConvert(t *testing.T) {
1822
}
1923

2024
func TestClient_CurrencyList(t *testing.T) {
21-
res, err := client.CurrencyList(&currency.ListRequest{})
25+
res, err := client.List(&ListRequest{})
2226
if assert.Nil(t, err) {
2327
assert.NotZero(t, len(res.Currencies))
2428
}

email.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

email/email.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
package email
22

3+
import (
4+
"github.com/go-resty/resty/v2"
5+
"github.com/labstack/labstack-go/common"
6+
)
7+
38
type (
9+
Client struct {
10+
resty *resty.Client
11+
}
12+
413
VerifyRequest struct {
514
Email string
615
}
@@ -13,3 +22,34 @@ type (
1322
Flags []string `json:"flags"`
1423
}
1524
)
25+
26+
const (
27+
url = "https://email.labstack.com/api/v1"
28+
)
29+
30+
func New(key string) *Client {
31+
return &Client{
32+
resty: resty.New().SetHostURL(url).SetAuthToken(key),
33+
}
34+
}
35+
36+
func (c *Client) Verify(req *VerifyRequest) (*VerifyResponse, *common.Error) {
37+
res := new(VerifyResponse)
38+
err := new(common.Error)
39+
r, e := c.resty.R().
40+
SetPathParams(map[string]string{
41+
"email": req.Email,
42+
}).
43+
SetResult(res).
44+
SetError(err).
45+
Get("/verify/{email}")
46+
if e != nil {
47+
return nil, &common.Error{
48+
Message: err.Error(),
49+
}
50+
}
51+
if common.IsError(r) {
52+
return nil, err
53+
}
54+
return res, nil
55+
}

email_test.go renamed to email/email_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
package labstack
1+
package email
22

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

9+
var (
10+
client = New(os.Getenv("KEY"))
11+
)
12+
913
func TestClient_EmailVerify(t *testing.T) {
10-
res, err := client.EmailVerify(&email.VerifyRequest{
14+
res, err := client.Verify(&VerifyRequest{
1115
1216
})
1317
if assert.Nil(t, err) {

0 commit comments

Comments
 (0)