feat: add Apps endpoint and query parameter support
- add `Apps` endpoint to the `Client` struct and initialize it in `NewClient` - implement `doWithJsonResponse` helper to handle request execution and JSON decoding in one step - update `newRequest` to support passing and encoding query parameters via `net/url` - rename `Option` to `ClientOption` for better clarity in the `NewClient` signature - refactor `Ping` tests to use a more structured nested layout - remove `option.go` and `option_test.go` as part of the configuration refactor
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package onspring
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
appsPath = "/apps"
|
||||
)
|
||||
|
||||
type AppsEndpoint struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
type PagingRequest struct {
|
||||
pageSize int
|
||||
pageNumber int
|
||||
}
|
||||
|
||||
func (pr *PagingRequest) ToParams() map[string]string {
|
||||
return map[string]string{
|
||||
"pageSize": strconv.Itoa(pr.pageNumber),
|
||||
"pageNumber": strconv.Itoa(pr.pageSize),
|
||||
}
|
||||
}
|
||||
|
||||
type PagingOption func(*PagingRequest)
|
||||
|
||||
type Page[T any] struct {
|
||||
PageNumber int `json:"pageNumber"`
|
||||
PageSize int `json:"pageSize"`
|
||||
TotalPages int `json:"totalPages"`
|
||||
TotalRecords int `json:"totalRecords"`
|
||||
Items []T `json:"items"`
|
||||
}
|
||||
|
||||
type App struct {
|
||||
Href string `json:"href"`
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (p *AppsEndpoint) Get(ctx context.Context, pagingOpts ...PagingOption) (Page[App], error) {
|
||||
pagingRequest := &PagingRequest{
|
||||
pageSize: 1,
|
||||
pageNumber: 50,
|
||||
}
|
||||
|
||||
for _, opt := range pagingOpts {
|
||||
opt(pagingRequest)
|
||||
}
|
||||
|
||||
req, requestCreationErr := p.client.newRequest(ctx, http.MethodGet, appsPath, pagingRequest.ToParams(), nil)
|
||||
|
||||
var page Page[App]
|
||||
|
||||
if requestCreationErr != nil {
|
||||
return page, requestCreationErr
|
||||
}
|
||||
|
||||
responseErr := p.client.doWithJsonResponse(req, &page)
|
||||
|
||||
if responseErr != nil {
|
||||
return page, responseErr
|
||||
}
|
||||
|
||||
return page, nil
|
||||
}
|
||||
Reference in New Issue
Block a user