@@ -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.
1314type Client interface {
1415 SearchRikishisAPI
16+ ListRankChangesAPI
17+ ListShikonaChangesAPI
18+ ListMeasurementChangesAPI
1519}
1620
1721type 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+ }
0 commit comments