github.com/artpar/rclone@v1.67.3/backend/hidrive/api/queries.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/url"
     6  	"path"
     7  	"strings"
     8  	"time"
     9  )
    10  
    11  // Some presets for different amounts of information that can be requested for fields;
    12  // it is recommended to only request the information that is actually needed.
    13  var (
    14  	HiDriveObjectNoMetadataFields            = []string{"name", "type"}
    15  	HiDriveObjectWithMetadataFields          = append(HiDriveObjectNoMetadataFields, "id", "size", "mtime", "chash")
    16  	HiDriveObjectWithDirectoryMetadataFields = append(HiDriveObjectWithMetadataFields, "nmembers")
    17  	DirectoryContentFields                   = []string{"nmembers"}
    18  )
    19  
    20  // QueryParameters represents the parameters passed to an API-call.
    21  type QueryParameters struct {
    22  	url.Values
    23  }
    24  
    25  // NewQueryParameters initializes an instance of QueryParameters and
    26  // returns a pointer to it.
    27  func NewQueryParameters() *QueryParameters {
    28  	return &QueryParameters{url.Values{}}
    29  }
    30  
    31  // SetFileInDirectory sets the appropriate parameters
    32  // to specify a path to a file in a directory.
    33  // This is used by requests that work with paths for files that do not exist yet.
    34  // (For example when creating a file).
    35  // Most requests use the format produced by SetPath(...).
    36  func (p *QueryParameters) SetFileInDirectory(filePath string) {
    37  	directory, file := path.Split(path.Clean(filePath))
    38  	p.Set("dir", path.Clean(directory))
    39  	p.Set("name", file)
    40  	// NOTE: It would be possible to switch to pid-based requests
    41  	// by modifying this function.
    42  }
    43  
    44  // SetPath sets the appropriate parameters to access the given path.
    45  func (p *QueryParameters) SetPath(objectPath string) {
    46  	p.Set("path", path.Clean(objectPath))
    47  	// NOTE: It would be possible to switch to pid-based requests
    48  	// by modifying this function.
    49  }
    50  
    51  // SetTime sets the key to the time-value. It replaces any existing values.
    52  func (p *QueryParameters) SetTime(key string, value time.Time) error {
    53  	valueAPI := Time(value)
    54  	valueBytes, err := json.Marshal(&valueAPI)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	p.Set(key, string(valueBytes))
    59  	return nil
    60  }
    61  
    62  // AddList adds the given values as a list
    63  // with each value separated by the separator.
    64  // It appends to any existing values associated with key.
    65  func (p *QueryParameters) AddList(key string, separator string, values ...string) {
    66  	original := p.Get(key)
    67  	p.Set(key, strings.Join(values, separator))
    68  	if original != "" {
    69  		p.Set(key, original+separator+p.Get(key))
    70  	}
    71  }
    72  
    73  // AddFields sets the appropriate parameter to access the given fields.
    74  // The given fields will be appended to any other existing fields.
    75  func (p *QueryParameters) AddFields(prefix string, fields ...string) {
    76  	modifiedFields := make([]string, len(fields))
    77  	for i, field := range fields {
    78  		modifiedFields[i] = prefix + field
    79  	}
    80  	p.AddList("fields", ",", modifiedFields...)
    81  }