Skip to content

Commit 480aed7

Browse files
committed
Towards v1 (3)
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent 04d4159 commit 480aed7

File tree

18 files changed

+533
-492
lines changed

18 files changed

+533
-492
lines changed

.idea/workspace.xml

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

client.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,33 @@ package labstack
22

33
import (
44
"github.com/go-resty/resty/v2"
5-
"github.com/labstack/labstack-go/currency"
65
)
76

87
type (
98
Client struct {
10-
key string
11-
apiResty *resty.Client
12-
currencyResty *resty.Client
13-
domainResty *resty.Client
14-
emailResty *resty.Client
15-
ipResty *resty.Client
16-
webpageResty *resty.Client
9+
resty *resty.Client
1710
}
1811

12+
Error struct {
13+
Code int `json:"code"`
14+
Message string `json:"message"`
15+
}
16+
)
17+
18+
const (
19+
url = "https://api.labstack.com"
1920
)
2021

2122
func New(key string) *Client {
2223
return &Client{
23-
key: key,
24-
apiResty: resty.New().SetHostURL("https://api.labstack.com").SetAuthToken(key),
25-
currencyResty: resty.New().SetHostURL("https://currency.labstack.com/api/v1").SetAuthToken(key),
26-
domainResty: resty.New().SetHostURL("https://domain.labstack.com/api/v1").SetAuthToken(key),
27-
emailResty: resty.New().SetHostURL("https://email.labstack.com/api/v1").SetAuthToken(key),
28-
ipResty: resty.New().SetHostURL("https://ip.labstack.com/api/v1").SetAuthToken(key),
29-
webpageResty: resty.New().SetHostURL("https://webpage.labstack.com/api/v1").SetAuthToken(key),
24+
resty: resty.New().SetHostURL(url).SetAuthToken(key),
3025
}
3126
}
3227

33-
func (c *Client) Currency() *currency.Client {
34-
return currency.New(c.key)
28+
func IsError(status int) bool {
29+
return status < 200 || status >= 300
3530
}
3631

32+
func (e *Error) Error() string {
33+
return e.Message
34+
}

common/common.go

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

currency/currency.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package currency
22

33
import (
44
"github.com/go-resty/resty/v2"
5-
"github.com/labstack/labstack-go/common"
5+
"github.com/labstack/labstack-go"
66
"strconv"
77
)
88

@@ -46,9 +46,9 @@ func New(key string) *Client {
4646
}
4747
}
4848

49-
func (c *Client) Convert(req *ConvertRequest) (*ConvertResponse, *common.Error) {
49+
func (c *Client) Convert(req *ConvertRequest) (*ConvertResponse, error) {
5050
res := new(ConvertResponse)
51-
err := new(common.Error)
51+
err := new(labstack.Error)
5252
r, e := c.resty.R().
5353
SetPathParams(map[string]string{
5454
"amount": strconv.FormatFloat(req.Amount, 'f', -1, 64),
@@ -59,29 +59,29 @@ func (c *Client) Convert(req *ConvertRequest) (*ConvertResponse, *common.Error)
5959
SetError(err).
6060
Get("/convert/{amount}/{from}/{to}")
6161
if e != nil {
62-
return nil, &common.Error{
62+
return nil, &labstack.Error{
6363
Message: e.Error(),
6464
}
6565
}
66-
if common.IsError(r) {
66+
if labstack.IsError(r.StatusCode()) {
6767
return nil, err
6868
}
6969
return res, nil
7070
}
7171

72-
func (c *Client) List(req *ListRequest) (*ListResponse, *common.Error) {
72+
func (c *Client) List(req *ListRequest) (*ListResponse, error) {
7373
res := new(ListResponse)
74-
err := new(common.Error)
74+
err := new(labstack.Error)
7575
r, e := c.resty.R().
7676
SetResult(res).
7777
SetError(err).
7878
Get("/list")
7979
if e != nil {
80-
return nil, &common.Error{
80+
return nil, &labstack.Error{
8181
Message: e.Error(),
8282
}
8383
}
84-
if common.IsError(r) {
84+
if labstack.IsError(r.StatusCode()) {
8585
return nil, err
8686
}
8787
return res, nil

currency/currency_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var (
1010
client = New(os.Getenv("KEY"))
1111
)
1212

13-
func TestClient_CurrencyConvert(t *testing.T) {
13+
func TestClient_Convert(t *testing.T) {
1414
res, err := client.Convert(&ConvertRequest{
1515
Amount: 10,
1616
From: "USD",
@@ -21,7 +21,7 @@ func TestClient_CurrencyConvert(t *testing.T) {
2121
}
2222
}
2323

24-
func TestClient_CurrencyList(t *testing.T) {
24+
func TestClient_List(t *testing.T) {
2525
res, err := client.List(&ListRequest{})
2626
if assert.Nil(t, err) {
2727
assert.NotZero(t, len(res.Currencies))

domain.go

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

domain/domain.go

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

3+
import (
4+
"github.com/go-resty/resty/v2"
5+
"github.com/labstack/labstack-go"
6+
)
7+
38
type (
9+
Client struct {
10+
resty *resty.Client
11+
}
12+
413
Record struct {
514
Domain string `json:"domain"`
615
Type string `json:"type"`
@@ -100,3 +109,98 @@ type (
100109
Raw string `json:"raw"`
101110
}
102111
)
112+
113+
const (
114+
url = "https://domain.labstack.com/api/v1"
115+
)
116+
117+
func New(key string) *Client {
118+
return &Client{
119+
resty: resty.New().SetHostURL(url).SetAuthToken(key),
120+
}
121+
}
122+
123+
func (c *Client) DNS(req *DNSRequest) (*DNSResponse, error) {
124+
res := new(DNSResponse)
125+
err := new(labstack.Error)
126+
r, e := c.resty.R().
127+
SetPathParams(map[string]string{
128+
"type": req.Type,
129+
"domain": req.Domain,
130+
}).
131+
SetResult(res).
132+
SetError(err).
133+
Get("/{type}/{domain}")
134+
if e != nil {
135+
return nil, &labstack.Error{
136+
Message: err.Error(),
137+
}
138+
}
139+
if labstack.IsError(r.StatusCode()) {
140+
return nil, err
141+
}
142+
return res, nil
143+
}
144+
145+
func (c *Client) Search(req *SearchRequest) (*SearchResponse, error) {
146+
res := new(SearchResponse)
147+
err := new(labstack.Error)
148+
r, e := c.resty.R().
149+
SetPathParams(map[string]string{
150+
"domain": req.Domain,
151+
}).
152+
SetResult(res).
153+
SetError(err).
154+
Get("/search/{domain}")
155+
if e != nil {
156+
return nil, &labstack.Error{
157+
Message: err.Error(),
158+
}
159+
}
160+
if labstack.IsError(r.StatusCode()) {
161+
return nil, err
162+
}
163+
return res, nil
164+
}
165+
166+
func (c *Client) Status(req *StatusRequest) (*StatusResponse, error) {
167+
res := new(StatusResponse)
168+
err := new(labstack.Error)
169+
r, e := c.resty.R().
170+
SetPathParams(map[string]string{
171+
"domain": req.Domain,
172+
}).
173+
SetResult(res).
174+
SetError(err).
175+
Get("/status/{domain}")
176+
if e != nil {
177+
return nil, &labstack.Error{
178+
Message: err.Error(),
179+
}
180+
}
181+
if labstack.IsError(r.StatusCode()) {
182+
return nil, err
183+
}
184+
return res, nil
185+
}
186+
187+
func (c *Client) Whois(req *WhoisRequest) (*WhoisResponse, error) {
188+
res := new(WhoisResponse)
189+
err := new(labstack.Error)
190+
r, e := c.resty.R().
191+
SetPathParams(map[string]string{
192+
"domain": req.Domain,
193+
}).
194+
SetResult(res).
195+
SetError(err).
196+
Get("/whois/{domain}")
197+
if e != nil {
198+
return nil, &labstack.Error{
199+
Message: err.Error(),
200+
}
201+
}
202+
if labstack.IsError(r.StatusCode()) {
203+
return nil, err
204+
}
205+
return res, nil
206+
}

domain/domain_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package domain
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"os"
6+
"testing"
7+
)
8+
9+
var (
10+
client = New(os.Getenv("KEY"))
11+
)
12+
13+
func TestClient_DNS(t *testing.T) {
14+
res, err := client.DNS(&DNSRequest{
15+
Type: "A",
16+
Domain: "twilio.com",
17+
})
18+
if assert.Nil(t, err) {
19+
assert.NotZero(t, len(res.Records))
20+
}
21+
}
22+
23+
func TestClient_Search(t *testing.T) {
24+
res, err := client.Search(&SearchRequest{
25+
Domain: "twilio.com",
26+
})
27+
if assert.Nil(t, err) {
28+
assert.NotZero(t, len(res.Results))
29+
}
30+
}
31+
32+
func TestClient_Status(t *testing.T) {
33+
res, err := client.Status(&StatusRequest{
34+
Domain: "twilio.com",
35+
})
36+
if assert.Nil(t, err) {
37+
assert.Equal(t, "unavailable", res.Result)
38+
}
39+
}
40+
41+
func TestClient_Whois(t *testing.T) {
42+
res, err := client.Whois(&WhoisRequest{
43+
Domain: "twilio.com",
44+
})
45+
if assert.Nil(t, err) {
46+
assert.NotEmpty(t, res.Raw)
47+
}
48+
}

0 commit comments

Comments
 (0)