Skip to content

Commit

Permalink
Making ConfigClient properties public
Browse files Browse the repository at this point in the history
  • Loading branch information
marcossegovia committed Mar 12, 2017
1 parent 0490bd5 commit ee80b12
Show file tree
Hide file tree
Showing 10 changed files with 710 additions and 710 deletions.
36 changes: 18 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ var queryLang = []string{"pt-BR", "zh-HK", "zh-CN", "zh-TW", "en", "nl", "fr", "
var speechLang = []string{"en-US", "en-AU", "en-CA", "en-GB", "en-IN", "ru-RU", "de-DE", "es-ES", "pt-PT", "pt-BR", "zh-CN", "zh-TW", "zh-HK", "ja-JP", "fr-FR"}

type ClientConfig struct {
token string //a9a9a9a9a9a9aa9a9a9a9a9a9a9a9a9a
sessionId string
version string //YYYYMMDD
queryLang string
speechLang string
Token string //a9a9a9a9a9a9aa9a9a9a9a9a9a9a9a9a
SessionId string
Version string //YYYYMMDD
QueryLang string
SpeechLang string
}

type ApiClient struct {
Expand Down Expand Up @@ -50,28 +50,28 @@ type Client interface {
}

func NewClient(conf *ClientConfig) (*ApiClient, error) {
if conf.token == "" {
return nil, fmt.Errorf("%v", "You have to provide a token")
if conf.Token == "" {
return nil, fmt.Errorf("%v", "You have to provide a Token")
}
if conf.sessionId == "" {
if conf.SessionId == "" {
return nil, fmt.Errorf("%v", "You have to provide a session id")
}
if len(conf.sessionId) > 36 {
if len(conf.SessionId) > 36 {
return nil, fmt.Errorf("%v", "You have to provide a valid session id, no longer than 36 symbols")
}
if conf.version == "" {
conf.version = defaultVersion
if conf.Version == "" {
conf.Version = defaultVersion
}
if conf.queryLang == "" {
conf.queryLang = defaultQueryLang
if conf.QueryLang == "" {
conf.QueryLang = defaultQueryLang
}
if conf.speechLang == "" {
conf.speechLang = defaultSpeechLang
if conf.SpeechLang == "" {
conf.SpeechLang = defaultSpeechLang
}
if !languageAvailable(conf.queryLang, queryLang) {
if !languageAvailable(conf.QueryLang, queryLang) {
return nil, fmt.Errorf("%v", "You have to provide a valid query language, see https://docs.api.ai/docs/languages")
}
if !languageAvailable(conf.speechLang, speechLang) {
if !languageAvailable(conf.SpeechLang, speechLang) {
return nil, fmt.Errorf("%v", "You have to provide a valid speech language, see https://docs.api.ai/docs/tts#headers")
}

Expand All @@ -88,7 +88,7 @@ func languageAvailable(inputLang string, languages []string) bool {
}

func (c *ApiClient) buildUrl(endpoint string, params map[string]string) string {
u := baseUrl + endpoint + "?v=" + c.config.version
u := baseUrl + endpoint + "?v=" + c.config.Version
if params != nil {
for i, v := range params {
u += "&" + i + "=" + url.QueryEscape(v)
Expand Down
286 changes: 143 additions & 143 deletions contexts.go
Original file line number Diff line number Diff line change
@@ -1,168 +1,168 @@
package apiai

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
)

type ContextParameter struct {
Name string `json:"name"`
Value string `json:"value"`
Name string `json:"name"`
Value string `json:"value"`
}

type Context struct {
Name string `json:"name"`
Lifespan int `json:"lifespan"`
Params []ContextParameter `json:"parameters"`
Name string `json:"name"`
Lifespan int `json:"lifespan"`
Params []ContextParameter `json:"parameters"`
}

func (c *ApiClient) GetContexts() ([]Context, error) {
req, err := http.NewRequest("GET", c.buildUrl("contexts", map[string]string{
"sessionId": c.config.sessionId,
}), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-type", "application/json, charset=utf-8")
req.Header.Set("Authorization", "Bearer "+c.config.token)

httpClient := http.DefaultClient
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var contexts []Context
switch resp.StatusCode {
case http.StatusOK:
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&contexts)
if err != nil {
return nil, err
}
return contexts, nil
default:
return nil, fmt.Errorf("apiai: wops something happens because status code is %v", resp.StatusCode)
}
req, err := http.NewRequest("GET", c.buildUrl("contexts", map[string]string{
"SessionId": c.config.SessionId,
}), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-type", "application/json, charset=utf-8")
req.Header.Set("Authorization", "Bearer "+c.config.Token)

httpClient := http.DefaultClient
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var contexts []Context
switch resp.StatusCode {
case http.StatusOK:
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&contexts)
if err != nil {
return nil, err
}
return contexts, nil
default:
return nil, fmt.Errorf("apiai: wops something happens because status code is %v", resp.StatusCode)
}
}

func (c *ApiClient) GetContext(name string) (*Context, error) {
req, err := http.NewRequest("GET", c.buildUrl("contexts/"+url.QueryEscape(name), map[string]string{
"sessionId": c.config.sessionId,
}), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-type", "application/json, charset=utf-8")
req.Header.Set("Authorization", "Bearer "+c.config.token)

httpClient := http.DefaultClient
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var context *Context
switch resp.StatusCode {
case http.StatusOK:
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&context)
if err != nil {
return nil, err
}
return context, nil
default:
return nil, fmt.Errorf("apiai: wops something happens because status code is %v", resp.StatusCode)
}
req, err := http.NewRequest("GET", c.buildUrl("contexts/"+url.QueryEscape(name), map[string]string{
"SessionId": c.config.SessionId,
}), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-type", "application/json, charset=utf-8")
req.Header.Set("Authorization", "Bearer "+c.config.Token)

httpClient := http.DefaultClient
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var context *Context
switch resp.StatusCode {
case http.StatusOK:
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&context)
if err != nil {
return nil, err
}
return context, nil
default:
return nil, fmt.Errorf("apiai: wops something happens because status code is %v", resp.StatusCode)
}
}

func (c *ApiClient) CreateContext(context Context) error {
body := new(bytes.Buffer)
err := json.NewEncoder(body).Encode(context)
if err != nil {
return err
}

req, err := http.NewRequest("POST", c.buildUrl("contexts", map[string]string{
"sessionId": c.config.sessionId,
}), body)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-type", "application/json, charset=utf-8")
req.Header.Set("Authorization", "Bearer "+c.config.token)

httpClient := http.DefaultClient
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

switch resp.StatusCode {
case http.StatusOK:
return nil
default:
return fmt.Errorf("apiai: wops something happens because status code is %v", resp.StatusCode)
}
body := new(bytes.Buffer)
err := json.NewEncoder(body).Encode(context)
if err != nil {
return err
}

req, err := http.NewRequest("POST", c.buildUrl("contexts", map[string]string{
"SessionId": c.config.SessionId,
}), body)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-type", "application/json, charset=utf-8")
req.Header.Set("Authorization", "Bearer "+c.config.Token)

httpClient := http.DefaultClient
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

switch resp.StatusCode {
case http.StatusOK:
return nil
default:
return fmt.Errorf("apiai: wops something happens because status code is %v", resp.StatusCode)
}
}

func (c *ApiClient) DeleteContexts() error {
req, err := http.NewRequest("DELETE", c.buildUrl("contexts", map[string]string{
"sessionId": c.config.sessionId,
}), nil)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-type", "application/json, charset=utf-8")
req.Header.Set("Authorization", "Bearer "+c.config.token)

httpClient := http.DefaultClient
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

switch resp.StatusCode {
case http.StatusOK:
return nil
default:
return fmt.Errorf("apiai: wops something happens because status code is %v", resp.StatusCode)
}
req, err := http.NewRequest("DELETE", c.buildUrl("contexts", map[string]string{
"SessionId": c.config.SessionId,
}), nil)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-type", "application/json, charset=utf-8")
req.Header.Set("Authorization", "Bearer "+c.config.Token)

httpClient := http.DefaultClient
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

switch resp.StatusCode {
case http.StatusOK:
return nil
default:
return fmt.Errorf("apiai: wops something happens because status code is %v", resp.StatusCode)
}
}

func (c *ApiClient) DeleteContext(name string) error {
req, err := http.NewRequest("DELETE", c.buildUrl("contexts/"+url.QueryEscape(name), map[string]string{
"sessionId": c.config.sessionId,
}), nil)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-type", "application/json, charset=utf-8")
req.Header.Set("Authorization", "Bearer "+c.config.token)

httpClient := http.DefaultClient
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

switch resp.StatusCode {
case http.StatusOK:
return nil
default:
return fmt.Errorf("apiai: wops something happens because status code is %v", resp.StatusCode)
}
req, err := http.NewRequest("DELETE", c.buildUrl("contexts/"+url.QueryEscape(name), map[string]string{
"SessionId": c.config.SessionId,
}), nil)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-type", "application/json, charset=utf-8")
req.Header.Set("Authorization", "Bearer "+c.config.Token)

httpClient := http.DefaultClient
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

switch resp.StatusCode {
case http.StatusOK:
return nil
default:
return fmt.Errorf("apiai: wops something happens because status code is %v", resp.StatusCode)
}
}
Loading

0 comments on commit ee80b12

Please sign in to comment.