Skip to content

Commit

Permalink
Merge pull request #6 from devilcove/develop
Browse files Browse the repository at this point in the history
Drop JSON method
  • Loading branch information
mattkasun authored Oct 23, 2022
2 parents 8b11441 + f4f0e37 commit d50ff0a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 49 deletions.
36 changes: 4 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* method with structure
## two options for results
* raw response (*http.Response)
* response body decoded to json
* response body decoded to json (func call only)
# Examples
## Full Response
equivalent of
Expand All @@ -15,7 +15,7 @@ equivalent of
```
func main() {
token := os.Getenv("API_TOKEN")
response, err := httpclient.API("", http.MethodGet, "https://api.example.com", token)
response, err := httpclient.GetResponse("", http.MethodGet, "https://api.example.com", token)
if err !=nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -43,7 +43,7 @@ func main() {
## JSON response
equivalent of

`curl 'https://api.example.com/api/login' -H 'Authorization: Bearer API_TOKEN' - H 'Content-Type: application/json' -d '{"name":"admin","pass":"password"}'`
`curl 'https://api.example.com/login' -H 'Authorization: Bearer API_TOKEN' - H 'Content-Type: application/json' -d '{"name":"admin","pass":"password"}'`
### function
```
func main() {
Expand All @@ -56,37 +56,9 @@ func main() {
Language_Pref: "EN",
JWT: "some string",
}
response, err := httpclient.JSON(data, http.MethodPOST, "https://api.example.com/api/login", token)
response, err := httpclient.GetJSON(data, http.MethodPOST, "https://api.example.com/api/login", "Bearer " + token)
if err !=nil {
log.Fatal(err)
}
log.Println(response)
}
```
## method
```
func main() {
token := os.Getenv("API_TOKEN")
data := struct {
Name: "admin",
Pass: "password",
}
response := struct {
Language_Pref: "EN",
JWT: "some string",
}
endpoint := httpclient.Endpoint {
URL: "https://api.clustercat.com",
Method: http.MethodPOST
Authorization: token,
Route: "/api/login",
Data: data,
Response: response,
}
response, err := httpclient.JSON()
if err !=nil {
log.Fatal(err)
}
log.Println(response)
}
```
20 changes: 9 additions & 11 deletions httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"net/http"
"time"

"github.com/kr/pretty"
)

var Client http.Client
Expand All @@ -17,7 +19,6 @@ type Endpoint struct {
Route string
Authorization string
Data any
Response any
}

func init() {
Expand All @@ -26,8 +27,8 @@ func init() {
}
}

// API returns respnse from http request to url
func API(data any, method, url, auth string) (*http.Response, error) {
// GetResponse returns respnse from http request to url
func GetResponse(data any, method, url, auth string) (*http.Response, error) {
var request *http.Request
var response *http.Response
var err error
Expand All @@ -54,24 +55,21 @@ func API(data any, method, url, auth string) (*http.Response, error) {
}

// JSON return JSON response from http request
func JSON[T any](data any, resp T, method, url, auth string) (any, error) {
response, err := API(data, method, url, auth)
func GetJSON[T any](data any, resp T, method, url, auth string) (any, error) {
response, err := GetResponse(data, method, url, auth)
if err != nil {
return nil, err
}
pretty.Println("response before json decodiing", resp)
defer response.Body.Close()
if err := json.NewDecoder(response.Body).Decode(&resp); err != nil {
return nil, err
}
pretty.Println("response after json decodiing", resp)
return resp, nil
}

// JSON return JSON response from http endpoint
func (e *Endpoint) JSON() (any, error) {
return JSON(e.Data, e.Response, e.Method, e.URL+e.Route, e.Authorization)
}

// GetResponse returns response from http endpoint
func (e *Endpoint) GetResponse() (*http.Response, error) {
return API(e.Data, e.Method, e.URL+e.Route, e.Authorization)
return GetResponse(e.Data, e.Method, e.URL+e.Route, e.Authorization)
}
12 changes: 6 additions & 6 deletions httpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/matryer/is"
)

func TestAPI(t *testing.T) {
func TestGetResponse(t *testing.T) {
t.Run("ip endpoint", func(t *testing.T) {
response, err := API(nil, http.MethodGet, "https://firefly.nusak.ca/ip", "")
response, err := GetResponse(nil, http.MethodGet, "https://firefly.nusak.ca/ip", "")
is := is.New(t)
is.NoErr(err)
is.Equal(response.StatusCode, http.StatusOK)
Expand All @@ -25,7 +25,7 @@ func TestAPI(t *testing.T) {
}
})
t.Run("invalid endpoint", func(t *testing.T) {
response, err := API(nil, http.MethodGet, "https://firefly.nusak.ca/invalidendpoint", "")
response, err := GetResponse(nil, http.MethodGet, "https://firefly.nusak.ca/invalidendpoint", "")
is := is.New(t)
is.NoErr(err)
is.Equal(response.StatusCode, http.StatusNotFound)
Expand All @@ -41,12 +41,12 @@ func TestAPI(t *testing.T) {
jwt := struct {
JWT string
}{}
response, err := API(data, http.MethodPost, "https://firefly.nusak.ca/login", "")
response, err := GetResponse(data, http.MethodPost, "https://firefly.nusak.ca/login", "")
is := is.New(t)
is.NoErr(err)
defer response.Body.Close()
is.NoErr(json.NewDecoder(response.Body).Decode(&jwt))
response, err = API("", http.MethodGet, "https://firefly.nusak.ca/api/hello", jwt.JWT)
response, err = GetResponse("", http.MethodGet, "https://firefly.nusak.ca/api/hello", jwt.JWT)
is.NoErr(err)
is.Equal(response.StatusCode, http.StatusOK)
defer response.Body.Close()
Expand All @@ -69,7 +69,7 @@ func TestAPI(t *testing.T) {
}{}
var data Data
data.Pass = "badpass"
response, err := API(data, http.MethodPost, "https://firefly.nusak.ca/login", "")
response, err := GetResponse(data, http.MethodPost, "https://firefly.nusak.ca/login", "")
is := is.New(t)
is.NoErr(err)
defer response.Body.Close()
Expand Down

0 comments on commit d50ff0a

Please sign in to comment.