Skip to content

Commit 0028e69

Browse files
authored
Merge pull request #11 from devilcove/develop
refactor jsonEndpoint
2 parents 8745d41 + 0919861 commit 0028e69

3 files changed

Lines changed: 16 additions & 18 deletions

File tree

examples/method/json/main.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ type Response struct {
1414

1515
func main() {
1616
var response Response
17-
end := httpclient.Endpoint{
17+
endpoint := httpclient.JSONEndpoint[Response]{
1818
URL: "https://api.ipify.org?format=json",
1919
Route: "",
2020
Method: http.MethodGet,
2121
Authorization: "",
2222
Data: nil,
23-
}
24-
endpoint := httpclient.JSONEndpoint[Response]{
25-
Endpoint: end,
26-
Response: response,
23+
Response: response,
2724
}
2825
answer, err := endpoint.GetJSON(response)
2926
if err != nil {

httpclient.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ type Endpoint struct {
1919
Data any
2020
}
2121

22-
type JSONEndpoint[T comparable] struct {
23-
Endpoint
24-
Response T
22+
type JSONEndpoint[T any] struct {
23+
URL string
24+
Method string
25+
Route string
26+
Authorization string
27+
Data any
28+
Response T
2529
}
2630

2731
func init() {

httpclient_test.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,14 @@ func TestGetJSON(t *testing.T) {
9696
response := struct {
9797
JWT string
9898
}{}
99-
e := Endpoint{
100-
URL: "http://firefly.nusak.ca",
101-
Route: "/login",
102-
Method: http.MethodPost,
103-
Data: data,
99+
e := JSONEndpoint[struct{ JWT string }]{
100+
URL: "http://firefly.nusak.ca",
101+
Route: "/login",
102+
Method: http.MethodPost,
103+
Data: data,
104+
Response: response,
104105
}
105-
g := JSONEndpoint[struct{ JWT string }]{
106-
e,
107-
response,
108-
}
109-
answer, err := g.GetJSON(response)
106+
answer, err := e.GetJSON(response)
110107
is.NoErr(err)
111108
answerType := fmt.Sprintf("%T", answer)
112109
is.True(answerType == "struct { JWT string }")

0 commit comments

Comments
 (0)