Skip to content

Commit

Permalink
New API version: 20240304
Browse files Browse the repository at this point in the history
  • Loading branch information
plutov committed Jul 3, 2024
1 parent 7a88bf3 commit 9b233aa
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 214 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.14
go-version: 1.22

- name: Check out code into the Go module directory
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Run Tests
run: go test -race -v .
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

July 3, 2024

- New default API version: 20240304. Adjusted types to new version
- Switch to latest Go version 1.22
- Fix integration tests

Nov 16, 2020

- Switch to Github Actions for CI/CD
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
![wit.ai](https://s3.amazonaws.com/pliutau.com/wit.png)

[![GoDoc](https://godoc.org/github.com/wit-ai/wit-go?status.svg)](https://godoc.org/github.com/wit-ai/wit-go) [![Go Report Card](https://goreportcard.com/badge/github.com/wit-ai/wit-go)](https://goreportcard.com/report/github.com/wit-ai/wit-go)
[![Go Reference](https://pkg.go.dev/badge/github.com/wit-ai/wit-go)](https://pkg.go.dev/github.com/wit-ai/wit-go)

*This repository is community-maintained. We gladly accept pull requests. Please see the [Wit HTTP Reference](https://wit.ai/docs/http/latest) for all supported endpoints.*

Go client for [wit.ai](https://wit.ai/) HTTP API.

API version: 20240304

## Install

```
Expand Down Expand Up @@ -37,7 +39,7 @@ func main() {

## Testing

Both Unit / Integration tests are executed by Github Actions.
Unit tests are executed by Github Actions, but Integration tests have to be executed manually by providing a valid token via `WITAI_INTEGRATION_TOKEN` env var.

### Unit tests

Expand All @@ -50,8 +52,7 @@ go test -race -v
Integration tests are connecting to real Wit.ai API, so you need to provide a valid token:

```
export WITAI_INTEGRATION_TOKEN=your_secret_token_here
go test -v -tags=integration
WITAI_INTEGRATION_TOKEN={SERVER_ACCESS_TOKEN} go test -v -tags=integration
```

## License
Expand Down
32 changes: 16 additions & 16 deletions apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
Ongoing AppTrainingStatus = "ongoing"
)

// App - https://wit.ai/docs/http/20200513/#get__apps_link
// App - https://wit.ai/docs/http/#get__apps_link
type App struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Expand Down Expand Up @@ -60,15 +60,15 @@ func (witTime *Time) UnmarshalJSON(input []byte) error {
return nil
}

// CreatedApp - https://wit.ai/docs/http/20200513/#post__apps_link
// CreatedApp - https://wit.ai/docs/http/#post__apps_link
type CreatedApp struct {
AccessToken string `json:"access_token"`
AppID string `json:"app_id"`
}

// GetApps - Returns an array of all apps that you own.
//
// https://wit.ai/docs/http/20200513/#get__apps_link
// https://wit.ai/docs/http/#get__apps_link
func (c *Client) GetApps(limit int, offset int) ([]App, error) {
if limit <= 0 {
limit = 0
Expand All @@ -92,7 +92,7 @@ func (c *Client) GetApps(limit int, offset int) ([]App, error) {

// GetApp - Returns an object representation of the specified app.
//
// https://wit.ai/docs/http/20200513/#get__apps__app_link
// https://wit.ai/docs/http/#get__apps__app_link
func (c *Client) GetApp(id string) (*App, error) {
resp, err := c.request(http.MethodGet, fmt.Sprintf("/apps/%s", url.PathEscape(id)), "application/json", nil)
if err != nil {
Expand All @@ -112,7 +112,7 @@ func (c *Client) GetApp(id string) (*App, error) {

// CreateApp - creates new app.
//
// https://wit.ai/docs/http/20200513/#post__apps_link
// https://wit.ai/docs/http/#post__apps_link
func (c *Client) CreateApp(app App) (*CreatedApp, error) {
appJSON, err := json.Marshal(app)
if err != nil {
Expand All @@ -135,7 +135,7 @@ func (c *Client) CreateApp(app App) (*CreatedApp, error) {

// UpdateApp - Updates an app.
//
// https://wit.ai/docs/http/20200513/#put__apps__app_link
// https://wit.ai/docs/http/#put__apps__app_link
func (c *Client) UpdateApp(id string, app App) error {
appJSON, err := json.Marshal(app)
if err != nil {
Expand All @@ -152,7 +152,7 @@ func (c *Client) UpdateApp(id string, app App) error {

// DeleteApp - deletes app by ID.
//
// https://wit.ai/docs/http/20200513/#delete__apps__app_link
// https://wit.ai/docs/http/#delete__apps__app_link
func (c *Client) DeleteApp(id string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/apps/%s", url.PathEscape(id)), "application/json", nil)
if err == nil {
Expand All @@ -162,7 +162,7 @@ func (c *Client) DeleteApp(id string) error {
return err
}

// AppTag - https://wit.ai/docs/http/20200513/#get__apps__app_tags__tag_link
// AppTag - https://wit.ai/docs/http/#get__apps__app_tags__tag_link
type AppTag struct {
Name string `json:"name,omitempty"`
Desc string `json:"desc,omitempty"`
Expand All @@ -174,7 +174,7 @@ type AppTag struct {
// GetAppTags - Returns an array of all tag groups for an app.
// Within a group, all tags point to the same app state (as a result of moving tags).
//
// https://wit.ai/docs/http/20200513/#get__apps__app_tags_link
// https://wit.ai/docs/http/#get__apps__app_tags_link
func (c *Client) GetAppTags(appID string) ([][]AppTag, error) {
resp, err := c.request(http.MethodGet, fmt.Sprintf("/apps/%s/tags", url.PathEscape(appID)), "application/json", nil)
if err != nil {
Expand All @@ -191,7 +191,7 @@ func (c *Client) GetAppTags(appID string) ([][]AppTag, error) {

// GetAppTag - returns the detail of the specified tag.
//
// https://wit.ai/docs/http/20200513/#get__apps__app_tags__tag_link
// https://wit.ai/docs/http/#get__apps__app_tags__tag_link
func (c *Client) GetAppTag(appID, tagID string) (*AppTag, error) {
resp, err := c.request(http.MethodGet, fmt.Sprintf("/apps/%s/tags/%s", url.PathEscape(appID), url.PathEscape(tagID)), "application/json", nil)
if err != nil {
Expand All @@ -209,7 +209,7 @@ func (c *Client) GetAppTag(appID, tagID string) (*AppTag, error) {
// CreateAppTag - Take a snapshot of the current app state, save it as a tag (version)
// of the app. The name of the tag created will be returned in the response.
//
// https://wit.ai/docs/http/20200513/#post__apps__app_tags_link
// https://wit.ai/docs/http/#post__apps__app_tags_link
func (c *Client) CreateAppTag(appID string, tag string) (*AppTag, error) {
type appTag struct {
Tag string `json:"tag"`
Expand Down Expand Up @@ -237,14 +237,14 @@ func (c *Client) CreateAppTag(appID string, tag string) (*AppTag, error) {
return &AppTag{Name: tmp.Tag}, nil
}

// UpdateAppTagRequest - https://wit.ai/docs/http/20200513/#put__apps__app_tags__tag_link
// UpdateAppTagRequest - https://wit.ai/docs/http/#put__apps__app_tags__tag_link
type UpdateAppTagRequest struct {
Tag string `json:"tag,omitempty"`
Desc string `json:"desc,omitempty"`
MoveTo string `json:"move_to,omitempty"`
}

// UpdateAppTagResponse - https://wit.ai/docs/http/20200513/#put__apps__app_tags__tag_link
// UpdateAppTagResponse - https://wit.ai/docs/http/#put__apps__app_tags__tag_link
type UpdateAppTagResponse struct {
Tag string `json:"tag,omitempty"`
Desc string `json:"desc,omitempty"`
Expand All @@ -253,7 +253,7 @@ type UpdateAppTagResponse struct {

// UpdateAppTag - Update the tag's name or description
//
// https://wit.ai/docs/http/20200513/#put__apps__app_tags__tag_link
// https://wit.ai/docs/http/#put__apps__app_tags__tag_link
func (c *Client) UpdateAppTag(appID, tagID string, updated AppTag) (*AppTag, error) {
type tag struct {
Tag string `json:"tag,omitempty"`
Expand Down Expand Up @@ -286,7 +286,7 @@ type MovedAppTag struct {

// MoveAppTag - move the tag to point to another tag.
//
// https://wit.ai/docs/http/20200513/#put__apps__app_tags__tag_link
// https://wit.ai/docs/http/#put__apps__app_tags__tag_link
func (c *Client) MoveAppTag(appID, tagID string, to string, updated *AppTag) (*MovedAppTag, error) {
type tag struct {
Tag string `json:"tag,omitempty"`
Expand Down Expand Up @@ -320,7 +320,7 @@ func (c *Client) MoveAppTag(appID, tagID string, to string, updated *AppTag) (*M

// DeleteAppTag - Permanently delete the tag.
//
// https://wit.ai/docs/http/20200513/#delete__apps__app_tags__tag_link
// https://wit.ai/docs/http/#delete__apps__app_tags__tag_link
func (c *Client) DeleteAppTag(appID, tagID string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/apps/%s/tags/%s", url.PathEscape(appID), url.PathEscape(tagID)), "application/json", nil)
if err == nil {
Expand Down
69 changes: 42 additions & 27 deletions entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// Entity represents a wit-ai Entity.
//
// https://wit.ai/docs/http/20200513/#post__entities_link
// https://wit.ai/docs/http/#post__entities_link
type Entity struct {
ID string `json:"id"`
Name string `json:"name"`
Expand All @@ -21,17 +21,30 @@ type Entity struct {
Keywords []EntityKeyword `json:"keywords,omitempty"`
}

type CreateEntityResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Lookups []string `json:"lookups,omitempty"`
Roles []EntityRole `json:"roles,omitempty"`
Keywords []EntityKeyword `json:"keywords,omitempty"`
}

type EntityRole struct {
ID string `json:"id"`
Name string `json:"name"`
}

// EntityKeyword is a keyword lookup for an Entity.
//
// https://wit.ai/docs/http/20200513/#post__entities__entity_keywords_link
// https://wit.ai/docs/http/#post__entities__entity_keywords_link
type EntityKeyword struct {
Keyword string `json:"keyword"`
Synonyms []string `json:"synonyms"`
}

// GetEntities - returns list of entities.
//
// https://wit.ai/docs/http/20200513/#get__entities_link
// https://wit.ai/docs/http/#get__entities_link
func (c *Client) GetEntities() ([]Entity, error) {
resp, err := c.request(http.MethodGet, "/entities", "application/json", nil)
if err != nil {
Expand All @@ -48,8 +61,8 @@ func (c *Client) GetEntities() ([]Entity, error) {

// CreateEntity - Creates a new entity with the given attributes
//
// https://wit.ai/docs/http/20200513/#post__entities_link
func (c *Client) CreateEntity(entity Entity) (*Entity, error) {
// https://wit.ai/docs/http/#post__entities_link
func (c *Client) CreateEntity(entity Entity) (*CreateEntityResponse, error) {
entityJSON, err := json.Marshal(entity)
if err != nil {
return nil, err
Expand All @@ -62,54 +75,56 @@ func (c *Client) CreateEntity(entity Entity) (*Entity, error) {

defer resp.Close()

var entityResp *Entity
var entityResp *CreateEntityResponse
decoder := json.NewDecoder(resp)
err = decoder.Decode(&entityResp)
return entityResp, err
}

// GetEntity - returns entity by ID or name.
//
// https://wit.ai/docs/http/20200513/#get__entities__entity_link
func (c *Client) GetEntity(entityID string) (*Entity, error) {
// https://wit.ai/docs/http/#get__entities__entity_link
func (c *Client) GetEntity(entityID string) (*CreateEntityResponse, error) {
resp, err := c.request(http.MethodGet, fmt.Sprintf("/entities/%s", url.PathEscape(entityID)), "application/json", nil)
if err != nil {
return nil, err
}

defer resp.Close()

var entity *Entity
var entity *CreateEntityResponse
decoder := json.NewDecoder(resp)
err = decoder.Decode(&entity)
return entity, err
}

// UpdateEntity - Updates an entity by ID or name.
// UpdateEntity - Updates an entity by name.
//
// https://wit.ai/docs/http/20200513/#put__entities__entity_link
func (c *Client) UpdateEntity(entityID string, entity Entity) error {
// https://wit.ai/docs/http/#put__entities__entity_link
func (c *Client) UpdateEntity(name string, entity Entity) (*CreateEntityResponse, error) {
entityJSON, err := json.Marshal(entity)
if err != nil {
return err
return nil, err
}

resp, err := c.request(http.MethodPut, fmt.Sprintf("/entities/%s", url.PathEscape(entityID)), "application/json", bytes.NewBuffer(entityJSON))
resp, err := c.request(http.MethodPut, fmt.Sprintf("/entities/%s", url.PathEscape(name)), "application/json", bytes.NewBuffer(entityJSON))
if err != nil {
return err
return nil, err
}

defer resp.Close()

var entityResp *CreateEntityResponse
decoder := json.NewDecoder(resp)
return decoder.Decode(&entity)
err = decoder.Decode(&entityResp)
return entityResp, err
}

// DeleteEntity - deletes entity by ID or name.
// DeleteEntity - deletes entity by name
//
// https://wit.ai/docs/http/20200513/#delete__entities__entity_link
func (c *Client) DeleteEntity(entityID string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/entities/%s", url.PathEscape(entityID)), "application/json", nil)
// https://wit.ai/docs/http/#delete__entities__entity_link
func (c *Client) DeleteEntity(name string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/entities/%s", url.PathEscape(name)), "application/json", nil)
if err == nil {
resp.Close()
}
Expand All @@ -119,9 +134,9 @@ func (c *Client) DeleteEntity(entityID string) error {

// DeleteEntityRole - deletes entity role.
//
// https://wit.ai/docs/http/20200513/#delete__entities__entity_role_link
func (c *Client) DeleteEntityRole(entityID string, role string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/entities/%s:%s", url.PathEscape(entityID), url.PathEscape(role)), "application/json", nil)
// https://wit.ai/docs/http/#delete__entities__entity_role_link
func (c *Client) DeleteEntityRole(name string, role string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/entities/%s:%s", url.PathEscape(name), url.PathEscape(role)), "application/json", nil)
if err == nil {
resp.Close()
}
Expand All @@ -131,7 +146,7 @@ func (c *Client) DeleteEntityRole(entityID string, role string) error {

// AddEntityKeyword - Add a possible value into the list of values for the keyword entity.
//
// https://wit.ai/docs/http/20200513/#post__entities__entity_keywords_link
// https://wit.ai/docs/http/#post__entities__entity_keywords_link
func (c *Client) AddEntityKeyword(entityID string, keyword EntityKeyword) (*Entity, error) {
valueJSON, err := json.Marshal(keyword)
if err != nil {
Expand All @@ -156,7 +171,7 @@ func (c *Client) AddEntityKeyword(entityID string, keyword EntityKeyword) (*Enti

// DeleteEntityKeyword - Delete a keyword from the keywords entity.
//
// https://wit.ai/docs/http/20200513/#delete__entities__entity_keywords__keyword_link
// https://wit.ai/docs/http/#delete__entities__entity_keywords__keyword_link
func (c *Client) DeleteEntityKeyword(entityID string, keyword string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/entities/%s/keywords/%s", url.PathEscape(entityID), url.PathEscape(keyword)), "application/json", nil)
if err == nil {
Expand All @@ -168,7 +183,7 @@ func (c *Client) DeleteEntityKeyword(entityID string, keyword string) error {

// AddEntityKeywordSynonym - Create a new synonym of the canonical value of the keywords entity.
//
// https://wit.ai/docs/http/20200513/#post__entities__entity_keywords__keyword_synonyms_link
// https://wit.ai/docs/http/#post__entities__entity_keywords__keyword_synonyms_link
func (c *Client) AddEntityKeywordSynonym(entityID string, keyword string, synonym string) (*Entity, error) {
type syn struct {
Synonym string `json:"synonym"`
Expand Down Expand Up @@ -199,7 +214,7 @@ func (c *Client) AddEntityKeywordSynonym(entityID string, keyword string, synony

// DeleteEntityKeywordSynonym - Delete a synonym of the keyword of the entity.
//
// https://wit.ai/docs/http/20200513/#delete__entities__entity_keywords__keyword_synonyms__synonym_link
// https://wit.ai/docs/http/#delete__entities__entity_keywords__keyword_synonyms__synonym_link
func (c *Client) DeleteEntityKeywordSynonym(entityID string, keyword string, expression string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/entities/%s/keywords/%s/synonyms/%s", url.PathEscape(entityID), url.PathEscape(keyword), url.PathEscape(expression)), "application/json", nil)
if err == nil {
Expand Down
Loading

0 comments on commit 9b233aa

Please sign in to comment.