github.com/artpar/rclone@v1.67.3/backend/yandex/api/types.go (about) 1 // Package api provides types used by the Yandex API. 2 package api 3 4 import ( 5 "fmt" 6 "strings" 7 ) 8 9 // DiskInfo contains disk metadata 10 type DiskInfo struct { 11 TotalSpace int64 `json:"total_space"` 12 UsedSpace int64 `json:"used_space"` 13 TrashSize int64 `json:"trash_size"` 14 } 15 16 // ResourceInfoRequestOptions struct 17 type ResourceInfoRequestOptions struct { 18 SortMode *SortMode 19 Limit uint64 20 Offset uint64 21 Fields []string 22 } 23 24 // ResourceInfoResponse struct is returned by the API for metadata requests. 25 type ResourceInfoResponse struct { 26 PublicKey string `json:"public_key"` 27 Name string `json:"name"` 28 Created string `json:"created"` 29 CustomProperties map[string]interface{} `json:"custom_properties"` 30 Preview string `json:"preview"` 31 PublicURL string `json:"public_url"` 32 OriginPath string `json:"origin_path"` 33 Modified string `json:"modified"` 34 Path string `json:"path"` 35 Md5 string `json:"md5"` 36 ResourceType string `json:"type"` 37 MimeType string `json:"mime_type"` 38 Size int64 `json:"size"` 39 Embedded *ResourceListResponse `json:"_embedded"` 40 } 41 42 // ResourceListResponse struct 43 type ResourceListResponse struct { 44 Sort *SortMode `json:"sort"` 45 PublicKey string `json:"public_key"` 46 Items []ResourceInfoResponse `json:"items"` 47 Path string `json:"path"` 48 Limit *uint64 `json:"limit"` 49 Offset *uint64 `json:"offset"` 50 Total *uint64 `json:"total"` 51 } 52 53 // AsyncInfo struct is returned by the API for various async operations. 54 type AsyncInfo struct { 55 HRef string `json:"href"` 56 Method string `json:"method"` 57 Templated bool `json:"templated"` 58 } 59 60 // AsyncStatus is returned when requesting the status of an async operations. Possible values in-progress, success, failure 61 type AsyncStatus struct { 62 Status string `json:"status"` 63 } 64 65 // CustomPropertyResponse struct we send and is returned by the API for CustomProperty request. 66 type CustomPropertyResponse struct { 67 CustomProperties map[string]interface{} `json:"custom_properties"` 68 } 69 70 // SortMode struct - sort mode 71 type SortMode struct { 72 mode string 73 } 74 75 // Default - sort mode 76 func (m *SortMode) Default() *SortMode { 77 return &SortMode{ 78 mode: "", 79 } 80 } 81 82 // ByName - sort mode 83 func (m *SortMode) ByName() *SortMode { 84 return &SortMode{ 85 mode: "name", 86 } 87 } 88 89 // ByPath - sort mode 90 func (m *SortMode) ByPath() *SortMode { 91 return &SortMode{ 92 mode: "path", 93 } 94 } 95 96 // ByCreated - sort mode 97 func (m *SortMode) ByCreated() *SortMode { 98 return &SortMode{ 99 mode: "created", 100 } 101 } 102 103 // ByModified - sort mode 104 func (m *SortMode) ByModified() *SortMode { 105 return &SortMode{ 106 mode: "modified", 107 } 108 } 109 110 // BySize - sort mode 111 func (m *SortMode) BySize() *SortMode { 112 return &SortMode{ 113 mode: "size", 114 } 115 } 116 117 // Reverse - sort mode 118 func (m *SortMode) Reverse() *SortMode { 119 if strings.HasPrefix(m.mode, "-") { 120 return &SortMode{ 121 mode: m.mode[1:], 122 } 123 } 124 return &SortMode{ 125 mode: "-" + m.mode, 126 } 127 } 128 129 func (m *SortMode) String() string { 130 return m.mode 131 } 132 133 // UnmarshalJSON sort mode 134 func (m *SortMode) UnmarshalJSON(value []byte) error { 135 if len(value) == 0 { 136 m.mode = "" 137 return nil 138 } 139 m.mode = string(value) 140 if strings.HasPrefix(m.mode, "\"") && strings.HasSuffix(m.mode, "\"") { 141 m.mode = m.mode[1 : len(m.mode)-1] 142 } 143 return nil 144 } 145 146 // ErrorResponse represents erroneous API response. 147 // Implements go's built in `error`. 148 type ErrorResponse struct { 149 ErrorName string `json:"error"` 150 Description string `json:"description"` 151 Message string `json:"message"` 152 153 StatusCode int `json:""` 154 } 155 156 func (e *ErrorResponse) Error() string { 157 return fmt.Sprintf("[%d - %s] %s (%s)", e.StatusCode, e.ErrorName, e.Description, e.Message) 158 }