https://github.com/kpurdon/apir
A simple package for making/consuming api[r] requests/responses.
https://github.com/kpurdon/apir
api go http
Last synced: about 1 year ago
JSON representation
A simple package for making/consuming api[r] requests/responses.
- Host: GitHub
- URL: https://github.com/kpurdon/apir
- Owner: kpurdon
- License: mit
- Created: 2020-11-11T16:07:06.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-06-24T22:06:08.000Z (almost 2 years ago)
- Last Synced: 2025-01-02T13:28:02.749Z (over 1 year ago)
- Topics: api, go, http
- Language: Go
- Homepage:
- Size: 36.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://pkg.go.dev/github.com/kpurdon/apir)
[](https://goreportcard.com/report/github.com/kpurdon/apir)
[](https://codecov.io/gh/kpurdon/apir)
apir
-----
A simple package for making/consuming api[r] requests/responses.
## Installation
`go get github.com/kpurdon/apir`
## Getting Started
### Initialize a Client
```go
client := requester.NewClient("myservice")
client.MustAddAPI("otherservice", discoverer.NewDirect("http://foo.com/api"),
requester.WithRetry(),
requester.WithContentType(requester.ApplicationJSON),
)
```
### Create a Request
```go
req, err := client.NewRequest(ctx, "otherservice", http.MethodGet, "/", nil)
if err != nil {
// an error occured trying to make the request
// handle the error
}
```
### Execute the Request
```go
var (
data struct{}
errData struct{}
)
ok, err := client.Execute(req, &data, &errData)
if err != nil {
// an error occured trying to executre the request
// handle the error
return
}
if !ok {
// we made the request, but got a >= 400 status code
// examine errData for error details
return
}
// the request was made and returned a < 400 status code
// examine data for the response
```