github.com/wit-ai/wit-go/v2@v2.0.2/trait.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  )
    12  
    13  // Trait - represents a wit-ai trait.
    14  //
    15  // https://wit.ai/docs/http/20200513/#post__traits_link
    16  type Trait struct {
    17  	ID     string       `json:"id"`
    18  	Name   string       `json:"name"`
    19  	Values []TraitValue `json:"values"`
    20  }
    21  
    22  // TraitValue - represents the value of a Trait.
    23  //
    24  // https://wit.ai/docs/http/20200513/#get__traits__trait_link
    25  type TraitValue struct {
    26  	ID    string `json:"id"`
    27  	Value string `json:"value"`
    28  }
    29  
    30  // GetTraits - returns a list of traits.
    31  //
    32  // https://wit.ai/docs/http/20200513/#get__traits_link
    33  func (c *Client) GetTraits() ([]Trait, error) {
    34  	resp, err := c.request(http.MethodGet, "/traits", "application/json", nil)
    35  	if err != nil {
    36  		return []Trait{}, err
    37  	}
    38  
    39  	defer resp.Close()
    40  
    41  	var traits []Trait
    42  	decoder := json.NewDecoder(resp)
    43  	err = decoder.Decode(&traits)
    44  	return traits, err
    45  }
    46  
    47  // CreateTrait - creates a new trait with the given values.
    48  //
    49  // https://wit.ai/docs/http/20200513/#post__traits_link
    50  func (c *Client) CreateTrait(name string, values []string) (*Trait, error) {
    51  	type trait struct {
    52  		Name   string   `json:"name"`
    53  		Values []string `json:"values"`
    54  	}
    55  
    56  	traitJSON, err := json.Marshal(trait{Name: name, Values: values})
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	resp, err := c.request(http.MethodPost, "/traits", "application/json", bytes.NewBuffer(traitJSON))
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	defer resp.Close()
    67  
    68  	var traitResp *Trait
    69  	decoder := json.NewDecoder(resp)
    70  	err = decoder.Decode(&traitResp)
    71  	return traitResp, err
    72  }
    73  
    74  // GetTrait - returns all available information about a trait.
    75  //
    76  // https://wit.ai/docs/http/20200513/#get__traits__trait_link
    77  func (c *Client) GetTrait(name string) (*Trait, error) {
    78  	resp, err := c.request(http.MethodGet, fmt.Sprintf("/traits/%s", url.PathEscape(name)), "application/json", nil)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	defer resp.Close()
    83  
    84  	var traitResp *Trait
    85  	decoder := json.NewDecoder(resp)
    86  	err = decoder.Decode(&traitResp)
    87  	return traitResp, err
    88  }
    89  
    90  // DeleteTrait - permanently deletes a trait.
    91  //
    92  // https://wit.ai/docs/http/20200513/#delete__traits__trait_link
    93  func (c *Client) DeleteTrait(name string) error {
    94  	resp, err := c.request(http.MethodDelete, fmt.Sprintf("/traits/%s", url.PathEscape(name)), "application/json", nil)
    95  	if err == nil {
    96  		resp.Close()
    97  	}
    98  
    99  	return err
   100  }
   101  
   102  // AddTraitValue - create a new value for a trait.
   103  //
   104  // https://wit.ai/docs/http/20200513/#post__traits__trait_values_link
   105  func (c *Client) AddTraitValue(traitName string, value string) (*Trait, error) {
   106  	type traitValue struct {
   107  		Value string `json:"value"`
   108  	}
   109  
   110  	valueJSON, err := json.Marshal(traitValue{Value: value})
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	resp, err := c.request(http.MethodPost, fmt.Sprintf("/traits/%s/values", url.PathEscape(traitName)), "application/json", bytes.NewBuffer(valueJSON))
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	defer resp.Close()
   121  
   122  	var traitResp *Trait
   123  	decoder := json.NewDecoder(resp)
   124  	if err = decoder.Decode(&traitResp); err != nil {
   125  		return nil, err
   126  	}
   127  
   128  	return traitResp, nil
   129  }
   130  
   131  // DeleteTraitValue - permanently deletes the trait value.
   132  //
   133  // https://wit.ai/docs/http/20200513/#delete__traits__trait_values__value_link
   134  func (c *Client) DeleteTraitValue(traitName string, value string) error {
   135  	resp, err := c.request(http.MethodDelete, fmt.Sprintf("/traits/%s/values/%s", url.PathEscape(traitName), url.PathEscape(value)), "application/json", nil)
   136  	if err == nil {
   137  		resp.Close()
   138  	}
   139  
   140  	return err
   141  }