Skip to content

Commit bbc3157

Browse files
authored
Merge pull request #2 from sumo-mcp/workspace
Add basho rikishi changes APIs
2 parents 54b2bd7 + e4fd5e8 commit bbc3157

21 files changed

Lines changed: 1243 additions & 173 deletions

basho.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ type BashoID struct {
1212
Month int // Month is 1-12.
1313
}
1414

15+
func (b BashoID) String() string {
16+
return fmt.Sprintf("%04d%02d", b.Year, b.Month)
17+
}
18+
1519
func (b BashoID) MarshalJSON() ([]byte, error) {
16-
return []byte(`"` + fmt.Sprintf("%04d%02d", b.Year, b.Month) + `"`), nil
20+
return []byte(`"` + b.String() + `"`), nil
1721
}
1822

1923
func (b *BashoID) UnmarshalJSON(data []byte) error {

basho_rikishi_id.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

basho_rikishi_id_test.go

Lines changed: 0 additions & 105 deletions
This file was deleted.

client.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import (
77
"io"
88
"net/http"
99
"net/url"
10+
"strings"
1011
)
1112

1213
// Client is a client for the Sumo API.
1314
type Client interface {
1415
SearchRikishisAPI
16+
ListRankChangesAPI
17+
ListShikonaChangesAPI
18+
ListMeasurementChangesAPI
1519
}
1620

1721
type client struct {
@@ -39,13 +43,22 @@ func New(opts ...Option) Client {
3943
return client
4044
}
4145

42-
func doRequest[obj any](ctx context.Context, c *client, method, path string, query url.Values) (*obj, error) {
46+
func (c *client) doRequest(ctx context.Context, method, path string, query url.Values, obj any) ([]byte, error) {
4347
u := fmt.Sprintf("https://sumo-api.com/api%s", path)
4448
if len(query) > 0 {
4549
u = fmt.Sprintf("%s?%s", u, query.Encode())
4650
}
4751

48-
req, err := http.NewRequestWithContext(ctx, method, u, nil)
52+
var body io.Reader
53+
if obj != nil {
54+
b, err := json.Marshal(obj)
55+
if err != nil {
56+
return nil, fmt.Errorf("error marshaling request body: %w", err)
57+
}
58+
body = strings.NewReader(string(b))
59+
}
60+
61+
req, err := http.NewRequestWithContext(ctx, method, u, body)
4962
if err != nil {
5063
return nil, fmt.Errorf("error creating http request: %w", err)
5164
}
@@ -74,10 +87,36 @@ func doRequest[obj any](ctx context.Context, c *client, method, path string, que
7487
return nil, fmt.Errorf("error reading response body: %w", err)
7588
}
7689

90+
return b, nil
91+
}
92+
93+
func getObject[obj any](ctx context.Context, c *client, path string, query url.Values) (*obj, error) {
94+
b, err := c.doRequest(ctx, http.MethodGet, path, query, nil)
95+
if err != nil {
96+
return nil, err
97+
}
7798
var o obj
7899
if err := json.Unmarshal(b, &o); err != nil {
79100
return nil, fmt.Errorf("error unmarshaling response body: %w", err)
80101
}
81-
82102
return &o, nil
83103
}
104+
105+
func listObjects[obj any](ctx context.Context, c *client, path string, query url.Values) ([]obj, error) {
106+
b, err := c.doRequest(ctx, http.MethodGet, path, query, nil)
107+
if err != nil {
108+
return nil, err
109+
}
110+
var l []obj
111+
if err := json.Unmarshal(b, &l); err != nil {
112+
return nil, fmt.Errorf("error unmarshaling response body: %w", err)
113+
}
114+
return l, nil
115+
}
116+
117+
func getSortOrder(order string) string {
118+
if o := strings.ToLower(strings.TrimSpace(order)); o == "asc" || o == "desc" {
119+
return o
120+
}
121+
return ""
122+
}

list_measurement_changes.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package sumoapi
2+
3+
import "context"
4+
5+
// ListMeasurementChangesAPI defines the methods available for listing Rikishi measurement changes across bashos (sumo tournaments).
6+
type ListMeasurementChangesAPI interface {
7+
// ListMeasurementChanges calls the GET /api/measurements endpoint.
8+
ListMeasurementChanges(ctx context.Context, req ListRikishiChangesRequest) ([]Measurement, error)
9+
}
10+
11+
func (c *client) ListMeasurementChanges(ctx context.Context, req ListRikishiChangesRequest) ([]Measurement, error) {
12+
return listRikishiChanges[Measurement](ctx, c, "/measurements", req)
13+
}

0 commit comments

Comments
 (0)