github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/backend/yandex/api/types.go (about)

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