@@ -24,7 +24,7 @@ import (
24
24
)
25
25
26
26
const (
27
- NAME = "kintone- go-SDK "
27
+ NAME = "go-kintone "
28
28
VERSION = "0.3.0"
29
29
DEFAULT_TIMEOUT = time .Second * 600 // Default value for App.Timeout
30
30
)
@@ -124,7 +124,7 @@ type App struct {
124
124
basicAuth bool // true to use Basic Authentication.
125
125
basicAuthUser string // User name for Basic Authentication.
126
126
basicAuthPassword string // Password for Basic Authentication.
127
- userAgentHeader string // User-agent request header string
127
+ extUserAgent string // User-agent request header string
128
128
}
129
129
130
130
// SetBasicAuth enables use of HTTP basic authentication for access
@@ -152,12 +152,71 @@ func (app *App) GetBasicAuthPassword() string {
152
152
153
153
// SetUserAgentHeader set custom user-agent header for http request
154
154
func (app * App ) SetUserAgentHeader (userAgentHeader string ) {
155
- app .userAgentHeader = userAgentHeader
155
+ app .extUserAgent = userAgentHeader
156
156
}
157
157
158
158
// GetUserAgentHeader get user-agent header string
159
159
func (app * App ) GetUserAgentHeader () string {
160
- return app .userAgentHeader
160
+ userAgent := NAME + "/" + VERSION
161
+ if len (app .extUserAgent ) > 0 {
162
+ return userAgent + " " + app .extUserAgent
163
+ }
164
+ return userAgent
165
+ }
166
+
167
+ func (app * App ) createUrl (api string , query string ) url.URL {
168
+ path := fmt .Sprintf ("/k/v1/%s.json" , api )
169
+ if app .GuestSpaceId > 0 {
170
+ path = fmt .Sprintf ("/k/guest/%d/v1/%s.json" , app .GuestSpaceId , api )
171
+ }
172
+
173
+ resultUrl := url.URL {
174
+ Scheme : "https" ,
175
+ Host : app .Domain ,
176
+ Path : path ,
177
+ }
178
+
179
+ if len (query ) > 0 {
180
+ resultUrl .RawQuery = query
181
+ }
182
+ return resultUrl
183
+ }
184
+ func (app * App ) setAuth (request * http.Request ) {
185
+ if app .basicAuth {
186
+ request .SetBasicAuth (app .basicAuthUser , app .basicAuthPassword )
187
+ }
188
+
189
+ if len (app .ApiToken ) > 0 {
190
+ request .Header .Set ("X-Cybozu-API-Token" , app .ApiToken )
191
+ }
192
+
193
+ if len (app .User ) > 0 && len (app .Password ) > 0 {
194
+ request .Header .Set ("X-Cybozu-Authorization" , base64 .StdEncoding .EncodeToString (
195
+ []byte (app .User + ":" + app .Password )))
196
+ }
197
+ }
198
+
199
+ //NewRequest create a request connect to kintone api.
200
+ func (app * App ) NewRequest (method , url string , body io.Reader ) (* http.Request , error ) {
201
+ bodyData := io .Reader (nil )
202
+ if body != nil {
203
+ bodyData = body
204
+ }
205
+
206
+ request , err := http .NewRequest (method , url , bodyData )
207
+ if err != nil {
208
+ return nil , err
209
+ }
210
+
211
+ request .Header .Set ("User-Agent" , app .GetUserAgentHeader ())
212
+
213
+ if method != "GET" {
214
+ request .Header .Set ("Content-Type" , "application/json" )
215
+ }
216
+
217
+ app .setAuth (request )
218
+
219
+ return request , nil
161
220
}
162
221
163
222
func (app * App ) newRequest (method , api string , body io.Reader ) (* http.Request , error ) {
@@ -191,12 +250,8 @@ func (app *App) newRequest(method, api string, body io.Reader) (*http.Request, e
191
250
req .Header .Set ("X-Cybozu-API-Token" , app .ApiToken )
192
251
}
193
252
req .Header .Set ("Content-Type" , "application/json" )
253
+ req .Header .Set ("User-Agent" , app .GetUserAgentHeader ())
194
254
195
- if len (app .GetUserAgentHeader ()) != 0 {
196
- req .Header .Set ("User-Agent" , app .userAgentHeader )
197
- } else {
198
- req .Header .Set ("User-Agent" , NAME + "/" + VERSION )
199
- }
200
255
return req , nil
201
256
}
202
257
@@ -1000,3 +1055,82 @@ func (app *App) Fields() (map[string]*FieldInfo, error) {
1000
1055
}
1001
1056
return ret , nil
1002
1057
}
1058
+
1059
+ //CreateCursor return the meta data of the Cursor in this application
1060
+ func (app * App ) CreateCursor (fields []string , query string , size uint64 ) (* Cursor , error ) {
1061
+ type cursor struct {
1062
+ App uint64 `json:"app"`
1063
+ Fields []string `json:"fields"`
1064
+ Size uint64 `json:"size"`
1065
+ Query string `json:"query"`
1066
+ }
1067
+ data := cursor {App : app .AppId , Fields : fields , Size : size , Query : query }
1068
+ jsonData , _ := json .Marshal (data )
1069
+ url := app .createUrl ("records/cursor" , "" )
1070
+ request , err := app .NewRequest ("POST" , url .String (), bytes .NewBuffer (jsonData ))
1071
+ if err != nil {
1072
+ return nil , err
1073
+ }
1074
+ response , err := app .do (request )
1075
+ if err != nil {
1076
+ return nil , err
1077
+ }
1078
+ body , err := parseResponse (response )
1079
+ if err != nil {
1080
+ return nil , err
1081
+ }
1082
+ result , err := decodeCursor (body )
1083
+ return result , nil
1084
+ }
1085
+
1086
+ // DeleteCursor - Delete cursor by id
1087
+ func (app * App ) DeleteCursor (id string ) error {
1088
+ type requestBody struct {
1089
+ Id string `json:"id"`
1090
+ }
1091
+ data , err := json .Marshal (requestBody {Id : id })
1092
+ if err != nil {
1093
+ return err
1094
+ }
1095
+
1096
+ url := app .createUrl ("records/cursor" , "" )
1097
+ request , err := app .NewRequest ("DELETE" , url .String (), bytes .NewBuffer (data ))
1098
+ if err != nil {
1099
+ return err
1100
+ }
1101
+
1102
+ response , err := app .do (request )
1103
+ if err != nil {
1104
+ return err
1105
+ }
1106
+
1107
+ _ , err = parseResponse (response )
1108
+ if err != nil {
1109
+ return err
1110
+ }
1111
+
1112
+ return nil
1113
+ }
1114
+
1115
+ //Using Cursor Id to get all records
1116
+ //GetRecordsByCursor return the meta data of the Record in this application
1117
+ func (app * App ) GetRecordsByCursor (id string ) (* GetRecordsCursorResponse , error ) {
1118
+ url := app .createUrl ("records/cursor" , "id=" + id )
1119
+ request , err := app .NewRequest ("GET" , url .String (), nil )
1120
+ if err != nil {
1121
+ return nil , err
1122
+ }
1123
+ response , err := app .do (request )
1124
+ if err != nil {
1125
+ return nil , err
1126
+ }
1127
+ data , err := parseResponse (response )
1128
+ if err != nil {
1129
+ return nil , err
1130
+ }
1131
+ recordsCursorResponse , err := DecodeGetRecordsCursorResponse (data )
1132
+ if err != nil {
1133
+ return nil , err
1134
+ }
1135
+ return recordsCursorResponse , nil
1136
+ }
0 commit comments