refactor: correct method names

This commit is contained in:
Stevan Freeborn
2026-03-23 10:30:35 -05:00
parent 02351d89db
commit 6361e41cf3
2 changed files with 36 additions and 27 deletions
+15 -6
View File
@@ -29,7 +29,7 @@ type AppBatch struct {
Items []App `json:"items"` Items []App `json:"items"`
} }
// Get retrieves a paginated list of apps from the Onspring API. // List retrieves a paginated list of apps from the Onspring API.
// //
// Parameters: // Parameters:
// - ctx: The context for the request // - ctx: The context for the request
@@ -38,7 +38,7 @@ type AppBatch struct {
// Returns: // Returns:
// - Page[App]: A page of apps with pagination metadata // - Page[App]: A page of apps with pagination metadata
// - error: An error if the request fails // - error: An error if the request fails
func (a *AppsEndpoint) Get(ctx context.Context, pagingOpts ...PagingOption) (Page[App], error) { func (a *AppsEndpoint) List(ctx context.Context, pagingOpts ...PagingOption) (Page[App], error) {
pagingRequest := createPagingRequest(pagingOpts) pagingRequest := createPagingRequest(pagingOpts)
req, requestCreationErr := a.client.newRequest(ctx, http.MethodGet, appsPath, pagingRequest.ToParams(), nil) req, requestCreationErr := a.client.newRequest(ctx, http.MethodGet, appsPath, pagingRequest.ToParams(), nil)
@@ -58,7 +58,7 @@ func (a *AppsEndpoint) Get(ctx context.Context, pagingOpts ...PagingOption) (Pag
return page, nil return page, nil
} }
// GetAll returns an iterator that yields all Apps across all pages. // ListAll returns an iterator that yields all Apps across all pages.
// It automatically handles pagination by making sequential calls to Get // It automatically handles pagination by making sequential calls to Get
// until all items have been retrieved or the caller stops the iteration. // until all items have been retrieved or the caller stops the iteration.
// //
@@ -74,12 +74,12 @@ func (a *AppsEndpoint) Get(ctx context.Context, pagingOpts ...PagingOption) (Pag
// - iter.Seq2[App, error]: An iterator yielding: // - iter.Seq2[App, error]: An iterator yielding:
// - App: The individual application record. // - App: The individual application record.
// - error: An error if a specific page request fails during iteration. // - error: An error if a specific page request fails during iteration.
func (a *AppsEndpoint) GetAll(ctx context.Context, pagingOpts ...PagingOption) iter.Seq2[App, error] { func (a *AppsEndpoint) ListAll(ctx context.Context, pagingOpts ...PagingOption) iter.Seq2[App, error] {
return func(yield func(App, error) bool) { return func(yield func(App, error) bool) {
pagingRequest := createPagingRequest(pagingOpts) pagingRequest := createPagingRequest(pagingOpts)
for { for {
page, err := a.Get(ctx, ForPageNumber(pagingRequest.PageNumber), WithPageSize(pagingRequest.PageSize)) page, err := a.List(ctx, ForPageNumber(pagingRequest.PageNumber), WithPageSize(pagingRequest.PageSize))
if err != nil { if err != nil {
yield(App{}, err) yield(App{}, err)
@@ -101,7 +101,16 @@ func (a *AppsEndpoint) GetAll(ctx context.Context, pagingOpts ...PagingOption) i
} }
} }
func (a *AppsEndpoint) GetBatch(ctx context.Context, appIds []int) (AppBatch, error) { // GetMany retrieves a batch of apps from the Onspring API.
//
// Parameters:
// - ctx: The context for the request
// - appIds: The ids of the apps to retrieve
//
// Returns:
// - AppBatch: A batch of apps
// - error: An error if the request fails
func (a *AppsEndpoint) GetMany(ctx context.Context, appIds []int) (AppBatch, error) {
req, requestCreationErr := a.client.newRequest(ctx, http.MethodPost, appsBatchPath, nil, appIds) req, requestCreationErr := a.client.newRequest(ctx, http.MethodPost, appsBatchPath, nil, appIds)
var appBatch AppBatch var appBatch AppBatch
+21 -21
View File
@@ -13,7 +13,7 @@ import (
) )
func TestApps(t *testing.T) { func TestApps(t *testing.T) {
t.Run("Get", func(t *testing.T) { t.Run("List", func(t *testing.T) {
t.Run("it should return an error if context is nil", func(t *testing.T) { t.Run("it should return an error if context is nil", func(t *testing.T) {
_, client := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) { _, client := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
@@ -21,7 +21,7 @@ func TestApps(t *testing.T) {
var nilContext context.Context = nil var nilContext context.Context = nil
_, err := client.Apps.Get(nilContext) _, err := client.Apps.List(nilContext)
if err == nil { if err == nil {
t.Errorf("Expected error for nil context, got nil") t.Errorf("Expected error for nil context, got nil")
@@ -37,7 +37,7 @@ func TestApps(t *testing.T) {
cancel() cancel()
_, err := client.Apps.Get(ctx) _, err := client.Apps.List(ctx)
if err == nil { if err == nil {
t.Errorf("Expected error for canceled context, got nil") t.Errorf("Expected error for canceled context, got nil")
@@ -51,7 +51,7 @@ func TestApps(t *testing.T) {
onspring.WithHTTPClient(&http.Client{Transport: &ErrorTransport{}}), onspring.WithHTTPClient(&http.Client{Transport: &ErrorTransport{}}),
) )
_, err := client.Apps.Get(t.Context()) _, err := client.Apps.List(t.Context())
if err == nil { if err == nil {
t.Errorf("Expected network error, got nil") t.Errorf("Expected network error, got nil")
@@ -69,7 +69,7 @@ func TestApps(t *testing.T) {
onspring.WithHTTPClient(client.HTTPClient()), onspring.WithHTTPClient(client.HTTPClient()),
) )
_, err := invalidClient.Apps.Get(t.Context()) _, err := invalidClient.Apps.List(t.Context())
if err == nil { if err == nil {
t.Errorf("Expected request creation error, got nil") t.Errorf("Expected request creation error, got nil")
@@ -121,7 +121,7 @@ func TestApps(t *testing.T) {
w.Write(jsonData) w.Write(jsonData)
}) })
page, err := client.Apps.Get(t.Context()) page, err := client.Apps.List(t.Context())
if err != nil { if err != nil {
t.Errorf("Expected no error, got %v", err) t.Errorf("Expected no error, got %v", err)
@@ -159,7 +159,7 @@ func TestApps(t *testing.T) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
}) })
client.Apps.Get( client.Apps.List(
t.Context(), t.Context(),
onspring.ForPageNumber(expectedPageNumber), onspring.ForPageNumber(expectedPageNumber),
onspring.WithPageSize(expectedPageSize), onspring.WithPageSize(expectedPageSize),
@@ -171,7 +171,7 @@ func TestApps(t *testing.T) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
}) })
_, err := client.Apps.Get(t.Context()) _, err := client.Apps.List(t.Context())
if err == nil { if err == nil {
t.Errorf("Expected error, got nil") t.Errorf("Expected error, got nil")
@@ -179,13 +179,13 @@ func TestApps(t *testing.T) {
}) })
}) })
t.Run("GetAll", func(t *testing.T) { t.Run("ListAll", func(t *testing.T) {
t.Run("it should return an error if fails to retrieve any pages of apps", func(t *testing.T) { t.Run("it should return an error if fails to retrieve any pages of apps", func(t *testing.T) {
_, client := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) { _, client := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
}) })
for _, err := range client.Apps.GetAll(t.Context()) { for _, err := range client.Apps.ListAll(t.Context()) {
if err == nil { if err == nil {
t.Errorf("Expected error, got nil") t.Errorf("Expected error, got nil")
} }
@@ -252,7 +252,7 @@ func TestApps(t *testing.T) {
retrievedApps := []onspring.App{} retrievedApps := []onspring.App{}
for app, _ := range client.Apps.GetAll(t.Context()) { for app, _ := range client.Apps.ListAll(t.Context()) {
retrievedApps = append(retrievedApps, app) retrievedApps = append(retrievedApps, app)
} }
@@ -305,7 +305,7 @@ func TestApps(t *testing.T) {
retrievedApps := []onspring.App{} retrievedApps := []onspring.App{}
encounteredErrors := []error{} encounteredErrors := []error{}
for app, err := range client.Apps.GetAll(t.Context()) { for app, err := range client.Apps.ListAll(t.Context()) {
if err != nil { if err != nil {
encounteredErrors = append(encounteredErrors, err) encounteredErrors = append(encounteredErrors, err)
} else { } else {
@@ -361,7 +361,7 @@ func TestApps(t *testing.T) {
retrievedApps := []onspring.App{} retrievedApps := []onspring.App{}
for app, _ := range client.Apps.GetAll(t.Context(), onspring.ForPageNumber(2)) { for app, _ := range client.Apps.ListAll(t.Context(), onspring.ForPageNumber(2)) {
retrievedApps = append(retrievedApps, app) retrievedApps = append(retrievedApps, app)
} }
@@ -414,7 +414,7 @@ func TestApps(t *testing.T) {
retrievedApps := []onspring.App{} retrievedApps := []onspring.App{}
for app, _ := range client.Apps.GetAll(t.Context(), onspring.WithPageSize(2)) { for app, _ := range client.Apps.ListAll(t.Context(), onspring.WithPageSize(2)) {
retrievedApps = append(retrievedApps, app) retrievedApps = append(retrievedApps, app)
} }
@@ -424,7 +424,7 @@ func TestApps(t *testing.T) {
}) })
}) })
t.Run("GetBatch", func(t *testing.T) { t.Run("GetMany", func(t *testing.T) {
t.Run("it should return an error if context is nil", func(t *testing.T) { t.Run("it should return an error if context is nil", func(t *testing.T) {
_, client := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) { _, client := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
@@ -432,7 +432,7 @@ func TestApps(t *testing.T) {
var nilContext context.Context = nil var nilContext context.Context = nil
_, err := client.Apps.GetBatch(nilContext, []int{}) _, err := client.Apps.GetMany(nilContext, []int{})
if err == nil { if err == nil {
t.Errorf("Expected error for nil context, got nil") t.Errorf("Expected error for nil context, got nil")
@@ -448,7 +448,7 @@ func TestApps(t *testing.T) {
cancel() cancel()
_, err := client.Apps.GetBatch(ctx, []int{}) _, err := client.Apps.GetMany(ctx, []int{})
if err == nil { if err == nil {
t.Errorf("Expected error for canceled context, got nil") t.Errorf("Expected error for canceled context, got nil")
@@ -462,7 +462,7 @@ func TestApps(t *testing.T) {
onspring.WithHTTPClient(&http.Client{Transport: &ErrorTransport{}}), onspring.WithHTTPClient(&http.Client{Transport: &ErrorTransport{}}),
) )
_, err := client.Apps.GetBatch(t.Context(), []int{}) _, err := client.Apps.GetMany(t.Context(), []int{})
if err == nil { if err == nil {
t.Errorf("Expected network error, got nil") t.Errorf("Expected network error, got nil")
@@ -480,7 +480,7 @@ func TestApps(t *testing.T) {
onspring.WithHTTPClient(client.HTTPClient()), onspring.WithHTTPClient(client.HTTPClient()),
) )
_, err := invalidClient.Apps.GetBatch(t.Context(), []int{}) _, err := invalidClient.Apps.GetMany(t.Context(), []int{})
if err == nil { if err == nil {
t.Errorf("Expected request creation error, got nil") t.Errorf("Expected request creation error, got nil")
@@ -492,7 +492,7 @@ func TestApps(t *testing.T) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
}) })
_, err := client.Apps.GetBatch(t.Context(), []int{}) _, err := client.Apps.GetMany(t.Context(), []int{})
if err == nil { if err == nil {
t.Errorf("Expected error, got nil") t.Errorf("Expected error, got nil")
@@ -529,7 +529,7 @@ func TestApps(t *testing.T) {
w.Write(jsonData) w.Write(jsonData)
}) })
batch, _ := client.Apps.GetBatch(t.Context(), []int{apps[0].Id}) batch, _ := client.Apps.GetMany(t.Context(), []int{apps[0].Id})
if !reflect.DeepEqual(expectedBatch, batch) { if !reflect.DeepEqual(expectedBatch, batch) {
t.Errorf("Expected %v but got %v", expectedBatch, batch) t.Errorf("Expected %v but got %v", expectedBatch, batch)