77 "fmt"
88 "net/http"
99 "net/url"
10+ "path"
1011 "strings"
11- "time"
1212
1313 "github.com/pkg/errors"
1414)
@@ -66,8 +66,8 @@ func NewChoice(id string, features ...Feature) Choice {
6666
6767// ModelOptions describes the behaviours of a Model.
6868type ModelOptions struct {
69- // Expiration is the time to wait for the reward before it expires.
70- Expiration time. Duration `json:"expiration ,omitempty"`
69+ // ExpirationSeconds is the number of seconds to wait for the reward before it expires.
70+ ExpirationSeconds int `json:"expiration_seconds ,omitempty"`
7171
7272 // Epsilon enables proportionate exploiting vs exploring ratio.
7373 Epsilon float64 `json:"epsilon,omitempty"`
@@ -81,6 +81,83 @@ type ModelOptions struct {
8181 Skipgrams int `json:"skipgrams,omitempty"`
8282}
8383
84+ // GetModel gets a Model by its ID.
85+ func (c * Client ) GetModel (ctx context.Context , modelID string ) (Model , error ) {
86+ var model Model
87+ u , err := url .Parse (c .addr + "/" + path .Join ("suggestionbox" , "models" , modelID ))
88+ if err != nil {
89+ return model , err
90+ }
91+ if ! u .IsAbs () {
92+ return model , errors .New ("box address must be absolute" )
93+ }
94+ req , err := http .NewRequest (http .MethodGet , u .String (), nil )
95+ if err != nil {
96+ return model , err
97+ }
98+ req = req .WithContext (ctx )
99+ req .Header .Set ("Accept" , "application/json; charset=utf-8" )
100+ req .Header .Set ("Content-Type" , "application/json; charset=utf-8" )
101+ resp , err := c .HTTPClient .Do (req )
102+ if err != nil {
103+ return model , err
104+ }
105+ defer resp .Body .Close ()
106+ if resp .StatusCode < 200 || resp .StatusCode >= 300 {
107+ return model , errors .New (resp .Status )
108+ }
109+ var response struct {
110+ Success bool
111+ Error string
112+ Model
113+ }
114+ if err := json .NewDecoder (resp .Body ).Decode (& response ); err != nil {
115+ return model , errors .Wrap (err , "decoding response" )
116+ }
117+ if ! response .Success {
118+ return model , ErrSuggestionbox (response .Error )
119+ }
120+ return response .Model , nil
121+ }
122+
123+ // DeleteModel gets a Model by its ID.
124+ func (c * Client ) DeleteModel (ctx context.Context , modelID string ) error {
125+ u , err := url .Parse (c .addr + "/" + path .Join ("suggestionbox" , "models" , modelID ))
126+ if err != nil {
127+ return err
128+ }
129+ if ! u .IsAbs () {
130+ return errors .New ("box address must be absolute" )
131+ }
132+ req , err := http .NewRequest (http .MethodDelete , u .String (), nil )
133+ if err != nil {
134+ return err
135+ }
136+ req = req .WithContext (ctx )
137+ req .Header .Set ("Accept" , "application/json; charset=utf-8" )
138+ req .Header .Set ("Content-Type" , "application/json; charset=utf-8" )
139+ resp , err := c .HTTPClient .Do (req )
140+ if err != nil {
141+ return err
142+ }
143+ defer resp .Body .Close ()
144+ if resp .StatusCode < 200 || resp .StatusCode >= 300 {
145+ return errors .New (resp .Status )
146+ }
147+ var response struct {
148+ Success bool
149+ Error string
150+ Model
151+ }
152+ if err := json .NewDecoder (resp .Body ).Decode (& response ); err != nil {
153+ return errors .Wrap (err , "decoding response" )
154+ }
155+ if ! response .Success {
156+ return ErrSuggestionbox (response .Error )
157+ }
158+ return nil
159+ }
160+
84161// CreateModel creates the Model in Suggestionbox.
85162// If no ID is set, one will be assigned in the return Model.
86163func (c * Client ) CreateModel (ctx context.Context , model Model ) (Model , error ) {
0 commit comments