Skip to content

Commit 34e1c18

Browse files
author
solongordon
committed
Merge pull request #4 from Clarifai/solon-json-requests
Use JSON for tag and feedback requests.
2 parents d20504a + dd00163 commit 34e1c18

File tree

5 files changed

+41
-62
lines changed

5 files changed

+41
-62
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ _test
1313
*.[568vq]
1414
[568vq].out
1515

16+
# IntelliJ
17+
*.iml
18+
.idea
19+
1620
*.cgo1.go
1721
*.cgo2.c
1822
_cgo_defun.c
@@ -24,5 +28,6 @@ _testmain.go
2428
*.exe
2529
*.test
2630
*.prof
31+
*.sw[nop]
2732

2833
mct.go

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func main() {
2929
// Let's get some context about these images
3030
urls := []string{"http://www.clarifai.com/img/metro-north.jpg", "http://www.clarifai.com/img/metro-north.jpg"}
3131
// Give it to Clarifai to run their magic
32-
tag_data, err := client.Tag(urls, nil)
32+
tag_data, err := client.Tag(clarifai.TagRequest{URLs: urls})
3333

3434
if err != nil {
3535
fmt.Println(err)

client.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package clarifai
33

44
import (
5+
"bytes"
56
"encoding/json"
67
"errors"
78
"io/ioutil"
@@ -82,20 +83,26 @@ func (client *Client) requestAccessToken() error {
8283
return nil
8384
}
8485

85-
func (client *Client) commonHTTPRequest(values url.Values, endpoint, verb string, retry bool) ([]byte, error) {
86-
if values == nil {
87-
values = url.Values{}
86+
func (client *Client) commonHTTPRequest(jsonBody interface{}, endpoint, verb string, retry bool) ([]byte, error) {
87+
if jsonBody == nil {
88+
jsonBody = struct{}{}
8889
}
8990

90-
req, err := http.NewRequest(verb, client.buildURL(endpoint), strings.NewReader(values.Encode()))
91+
body, err := json.Marshal(jsonBody)
9192

9293
if err != nil {
9394
return nil, err
9495
}
9596

96-
req.Header.Set("Content-Length", strconv.Itoa(len(values.Encode())))
97+
req, err := http.NewRequest(verb, client.buildURL(endpoint), bytes.NewReader(body))
98+
99+
if err != nil {
100+
return nil, err
101+
}
102+
103+
req.Header.Set("Content-Length", strconv.Itoa(len(body)))
97104
req.Header.Set("Authorization", "Bearer "+client.AccessToken)
98-
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
105+
req.Header.Set("Content-Type", "application/json")
99106

100107
httpClient := &http.Client{}
101108
res, err := httpClient.Do(req)
@@ -118,7 +125,7 @@ func (client *Client) commonHTTPRequest(values url.Values, endpoint, verb string
118125
if err != nil {
119126
return nil, err
120127
}
121-
return client.commonHTTPRequest(values, endpoint, verb, true)
128+
return client.commonHTTPRequest(jsonBody, endpoint, verb, true)
122129
}
123130
return nil, errors.New("TOKEN_INVALID")
124131
case 429:

requests.go

+20-53
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package clarifai
33
import (
44
"encoding/json"
55
"errors"
6-
"net/url"
7-
"strings"
86
)
97

108
// InfoResp represents the expected JSON response from /info/
@@ -27,6 +25,13 @@ type InfoResp struct {
2725
}
2826
}
2927

28+
// TagRequest represents a JSON request for /tag/
29+
type TagRequest struct {
30+
URLs []string `json:"url"`
31+
LocalIDs []string `json:"local_ids,omitempty"`
32+
Model string `json:"model,omitempty"`
33+
}
34+
3035
// TagResp represents the expected JSON response from /tag/
3136
type TagResp struct {
3237
StatusCode string `json:"status_code"`
@@ -60,13 +65,13 @@ type TagResult struct {
6065

6166
// FeedbackForm is used to send feedback back to Clarifai
6267
type FeedbackForm struct {
63-
DocIDs []string
64-
URLs []string
65-
AddTags []string
66-
RemoveTags []string
67-
DissimilarDocIDs []string
68-
SimilarDocIDs []string
69-
SearchClick []string
68+
DocIDs []string `json:"docids,omitempty"`
69+
URLs []string `json:"url,omitempty"`
70+
AddTags []string `json:"add_tags,omitempty"`
71+
RemoveTags []string `json:"remove_tags,omitempty"`
72+
DissimilarDocIDs []string `json:"dissimilar_docids,omitempty"`
73+
SimilarDocIDs []string `json:"similar_docids,omitempty"`
74+
SearchClick []string `json:"search_click,omitempty"`
7075
}
7176

7277
// FeedbackResp is the expected response from /feedback/
@@ -90,22 +95,12 @@ func (client *Client) Info() (*InfoResp, error) {
9095
}
9196

9297
// Tag allows the client to request tag data on a single, or multiple photos
93-
func (client *Client) Tag(urls, localIDs []string) (*TagResp, error) {
94-
if urls == nil {
98+
func (client *Client) Tag(req TagRequest) (*TagResp, error) {
99+
if len(req.URLs) < 1 {
95100
return nil, errors.New("Requires at least one url")
96101
}
97102

98-
form := url.Values{}
99-
for _, url := range urls {
100-
form.Add("url", url)
101-
}
102-
if localIDs != nil {
103-
for _, localID := range localIDs {
104-
form.Add("local_id", localID)
105-
}
106-
}
107-
108-
res, err := client.commonHTTPRequest(form, "tag", "POST", false)
103+
res, err := client.commonHTTPRequest(req, "tag", "POST", false)
109104

110105
if err != nil {
111106
return nil, err
@@ -118,43 +113,15 @@ func (client *Client) Tag(urls, localIDs []string) (*TagResp, error) {
118113
}
119114

120115
// Feedback allows the user to provide contextual feedback to Clarifai in order to improve their results
121-
func (client *Client) Feedback(params FeedbackForm) (*FeedbackResp, error) {
122-
if params.DocIDs == nil && params.URLs == nil {
116+
func (client *Client) Feedback(form FeedbackForm) (*FeedbackResp, error) {
117+
if form.DocIDs == nil && form.URLs == nil {
123118
return nil, errors.New("Requires at least one docid or url")
124119
}
125120

126-
if params.DocIDs != nil && params.URLs != nil {
121+
if form.DocIDs != nil && form.URLs != nil {
127122
return nil, errors.New("Request must provide exactly one of the following fields: {'DocIDs', 'URLs'}")
128123
}
129124

130-
form := url.Values{}
131-
132-
if params.DocIDs != nil {
133-
form.Add("docids", strings.Join(params.DocIDs, ","))
134-
} else {
135-
form.Add("url", strings.Join(params.URLs, ","))
136-
}
137-
138-
if params.AddTags != nil {
139-
form.Add("add_tags", strings.Join(params.AddTags, ","))
140-
}
141-
142-
if params.RemoveTags != nil {
143-
form.Add("remove_tags", strings.Join(params.RemoveTags, ","))
144-
}
145-
146-
if params.DissimilarDocIDs != nil {
147-
form.Add("dissimilar_docids", strings.Join(params.DissimilarDocIDs, ","))
148-
}
149-
150-
if params.SimilarDocIDs != nil {
151-
form.Add("similar_docids", strings.Join(params.SimilarDocIDs, ","))
152-
}
153-
154-
if params.SearchClick != nil {
155-
form.Add("search_click", strings.Join(params.SearchClick, ","))
156-
}
157-
158125
res, err := client.commonHTTPRequest(form, "feedback", "POST", false)
159126

160127
feedbackres := new(FeedbackResp)

requests_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestTagMultiple(t *testing.T) {
5555
})
5656

5757
urls := []string{"http://www.clarifai.com/img/metro-north.jpg", "http://www.clarifai.com/img/metro-north.jpg"}
58-
_, err := client.Tag(urls, nil)
58+
_, err := client.Tag(TagRequest{URLs: urls})
5959

6060
if err != nil {
6161
t.Errorf("Tag() should not return error with valid request: %q\n", err)

0 commit comments

Comments
 (0)