Skip to content

Commit f908b42

Browse files
committed
Updated clients
1 parent 6c25d41 commit f908b42

16 files changed

+314
-146
lines changed

pkg/client/client.go

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

33
import (
4-
"bytes"
54
"encoding/json"
65
"encoding/xml"
76
"fmt"
@@ -129,32 +128,24 @@ func (client *Client) Do(in Payload, out any, opts ...RequestOpt) error {
129128
}(now)
130129

131130
// Make a request
132-
var body io.Reader
133131
var method string = http.MethodGet
134132
var accept, mimetype string
135133
if in != nil {
136-
if in.Type() != "" {
137-
data, err := json.Marshal(in)
138-
if err != nil {
139-
return err
140-
}
141-
body = bytes.NewReader(data)
142-
}
143134
method = in.Method()
144135
accept = in.Accept()
145136
mimetype = in.Type()
146137
}
147-
req, err := client.request(method, accept, mimetype, body)
138+
req, err := client.request(method, accept, mimetype, in)
148139
if err != nil {
149140
return err
150141
}
151142

152143
// If debug, then log the payload
153-
if debug, ok := client.Client.Transport.(*logtransport); ok {
154-
if body != nil {
155-
debug.Payload(in)
156-
}
157-
}
144+
//if debug, ok := client.Client.Transport.(*logtransport); ok {
145+
// if body != nil {
146+
// debug.Payload(in)
147+
// }
148+
//}
158149

159150
// If client token is set, then add to request
160151
if client.token.Scheme != "" && client.token.Value != "" {

pkg/client/payload.go

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,109 @@
11
package client
22

33
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
47
"net/http"
8+
"strconv"
59
)
610

711
///////////////////////////////////////////////////////////////////////////////
812
// TYPES
913

10-
type Payload interface {
11-
Type() string
12-
Method() string
13-
Accept() string
14-
}
15-
16-
type payload struct {
14+
type Request struct {
1715
method string
1816
accept string
1917
mimetype string
18+
buffer *bytes.Buffer
19+
}
20+
21+
type Payload interface {
22+
io.Reader
23+
24+
Method() string
25+
Accept() string
26+
Type() string
2027
}
2128

2229
///////////////////////////////////////////////////////////////////////////////
2330
// LIFECYCLE
2431

25-
func NewGetPayload(accept string) Payload {
26-
this := new(payload)
32+
// Return a new empty request which defaults to GET. The accept parameter is the
33+
// accepted mime-type of the response.
34+
func NewRequest(accept string) *Request {
35+
this := new(Request)
2736
this.method = http.MethodGet
28-
this.mimetype = ContentTypeJson
2937
this.accept = accept
3038
return this
3139
}
3240

41+
// Return a new request with a JSON payload which defaults to GET. The accept
42+
// parameter is the accepted mime-type of the response.
43+
func NewJSONRequest(payload any, accept string) (*Request, error) {
44+
this := new(Request)
45+
this.method = http.MethodGet
46+
this.mimetype = ContentTypeJson
47+
this.accept = accept
48+
this.buffer = new(bytes.Buffer)
49+
if err := json.NewEncoder(this.buffer).Encode(payload); err != nil {
50+
return nil, err
51+
}
52+
return this, nil
53+
}
54+
55+
///////////////////////////////////////////////////////////////////////////////
56+
// STRINGIFY
57+
58+
func (req *Request) String() string {
59+
str := "<payload"
60+
if req.method != "" {
61+
str += " method=" + strconv.Quote(req.method)
62+
}
63+
if req.accept != "" {
64+
str += " accept=" + strconv.Quote(req.accept)
65+
}
66+
if req.mimetype != "" {
67+
str += " mimetype=" + strconv.Quote(req.mimetype)
68+
}
69+
return str + ">"
70+
}
71+
3372
///////////////////////////////////////////////////////////////////////////////
3473
// PAYLOAD METHODS
3574

36-
func (payload *payload) Method() string {
37-
return payload.method
75+
// Set the HTTP method to POST
76+
func (req *Request) Post() *Request {
77+
req.method = http.MethodPost
78+
return req
79+
}
80+
81+
// Set the HTTP method to DELETE
82+
func (req *Request) Delete() *Request {
83+
req.method = http.MethodDelete
84+
return req
85+
}
86+
87+
// Return the HTTP method
88+
func (req *Request) Method() string {
89+
return req.method
90+
}
91+
92+
// Set the request mimetype
93+
func (req *Request) Type() string {
94+
return req.mimetype
3895
}
3996

40-
func (payload *payload) Accept() string {
41-
return payload.accept
97+
// Return the acceptable mimetype responses
98+
func (req *Request) Accept() string {
99+
return req.accept
42100
}
43101

44-
func (payload *payload) Type() string {
45-
return payload.mimetype
102+
// Implements the io.Reader interface for a payload
103+
func (req *Request) Read(b []byte) (n int, err error) {
104+
if req.buffer != nil {
105+
return req.buffer.Read(b)
106+
} else {
107+
return 0, io.EOF
108+
}
46109
}

pkg/client/payload_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package client_test
2+
3+
import (
4+
"testing"
5+
6+
// Packages
7+
"github.com/mutablelogic/go-client/pkg/client"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func Test_payload_001(t *testing.T) {
12+
assert := assert.New(t)
13+
payload := client.NewPayload(client.ContentTypeBinary)
14+
assert.NotNil(payload)
15+
assert.Equal("GET", payload.Method())
16+
assert.Equal(client.ContentTypeJson, payload.Type())
17+
assert.Equal(client.ContentTypeBinary, payload.Accept())
18+
}

pkg/client/transport.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ func (transport *logtransport) Payload(v interface{}) {
4848

4949
// RoundTrip is called as part of the request/response cycle
5050
func (transport *logtransport) RoundTrip(req *http.Request) (*http.Response, error) {
51-
fmt.Fprintln(transport.w, "req:", req.Method, redactedUrl(req.URL))
51+
fmt.Fprintln(transport.w, "request:", req.Method, redactedUrl(req.URL))
5252
if transport.v {
5353
for key := range req.Header {
54-
fmt.Fprintf(transport.w, " <= %v: %q\n", key, req.Header.Get(key))
54+
fmt.Fprintf(transport.w, " => %v: %q\n", key, req.Header.Get(key))
5555
}
5656
}
5757
then := time.Now()
@@ -60,11 +60,11 @@ func (transport *logtransport) RoundTrip(req *http.Request) (*http.Response, err
6060
}()
6161
resp, err := transport.RoundTripper.RoundTrip(req)
6262
if err != nil {
63-
fmt.Fprintln(transport.w, " => Error:", err)
63+
fmt.Fprintln(transport.w, "error:", err)
6464
} else {
65-
fmt.Fprintln(transport.w, " =>", resp.Status)
65+
fmt.Fprintln(transport.w, "response:", resp.Status)
6666
for k, v := range resp.Header {
67-
fmt.Fprintf(transport.w, " => %v: %q\n", k, v)
67+
fmt.Fprintf(transport.w, " <= %v: %q\n", k, v)
6868
}
6969
// If verbose is switched on, read the body
7070
if transport.v && resp.Body != nil {

pkg/ipify/client.go

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ to test the client package.
55
package ipify
66

77
import (
8-
"net/http"
98
"net/url"
109

1110
// Packages
@@ -19,35 +18,20 @@ type Client struct {
1918
*client.Client
2019
}
2120

22-
///////////////////////////////////////////////////////////////////////////////
23-
// GLOBALS
24-
25-
const (
26-
endPoint = "https://api.ipify.org/"
27-
)
28-
29-
///////////////////////////////////////////////////////////////////////////////
30-
// SCHEMA
31-
3221
type Request struct {
33-
client.Payload `json:"-"`
22+
client.Request
3423
}
3524

3625
type Response struct {
3726
IP string `json:"ip"`
3827
}
3928

40-
func (r Request) Method() string {
41-
return http.MethodGet
42-
}
43-
44-
func (r Request) Type() string {
45-
return ""
46-
}
29+
///////////////////////////////////////////////////////////////////////////////
30+
// GLOBALS
4731

48-
func (r Request) Accept() string {
49-
return client.ContentTypeJson
50-
}
32+
const (
33+
endPoint = "https://api.ipify.org/"
34+
)
5135

5236
///////////////////////////////////////////////////////////////////////////////
5337
// LIFECYCLE
@@ -70,30 +54,8 @@ func New(opts ...client.ClientOpt) (*Client, error) {
7054
// Get returns the current IP address from the API
7155
func (c *Client) Get() (Response, error) {
7256
var response Response
73-
if err := c.Do(nil, &response, client.OptQuery(url.Values{"format": []string{"json"}})); err != nil {
57+
if err := c.Do(client.NewRequest(client.ContentTypeJson), &response, client.OptQuery(url.Values{"format": []string{"json"}})); err != nil {
7458
return Response{}, err
7559
}
7660
return response, nil
7761
}
78-
79-
///////////////////////////////////////////////////////////////////////////////
80-
// WRITER IMPLEMENTATION
81-
82-
func (Response) Columns() []string {
83-
return []string{"IP"}
84-
}
85-
86-
func (r Response) Count() int {
87-
if r.IP == "" {
88-
return 0
89-
} else {
90-
return 1
91-
}
92-
}
93-
94-
func (r Response) Row(n int) []any {
95-
if n != 0 {
96-
return nil
97-
}
98-
return []any{r.IP}
99-
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)