@@ -3,8 +3,6 @@ package clarifai
3
3
import (
4
4
"encoding/json"
5
5
"errors"
6
- "net/url"
7
- "strings"
8
6
)
9
7
10
8
// InfoResp represents the expected JSON response from /info/
@@ -27,6 +25,13 @@ type InfoResp struct {
27
25
}
28
26
}
29
27
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
+
30
35
// TagResp represents the expected JSON response from /tag/
31
36
type TagResp struct {
32
37
StatusCode string `json:"status_code"`
@@ -60,13 +65,13 @@ type TagResult struct {
60
65
61
66
// FeedbackForm is used to send feedback back to Clarifai
62
67
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"`
70
75
}
71
76
72
77
// FeedbackResp is the expected response from /feedback/
@@ -90,22 +95,12 @@ func (client *Client) Info() (*InfoResp, error) {
90
95
}
91
96
92
97
// 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 {
95
100
return nil , errors .New ("Requires at least one url" )
96
101
}
97
102
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 )
109
104
110
105
if err != nil {
111
106
return nil , err
@@ -118,43 +113,15 @@ func (client *Client) Tag(urls, localIDs []string) (*TagResp, error) {
118
113
}
119
114
120
115
// 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 {
123
118
return nil , errors .New ("Requires at least one docid or url" )
124
119
}
125
120
126
- if params .DocIDs != nil && params .URLs != nil {
121
+ if form .DocIDs != nil && form .URLs != nil {
127
122
return nil , errors .New ("Request must provide exactly one of the following fields: {'DocIDs', 'URLs'}" )
128
123
}
129
124
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
-
158
125
res , err := client .commonHTTPRequest (form , "feedback" , "POST" , false )
159
126
160
127
feedbackres := new (FeedbackResp )
0 commit comments