@@ -81,6 +81,44 @@ type ModelOptions struct {
8181 Skipgrams int `json:"skipgrams,omitempty"`
8282}
8383
84+ // ListModels gets a Model by its ID.
85+ func (c * Client ) ListModels (ctx context.Context , modelID string ) ([]Model , error ) {
86+ u , err := url .Parse (c .addr + "/suggestionbox/models" )
87+ if err != nil {
88+ return nil , err
89+ }
90+ if ! u .IsAbs () {
91+ return nil , errors .New ("box address must be absolute" )
92+ }
93+ req , err := http .NewRequest (http .MethodGet , u .String (), nil )
94+ if err != nil {
95+ return nil , err
96+ }
97+ req = req .WithContext (ctx )
98+ req .Header .Set ("Accept" , "application/json; charset=utf-8" )
99+ req .Header .Set ("Content-Type" , "application/json; charset=utf-8" )
100+ resp , err := c .HTTPClient .Do (req )
101+ if err != nil {
102+ return nil , err
103+ }
104+ defer resp .Body .Close ()
105+ if resp .StatusCode < 200 || resp .StatusCode >= 300 {
106+ return nil , errors .New (resp .Status )
107+ }
108+ var response struct {
109+ Success bool
110+ Error string
111+ Models []Model
112+ }
113+ if err := json .NewDecoder (resp .Body ).Decode (& response ); err != nil {
114+ return nil , errors .Wrap (err , "decoding response" )
115+ }
116+ if ! response .Success {
117+ return nil , ErrSuggestionbox (response .Error )
118+ }
119+ return response .Models , nil
120+ }
121+
84122// GetModel gets a Model by its ID.
85123func (c * Client ) GetModel (ctx context.Context , modelID string ) (Model , error ) {
86124 var model Model
0 commit comments