github.com/wit-ai/wit-go/v2@v2.0.2/apps.go (about)

     1  // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
     2  
     3  package witai
     4  
     5  import (
     6  	"bytes"
     7  	"encoding/json"
     8  	"fmt"
     9  	"net/http"
    10  	"net/url"
    11  	"strings"
    12  	"time"
    13  )
    14  
    15  // AppTrainingStatus - Represents the status of an app
    16  type AppTrainingStatus string
    17  
    18  const (
    19  	// Done status
    20  	Done AppTrainingStatus = "done"
    21  	// Scheduled status
    22  	Scheduled AppTrainingStatus = "scheduled"
    23  	// Ongoing status
    24  	Ongoing AppTrainingStatus = "ongoing"
    25  )
    26  
    27  // App - https://wit.ai/docs/http/20200513/#get__apps_link
    28  type App struct {
    29  	ID      string `json:"id,omitempty"`
    30  	Name    string `json:"name"`
    31  	Lang    string `json:"lang"`
    32  	Private bool   `json:"private"`
    33  
    34  	// Timezone is only used when creating/updating an app; it's
    35  	// not available when getting the details of an app.
    36  	Timezone string `json:"timezone,omitempty"`
    37  
    38  	CreatedAt Time `json:"created_at,omitempty"`
    39  
    40  	WillTrainAt              Time              `json:"will_train_at,omitempty"`
    41  	LastTrainedAt            Time              `json:"last_trained_at,omitempty"`
    42  	LastTrainingDurationSecs int               `json:"last_training_duration_secs,omitempty"`
    43  	TrainingStatus           AppTrainingStatus `json:"training_status,omitempty"`
    44  }
    45  
    46  // Time - Custom type to encapsulated a time.Time
    47  type Time struct {
    48  	time.Time
    49  }
    50  
    51  // UnmarshalJSON - Our unmarshal function for our custom type
    52  func (witTime *Time) UnmarshalJSON(input []byte) error {
    53  	strInput := string(input)
    54  	strInput = strings.Trim(strInput, `"`)
    55  	newTime, err := time.Parse(WitTimeFormat, strInput)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	witTime.Time = newTime
    60  	return nil
    61  }
    62  
    63  // CreatedApp - https://wit.ai/docs/http/20200513/#post__apps_link
    64  type CreatedApp struct {
    65  	AccessToken string `json:"access_token"`
    66  	AppID       string `json:"app_id"`
    67  }
    68  
    69  // GetApps - Returns an array of all apps that you own.
    70  //
    71  // https://wit.ai/docs/http/20200513/#get__apps_link
    72  func (c *Client) GetApps(limit int, offset int) ([]App, error) {
    73  	if limit <= 0 {
    74  		limit = 0
    75  	}
    76  	if offset <= 0 {
    77  		offset = 0
    78  	}
    79  
    80  	resp, err := c.request(http.MethodGet, fmt.Sprintf("/apps?limit=%d&offset=%d", limit, offset), "application/json", nil)
    81  	if err != nil {
    82  		return []App{}, err
    83  	}
    84  
    85  	defer resp.Close()
    86  
    87  	var apps []App
    88  	decoder := json.NewDecoder(resp)
    89  	err = decoder.Decode(&apps)
    90  	return apps, err
    91  }
    92  
    93  // GetApp - Returns an object representation of the specified app.
    94  //
    95  // https://wit.ai/docs/http/20200513/#get__apps__app_link
    96  func (c *Client) GetApp(id string) (*App, error) {
    97  	resp, err := c.request(http.MethodGet, fmt.Sprintf("/apps/%s", url.PathEscape(id)), "application/json", nil)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	defer resp.Close()
   103  
   104  	var app *App
   105  	decoder := json.NewDecoder(resp)
   106  	if err = decoder.Decode(&app); err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	return app, nil
   111  }
   112  
   113  // CreateApp - creates new app.
   114  //
   115  // https://wit.ai/docs/http/20200513/#post__apps_link
   116  func (c *Client) CreateApp(app App) (*CreatedApp, error) {
   117  	appJSON, err := json.Marshal(app)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	resp, err := c.request(http.MethodPost, "/apps", "application/json", bytes.NewBuffer(appJSON))
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	defer resp.Close()
   128  
   129  	var createdApp *CreatedApp
   130  	decoder := json.NewDecoder(resp)
   131  	err = decoder.Decode(&createdApp)
   132  
   133  	return createdApp, err
   134  }
   135  
   136  // UpdateApp - Updates an app.
   137  //
   138  // https://wit.ai/docs/http/20200513/#put__apps__app_link
   139  func (c *Client) UpdateApp(id string, app App) error {
   140  	appJSON, err := json.Marshal(app)
   141  	if err != nil {
   142  		return err
   143  	}
   144  
   145  	resp, err := c.request(http.MethodPut, fmt.Sprintf("/apps/%s", url.PathEscape(id)), "application/json", bytes.NewBuffer(appJSON))
   146  	if err == nil {
   147  		resp.Close()
   148  	}
   149  
   150  	return err
   151  }
   152  
   153  // DeleteApp - deletes app by ID.
   154  //
   155  // https://wit.ai/docs/http/20200513/#delete__apps__app_link
   156  func (c *Client) DeleteApp(id string) error {
   157  	resp, err := c.request(http.MethodDelete, fmt.Sprintf("/apps/%s", url.PathEscape(id)), "application/json", nil)
   158  	if err == nil {
   159  		resp.Close()
   160  	}
   161  
   162  	return err
   163  }
   164  
   165  // AppTag - https://wit.ai/docs/http/20200513/#get__apps__app_tags__tag_link
   166  type AppTag struct {
   167  	Name string `json:"name,omitempty"`
   168  	Desc string `json:"desc,omitempty"`
   169  
   170  	CreatedAt Time `json:"created_at,omitempty"`
   171  	UpdatedAt Time `json:"updated_at,omitempty"`
   172  }
   173  
   174  // GetAppTags - Returns an array of all tag groups for an app.
   175  // Within a group, all tags point to the same app state (as a result of moving tags).
   176  //
   177  // https://wit.ai/docs/http/20200513/#get__apps__app_tags_link
   178  func (c *Client) GetAppTags(appID string) ([][]AppTag, error) {
   179  	resp, err := c.request(http.MethodGet, fmt.Sprintf("/apps/%s/tags", url.PathEscape(appID)), "application/json", nil)
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  
   184  	defer resp.Close()
   185  
   186  	var tags [][]AppTag
   187  	decoder := json.NewDecoder(resp)
   188  	err = decoder.Decode(&tags)
   189  	return tags, err
   190  }
   191  
   192  // GetAppTag - returns the detail of the specified tag.
   193  //
   194  // https://wit.ai/docs/http/20200513/#get__apps__app_tags__tag_link
   195  func (c *Client) GetAppTag(appID, tagID string) (*AppTag, error) {
   196  	resp, err := c.request(http.MethodGet, fmt.Sprintf("/apps/%s/tags/%s", url.PathEscape(appID), url.PathEscape(tagID)), "application/json", nil)
   197  	if err != nil {
   198  		return nil, err
   199  	}
   200  
   201  	defer resp.Close()
   202  
   203  	var tag *AppTag
   204  	decoder := json.NewDecoder(resp)
   205  	err = decoder.Decode(&tag)
   206  	return tag, err
   207  }
   208  
   209  // CreateAppTag - Take a snapshot of the current app state, save it as a tag (version)
   210  // of the app. The name of the tag created will be returned in the response.
   211  //
   212  // https://wit.ai/docs/http/20200513/#post__apps__app_tags_link
   213  func (c *Client) CreateAppTag(appID string, tag string) (*AppTag, error) {
   214  	type appTag struct {
   215  		Tag string `json:"tag"`
   216  	}
   217  
   218  	tagJSON, err := json.Marshal(appTag{Tag: tag})
   219  	if err != nil {
   220  		return nil, err
   221  	}
   222  
   223  	resp, err := c.request(http.MethodPost, fmt.Sprintf("/apps/%s/tags", url.PathEscape(tag)), "application/json", bytes.NewBuffer(tagJSON))
   224  	if err != nil {
   225  		return nil, err
   226  	}
   227  
   228  	defer resp.Close()
   229  
   230  	// theresponse format is different than the one in get API.
   231  	var tmp appTag
   232  	decoder := json.NewDecoder(resp)
   233  	if err := decoder.Decode(&tmp); err != nil {
   234  		return nil, err
   235  	}
   236  
   237  	return &AppTag{Name: tmp.Tag}, nil
   238  }
   239  
   240  // UpdateAppTagRequest - https://wit.ai/docs/http/20200513/#put__apps__app_tags__tag_link
   241  type UpdateAppTagRequest struct {
   242  	Tag    string `json:"tag,omitempty"`
   243  	Desc   string `json:"desc,omitempty"`
   244  	MoveTo string `json:"move_to,omitempty"`
   245  }
   246  
   247  // UpdateAppTagResponse - https://wit.ai/docs/http/20200513/#put__apps__app_tags__tag_link
   248  type UpdateAppTagResponse struct {
   249  	Tag     string `json:"tag,omitempty"`
   250  	Desc    string `json:"desc,omitempty"`
   251  	MovedTo string `json:"moved_to,omitempty"`
   252  }
   253  
   254  // UpdateAppTag - Update the tag's name or description
   255  //
   256  // https://wit.ai/docs/http/20200513/#put__apps__app_tags__tag_link
   257  func (c *Client) UpdateAppTag(appID, tagID string, updated AppTag) (*AppTag, error) {
   258  	type tag struct {
   259  		Tag  string `json:"tag,omitempty"`
   260  		Desc string `json:"desc,omitempty"`
   261  	}
   262  
   263  	updateJSON, err := json.Marshal(tag{Tag: updated.Name, Desc: updated.Desc})
   264  	if err != nil {
   265  		return nil, err
   266  	}
   267  
   268  	resp, err := c.request(http.MethodPut, fmt.Sprintf("/apps/%s/tags/%s", url.PathEscape(appID), url.PathEscape(tagID)), "application/json", bytes.NewBuffer(updateJSON))
   269  	if err != nil {
   270  		return nil, err
   271  	}
   272  
   273  	defer resp.Close()
   274  
   275  	var tagResp tag
   276  	decoder := json.NewDecoder(resp)
   277  	err = decoder.Decode(&tagResp)
   278  	return &AppTag{Name: tagResp.Tag, Desc: tagResp.Desc}, err
   279  }
   280  
   281  type MovedAppTag struct {
   282  	Tag     string `json:"tag"`
   283  	Desc    string `json:"desc"`
   284  	MovedTo string `json:"moved_to"`
   285  }
   286  
   287  // MoveAppTag - move the tag to point to another tag.
   288  //
   289  // https://wit.ai/docs/http/20200513/#put__apps__app_tags__tag_link
   290  func (c *Client) MoveAppTag(appID, tagID string, to string, updated *AppTag) (*MovedAppTag, error) {
   291  	type tag struct {
   292  		Tag    string `json:"tag,omitempty"`
   293  		Desc   string `json:"desc,omitempty"`
   294  		MoveTo string `json:"move_to,omitempty"`
   295  	}
   296  
   297  	updateReq := tag{MoveTo: to}
   298  	if updated != nil {
   299  		updateReq.Tag = updated.Name
   300  		updateReq.Desc = updated.Desc
   301  	}
   302  
   303  	updateJSON, err := json.Marshal(updateReq)
   304  	if err != nil {
   305  		return nil, err
   306  	}
   307  
   308  	resp, err := c.request(http.MethodPut, fmt.Sprintf("/apps/%s/tags/%s", url.PathEscape(appID), url.PathEscape(tagID)), "application/json", bytes.NewBuffer(updateJSON))
   309  	if err != nil {
   310  		return nil, err
   311  	}
   312  
   313  	defer resp.Close()
   314  
   315  	var tagResp *MovedAppTag
   316  	decoder := json.NewDecoder(resp)
   317  	err = decoder.Decode(&tagResp)
   318  	return tagResp, err
   319  }
   320  
   321  // DeleteAppTag - Permanently delete the tag.
   322  //
   323  // https://wit.ai/docs/http/20200513/#delete__apps__app_tags__tag_link
   324  func (c *Client) DeleteAppTag(appID, tagID string) error {
   325  	resp, err := c.request(http.MethodDelete, fmt.Sprintf("/apps/%s/tags/%s", url.PathEscape(appID), url.PathEscape(tagID)), "application/json", nil)
   326  	if err == nil {
   327  		resp.Close()
   328  	}
   329  
   330  	return err
   331  }