Skip to content

Commit

Permalink
Session id changes to provide unique user identifier on each request
Browse files Browse the repository at this point in the history
  • Loading branch information
marcossegovia committed May 12, 2017
2 parents 0e37fb0 + c47f5f4 commit a4773d3
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 53 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ func main() {
client, err := apiai.NewClient(
&apiai.ClientConfig{
Token: "YOUR-API-AI-TOKEN",
SessionId: "YOUR_USER_SESSION_ID",
QueryLang: "en", //Default en
SpeechLang: "en-US", //Default en-US
},
)
if err != nil {
fmt.Printf("%v", err)
}

qr, err := client.Query(apiai.Query{Query: []string{"My name is Marcos and I live in Barcelona"}})
//Set the query string and your current user identifier.
qr, err := client.Query(apiai.Query{Query: []string{"My name is Marcos and I live in Barcelona"}, SessionId: "123454321"})
if err != nil {
fmt.Printf("%v", err)
}
Expand Down
7 changes: 0 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var speechLang = []string{"en-US", "en-AU", "en-CA", "en-GB", "en-IN", "ru-RU",

type ClientConfig struct {
Token string //a9a9a9a9a9a9aa9a9a9a9a9a9a9a9a9a
SessionId string
Version string //YYYYMMDD
QueryLang string
SpeechLang string
Expand Down Expand Up @@ -53,12 +52,6 @@ func NewClient(conf *ClientConfig) (*ApiClient, error) {
if conf.Token == "" {
return nil, fmt.Errorf("%v", "You have to provide a Token")
}
if conf.SessionId == "" {
return nil, fmt.Errorf("%v", "You have to provide a session id")
}
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
}
Expand Down
20 changes: 10 additions & 10 deletions contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type Context struct {
Params map[string]string `json:"parameters"`
}

func (c *ApiClient) GetContexts() ([]Context, error) {
func (c *ApiClient) GetContexts(sessionId string) ([]Context, error) {
req, err := http.NewRequest("GET", c.buildUrl("contexts", map[string]string{
"SessionId": c.config.SessionId,
"sessionId": sessionId,
}), nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -46,9 +46,9 @@ func (c *ApiClient) GetContexts() ([]Context, error) {
}
}

func (c *ApiClient) GetContext(name string) (*Context, error) {
func (c *ApiClient) GetContext(name, sessionId string) (*Context, error) {
req, err := http.NewRequest("GET", c.buildUrl("contexts/"+url.QueryEscape(name), map[string]string{
"SessionId": c.config.SessionId,
"sessionId": sessionId,
}), nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -78,15 +78,15 @@ func (c *ApiClient) GetContext(name string) (*Context, error) {
}
}

func (c *ApiClient) CreateContext(context Context) error {
func (c *ApiClient) CreateContext(context Context, sessionId string) 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,
"sessionId": sessionId,
}), body)
if err != nil {
return err
Expand All @@ -110,9 +110,9 @@ func (c *ApiClient) CreateContext(context Context) error {
}
}

func (c *ApiClient) DeleteContexts() error {
func (c *ApiClient) DeleteContexts(sessionId string) error {
req, err := http.NewRequest("DELETE", c.buildUrl("contexts", map[string]string{
"SessionId": c.config.SessionId,
"sessionId": sessionId,
}), nil)
if err != nil {
return err
Expand All @@ -136,9 +136,9 @@ func (c *ApiClient) DeleteContexts() error {
}
}

func (c *ApiClient) DeleteContext(name string) error {
func (c *ApiClient) DeleteContext(name, sessionId string) error {
req, err := http.NewRequest("DELETE", c.buildUrl("contexts/"+url.QueryEscape(name), map[string]string{
"SessionId": c.config.SessionId,
"sessionId": sessionId,
}), nil)
if err != nil {
return err
Expand Down
30 changes: 15 additions & 15 deletions contexts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestGetContexts(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -68,10 +68,10 @@ func TestGetContexts(t *testing.T) {

for _, tc := range tests {
httpmock.RegisterResponder("GET", c.buildUrl("contexts", map[string]string{
"SessionId": c.config.SessionId,
"sessionId": "123454321",
}), tc.responder)

r, err := c.GetContexts()
r, err := c.GetContexts("123454321")

assert.Equal(r, tc.expectedResponse, tc.description)
assert.Equal(err, tc.expectedError, tc.description)
Expand All @@ -81,7 +81,7 @@ func TestGetContexts(t *testing.T) {
}

func TestGetContext(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -126,10 +126,10 @@ func TestGetContext(t *testing.T) {

for _, tc := range tests {
httpmock.RegisterResponder("GET", c.buildUrl("contexts/"+url.QueryEscape("Coffee time"), map[string]string{
"SessionId": c.config.SessionId,
"sessionId": "123454321",
}), tc.responder)

r, err := c.GetContext("Coffee time")
r, err := c.GetContext("Coffee time", "123454321")

assert.Equal(r, tc.expectedResponse, tc.description)
assert.Equal(err, tc.expectedError, tc.description)
Expand All @@ -139,7 +139,7 @@ func TestGetContext(t *testing.T) {
}

func TestCreateContext(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand All @@ -166,7 +166,7 @@ func TestCreateContext(t *testing.T) {

for _, tc := range tests {
httpmock.RegisterResponder("POST", c.buildUrl("contexts", map[string]string{
"SessionId": c.config.SessionId,
"sessionId": "123454321",
}), tc.responder)

err := c.CreateContext(Context{
Expand All @@ -177,7 +177,7 @@ func TestCreateContext(t *testing.T) {
"temperature-1": "hot",
"temperature-2": "cold",
},
})
}, "123454321")

assert.Equal(err, tc.expectedError, tc.description)

Expand All @@ -186,7 +186,7 @@ func TestCreateContext(t *testing.T) {
}

func TestDeleteContexts(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand All @@ -212,10 +212,10 @@ func TestDeleteContexts(t *testing.T) {

for _, tc := range tests {
httpmock.RegisterResponder("DELETE", c.buildUrl("contexts", map[string]string{
"SessionId": c.config.SessionId,
"sessionId": "123454321",
}), tc.responder)

err := c.DeleteContexts()
err := c.DeleteContexts("123454321")

assert.Equal(err, tc.expectedError, tc.description)

Expand All @@ -224,7 +224,7 @@ func TestDeleteContexts(t *testing.T) {
}

func TestDeleteContext(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand All @@ -250,10 +250,10 @@ func TestDeleteContext(t *testing.T) {

for _, tc := range tests {
httpmock.RegisterResponder("DELETE", c.buildUrl("contexts/"+url.QueryEscape("Coffee time"), map[string]string{
"SessionId": c.config.SessionId,
"sessionId": "123454321",
}), tc.responder)

err := c.DeleteContext("Coffee time")
err := c.DeleteContext("Coffee time", "123454321")

assert.Equal(err, tc.expectedError, tc.description)

Expand Down
18 changes: 9 additions & 9 deletions entities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestGetEntities(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestGetEntities(t *testing.T) {
}

func TestGetEntity(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestGetEntity(t *testing.T) {
}

func TestCreateEntity(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -214,7 +214,7 @@ func TestCreateEntity(t *testing.T) {
}

func TestAddEntries(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func TestAddEntries(t *testing.T) {
}

func TestUpdateEntities(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -317,7 +317,7 @@ func TestUpdateEntities(t *testing.T) {
}

func TestUpdateEntity(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -370,7 +370,7 @@ func TestUpdateEntity(t *testing.T) {
}

func TestUpdateEntries(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -420,7 +420,7 @@ func TestUpdateEntries(t *testing.T) {
}

func TestDeleteEntity(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -456,7 +456,7 @@ func TestDeleteEntity(t *testing.T) {
}

func TestDeleteEntries(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down
10 changes: 5 additions & 5 deletions intents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestGetIntents(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestGetIntents(t *testing.T) {
}

func TestGetIntent(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -390,7 +390,7 @@ func TestGetIntent(t *testing.T) {
}

func TestCreateIntent(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -521,7 +521,7 @@ func TestCreateIntent(t *testing.T) {
}

func TestUpdateIntent(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -640,7 +640,7 @@ func TestUpdateIntent(t *testing.T) {
}

func TestDeleteIntent(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down
1 change: 0 additions & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ type QueryResponse struct {

func (c *ApiClient) Query(q Query) (*QueryResponse, error) {
q.Version = c.config.Version
q.SessionId = c.config.SessionId
q.Language = c.config.QueryLang
body := new(bytes.Buffer)
err := json.NewEncoder(body).Encode(q)
Expand Down
6 changes: 3 additions & 3 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestQuery(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken", SessionId: "123454321"})
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestQuery(t *testing.T) {
"code": 200,
"errorType": "success"
},
"SessionId": "123454321"
"sessionId": "123454321"
}`),
expectedResponse: &QueryResponse{
Id: "b340a1f7-abee-4e13-9bdd-5e8938a48b7d",
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestQuery(t *testing.T) {
for _, tc := range tests {
httpmock.RegisterResponder("POST", c.buildUrl("query", nil), tc.responder)

r, err := c.Query(Query{Query: []string{"my name is Marcos and I live in Barcelona"}})
r, err := c.Query(Query{Query: []string{"my name is Marcos and I live in Barcelona"}, SessionId: "123454321"})

assert.Equal(r, tc.expectedResponse, tc.description)
assert.Equal(err, tc.expectedError, tc.description)
Expand Down

0 comments on commit a4773d3

Please sign in to comment.