github.com/artpar/rclone@v1.67.3/backend/box/api/types.go (about) 1 // Package api has type definitions for box 2 // 3 // Converted from the API docs with help from https://mholt.github.io/json-to-go/ 4 package api 5 6 import ( 7 "encoding/json" 8 "fmt" 9 "time" 10 ) 11 12 const ( 13 // 2017-05-03T07:26:10-07:00 14 timeFormat = `"` + time.RFC3339 + `"` 15 ) 16 17 // Time represents date and time information for the 18 // box API, by using RFC3339 19 type Time time.Time 20 21 // MarshalJSON turns a Time into JSON (in UTC) 22 func (t *Time) MarshalJSON() (out []byte, err error) { 23 timeString := (*time.Time)(t).Format(timeFormat) 24 return []byte(timeString), nil 25 } 26 27 // UnmarshalJSON turns JSON into a Time 28 func (t *Time) UnmarshalJSON(data []byte) error { 29 newT, err := time.Parse(timeFormat, string(data)) 30 if err != nil { 31 return err 32 } 33 *t = Time(newT) 34 return nil 35 } 36 37 // Error is returned from box when things go wrong 38 type Error struct { 39 Type string `json:"type"` 40 Status int `json:"status"` 41 Code string `json:"code"` 42 ContextInfo json.RawMessage `json:"context_info"` 43 HelpURL string `json:"help_url"` 44 Message string `json:"message"` 45 RequestID string `json:"request_id"` 46 } 47 48 // Error returns a string for the error and satisfies the error interface 49 func (e *Error) Error() string { 50 out := fmt.Sprintf("Error %q (%d)", e.Code, e.Status) 51 if e.Message != "" { 52 out += ": " + e.Message 53 } 54 if e.ContextInfo != nil { 55 out += fmt.Sprintf(" (%s)", string(e.ContextInfo)) 56 } 57 return out 58 } 59 60 // Check Error satisfies the error interface 61 var _ error = (*Error)(nil) 62 63 // ItemFields are the fields needed for FileInfo 64 var ItemFields = "type,id,sequence_id,etag,sha1,name,size,created_at,modified_at,content_created_at,content_modified_at,item_status,shared_link,owned_by" 65 66 // Types of things in Item/ItemMini 67 const ( 68 ItemTypeFolder = "folder" 69 ItemTypeFile = "file" 70 ItemStatusActive = "active" 71 ItemStatusTrashed = "trashed" 72 ItemStatusDeleted = "deleted" 73 ) 74 75 // ItemMini is a subset of the elements in a full Item returned by some API calls 76 type ItemMini struct { 77 Type string `json:"type"` 78 ID string `json:"id"` 79 SequenceID int64 `json:"sequence_id,string"` 80 Etag string `json:"etag"` 81 SHA1 string `json:"sha1"` 82 Name string `json:"name"` 83 } 84 85 // Item describes a folder or a file as returned by Get Folder Items and others 86 type Item struct { 87 Type string `json:"type"` 88 ID string `json:"id"` 89 SequenceID int64 `json:"sequence_id,string"` 90 Etag string `json:"etag"` 91 SHA1 string `json:"sha1"` 92 Name string `json:"name"` 93 Size float64 `json:"size"` // box returns this in xEyy format for very large numbers - see #2261 94 CreatedAt Time `json:"created_at"` 95 ModifiedAt Time `json:"modified_at"` 96 ContentCreatedAt Time `json:"content_created_at"` 97 ContentModifiedAt Time `json:"content_modified_at"` 98 ItemStatus string `json:"item_status"` // active, trashed if the file has been moved to the trash, and deleted if the file has been permanently deleted 99 Parent ItemMini `json:"parent"` 100 SharedLink struct { 101 URL string `json:"url,omitempty"` 102 Access string `json:"access,omitempty"` 103 } `json:"shared_link"` 104 OwnedBy struct { 105 Type string `json:"type"` 106 ID string `json:"id"` 107 Name string `json:"name"` 108 Login string `json:"login"` 109 } `json:"owned_by"` 110 } 111 112 // ModTime returns the modification time of the item 113 func (i *Item) ModTime() (t time.Time) { 114 t = time.Time(i.ContentModifiedAt) 115 if t.IsZero() { 116 t = time.Time(i.ModifiedAt) 117 } 118 return t 119 } 120 121 // FolderItems is returned from the GetFolderItems call 122 type FolderItems struct { 123 TotalCount int `json:"total_count"` 124 Entries []Item `json:"entries"` 125 Offset int `json:"offset"` 126 Limit int `json:"limit"` 127 NextMarker *string `json:"next_marker,omitempty"` 128 Order []struct { 129 By string `json:"by"` 130 Direction string `json:"direction"` 131 } `json:"order"` 132 } 133 134 // Parent defined the ID of the parent directory 135 type Parent struct { 136 ID string `json:"id"` 137 } 138 139 // CreateFolder is the request for Create Folder 140 type CreateFolder struct { 141 Name string `json:"name"` 142 Parent Parent `json:"parent"` 143 } 144 145 // UploadFile is the request for Upload File 146 type UploadFile struct { 147 Name string `json:"name"` 148 Parent Parent `json:"parent"` 149 ContentCreatedAt Time `json:"content_created_at"` 150 ContentModifiedAt Time `json:"content_modified_at"` 151 } 152 153 // PreUploadCheck is the request for upload preflight check 154 type PreUploadCheck struct { 155 Name string `json:"name"` 156 Parent Parent `json:"parent"` 157 Size *int64 `json:"size,omitempty"` 158 } 159 160 // PreUploadCheckResponse is the response from upload preflight check 161 // if successful 162 type PreUploadCheckResponse struct { 163 UploadToken string `json:"upload_token"` 164 UploadURL string `json:"upload_url"` 165 } 166 167 // PreUploadCheckConflict is returned in the ContextInfo error field 168 // from PreUploadCheck when the error code is "item_name_in_use" 169 type PreUploadCheckConflict struct { 170 Conflicts ItemMini `json:"conflicts"` 171 } 172 173 // UpdateFileModTime is used in Update File Info 174 type UpdateFileModTime struct { 175 ContentModifiedAt Time `json:"content_modified_at"` 176 } 177 178 // UpdateFileMove is the request for Upload File to change name and parent 179 type UpdateFileMove struct { 180 Name string `json:"name"` 181 Parent Parent `json:"parent"` 182 } 183 184 // CopyFile is the request for Copy File 185 type CopyFile struct { 186 Name string `json:"name"` 187 Parent Parent `json:"parent"` 188 } 189 190 // CreateSharedLink is the request for Public Link 191 type CreateSharedLink struct { 192 SharedLink struct { 193 URL string `json:"url,omitempty"` 194 Access string `json:"access,omitempty"` 195 } `json:"shared_link"` 196 } 197 198 // UploadSessionRequest is uses in Create Upload Session 199 type UploadSessionRequest struct { 200 FolderID string `json:"folder_id,omitempty"` // don't pass for update 201 FileSize int64 `json:"file_size"` 202 FileName string `json:"file_name,omitempty"` // optional for update 203 } 204 205 // UploadSessionResponse is returned from Create Upload Session 206 type UploadSessionResponse struct { 207 TotalParts int `json:"total_parts"` 208 PartSize int64 `json:"part_size"` 209 SessionEndpoints struct { 210 ListParts string `json:"list_parts"` 211 Commit string `json:"commit"` 212 UploadPart string `json:"upload_part"` 213 Status string `json:"status"` 214 Abort string `json:"abort"` 215 } `json:"session_endpoints"` 216 SessionExpiresAt Time `json:"session_expires_at"` 217 ID string `json:"id"` 218 Type string `json:"type"` 219 NumPartsProcessed int `json:"num_parts_processed"` 220 } 221 222 // Part defines the return from upload part call which are passed to commit upload also 223 type Part struct { 224 PartID string `json:"part_id"` 225 Offset int64 `json:"offset"` 226 Size int64 `json:"size"` 227 Sha1 string `json:"sha1"` 228 } 229 230 // UploadPartResponse is returned from the upload part call 231 type UploadPartResponse struct { 232 Part Part `json:"part"` 233 } 234 235 // CommitUpload is used in the Commit Upload call 236 type CommitUpload struct { 237 Parts []Part `json:"parts"` 238 Attributes struct { 239 ContentCreatedAt Time `json:"content_created_at"` 240 ContentModifiedAt Time `json:"content_modified_at"` 241 } `json:"attributes"` 242 } 243 244 // ConfigJSON defines the shape of a box config.json 245 type ConfigJSON struct { 246 BoxAppSettings AppSettings `json:"boxAppSettings"` 247 EnterpriseID string `json:"enterpriseID"` 248 } 249 250 // AppSettings defines the shape of the boxAppSettings within box config.json 251 type AppSettings struct { 252 ClientID string `json:"clientID"` 253 ClientSecret string `json:"clientSecret"` 254 AppAuth AppAuth `json:"appAuth"` 255 } 256 257 // AppAuth defines the shape of the appAuth within boxAppSettings in config.json 258 type AppAuth struct { 259 PublicKeyID string `json:"publicKeyID"` 260 PrivateKey string `json:"privateKey"` 261 Passphrase string `json:"passphrase"` 262 } 263 264 // User is returned from /users/me 265 type User struct { 266 Type string `json:"type"` 267 ID string `json:"id"` 268 Name string `json:"name"` 269 Login string `json:"login"` 270 CreatedAt time.Time `json:"created_at"` 271 ModifiedAt time.Time `json:"modified_at"` 272 Language string `json:"language"` 273 Timezone string `json:"timezone"` 274 SpaceAmount int64 `json:"space_amount"` 275 SpaceUsed int64 `json:"space_used"` 276 MaxUploadSize int64 `json:"max_upload_size"` 277 Status string `json:"status"` 278 JobTitle string `json:"job_title"` 279 Phone string `json:"phone"` 280 Address string `json:"address"` 281 AvatarURL string `json:"avatar_url"` 282 } 283 284 // FileTreeChangeEventTypes are the events that can require cache invalidation 285 var FileTreeChangeEventTypes = map[string]struct{}{ 286 "ITEM_COPY": {}, 287 "ITEM_CREATE": {}, 288 "ITEM_MAKE_CURRENT_VERSION": {}, 289 "ITEM_MODIFY": {}, 290 "ITEM_MOVE": {}, 291 "ITEM_RENAME": {}, 292 "ITEM_TRASH": {}, 293 "ITEM_UNDELETE_VIA_TRASH": {}, 294 "ITEM_UPLOAD": {}, 295 } 296 297 // Event is an array element in the response returned from /events 298 type Event struct { 299 EventType string `json:"event_type"` 300 EventID string `json:"event_id"` 301 Source Item `json:"source"` 302 } 303 304 // Events is returned from /events 305 type Events struct { 306 ChunkSize int64 `json:"chunk_size"` 307 Entries []Event `json:"entries"` 308 NextStreamPosition int64 `json:"next_stream_position"` 309 }