rest, b2, onedrive: remove Absolute parameter from rest.Opts and replace with RootURL

This commit is contained in:
Nick Craig-Wood 2017-07-07 08:18:13 +01:00
parent 5f70746d39
commit 384724fd11
4 changed files with 27 additions and 39 deletions

View file

@ -227,7 +227,7 @@ func errorHandler(resp *http.Response) error {
// NewFs contstructs an Fs from the path, bucket:path // NewFs contstructs an Fs from the path, bucket:path
func NewFs(name, root string) (fs.Fs, error) { func NewFs(name, root string) (fs.Fs, error) {
if uploadCutoff < chunkSize { if uploadCutoff < chunkSize {
return nil, errors.Errorf("b2: upload cutoff must be less than chunk size %v - was %v", chunkSize, uploadCutoff) return nil, errors.Errorf("b2: upload cutoff (%v) must be greater than or equal to chunk size (%v)", uploadCutoff, chunkSize)
} }
if chunkSize < minChunkSize { if chunkSize < minChunkSize {
return nil, errors.Errorf("b2: chunk size can't be less than %v - was %v", minChunkSize, chunkSize) return nil, errors.Errorf("b2: chunk size can't be less than %v - was %v", minChunkSize, chunkSize)
@ -303,9 +303,9 @@ func (f *Fs) authorizeAccount() error {
f.authMu.Lock() f.authMu.Lock()
defer f.authMu.Unlock() defer f.authMu.Unlock()
opts := rest.Opts{ opts := rest.Opts{
Absolute: true,
Method: "GET", Method: "GET",
Path: f.endpoint + "/b2api/v1/b2_authorize_account", Path: "/b2api/v1/b2_authorize_account",
RootURL: f.endpoint,
UserName: f.account, UserName: f.account,
Password: f.key, Password: f.key,
ExtraHeaders: map[string]string{"Authorization": ""}, // unset the Authorization for this request ExtraHeaders: map[string]string{"Authorization": ""}, // unset the Authorization for this request
@ -1119,8 +1119,7 @@ var _ io.ReadCloser = &openFile{}
func (o *Object) Open(options ...fs.OpenOption) (in io.ReadCloser, err error) { func (o *Object) Open(options ...fs.OpenOption) (in io.ReadCloser, err error) {
opts := rest.Opts{ opts := rest.Opts{
Method: "GET", Method: "GET",
Absolute: true, RootURL: o.fs.info.DownloadURL,
Path: o.fs.info.DownloadURL,
Options: options, Options: options,
} }
// Download by id if set otherwise by name // Download by id if set otherwise by name
@ -1323,8 +1322,7 @@ func (o *Object) Update(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOptio
opts := rest.Opts{ opts := rest.Opts{
Method: "POST", Method: "POST",
Absolute: true, RootURL: upload.UploadURL,
Path: upload.UploadURL,
Body: in, Body: in,
ExtraHeaders: map[string]string{ ExtraHeaders: map[string]string{
"Authorization": upload.AuthorizationToken, "Authorization": upload.AuthorizationToken,

View file

@ -164,8 +164,7 @@ func (up *largeUpload) transferChunk(part int64, body []byte) error {
// passed to b2_finish_large_file. // passed to b2_finish_large_file.
opts := rest.Opts{ opts := rest.Opts{
Method: "POST", Method: "POST",
Absolute: true, RootURL: upload.UploadURL,
Path: upload.UploadURL,
Body: fs.AccountPart(up.o, bytes.NewBuffer(body)), Body: fs.AccountPart(up.o, bytes.NewBuffer(body)),
ExtraHeaders: map[string]string{ ExtraHeaders: map[string]string{
"Authorization": upload.AuthorizationToken, "Authorization": upload.AuthorizationToken,

View file

@ -393,8 +393,8 @@ OUTER:
if result.NextLink == "" { if result.NextLink == "" {
break break
} }
opts.Path = result.NextLink opts.Path = ""
opts.Absolute = true opts.RootURL = result.NextLink
} }
return return
} }
@ -564,8 +564,7 @@ func (f *Fs) waitForJob(location string, o *Object) error {
for time.Now().Before(deadline) { for time.Now().Before(deadline) {
opts := rest.Opts{ opts := rest.Opts{
Method: "GET", Method: "GET",
Path: location, RootURL: location,
Absolute: true,
IgnoreStatus: true, // Ignore the http status response since it seems to return valid info on 500 errors IgnoreStatus: true, // Ignore the http status response since it seems to return valid info on 500 errors
} }
var resp *http.Response var resp *http.Response
@ -929,13 +928,11 @@ func (o *Object) createUploadSession() (response *api.CreateUploadResponse, err
func (o *Object) uploadFragment(url string, start int64, totalSize int64, chunk io.ReadSeeker, chunkSize int64) (err error) { func (o *Object) uploadFragment(url string, start int64, totalSize int64, chunk io.ReadSeeker, chunkSize int64) (err error) {
opts := rest.Opts{ opts := rest.Opts{
Method: "PUT", Method: "PUT",
Path: url, RootURL: url,
Absolute: true,
ContentLength: &chunkSize, ContentLength: &chunkSize,
ContentRange: fmt.Sprintf("bytes %d-%d/%d", start, start+chunkSize-1, totalSize), ContentRange: fmt.Sprintf("bytes %d-%d/%d", start, start+chunkSize-1, totalSize),
Body: chunk, Body: chunk,
} }
fs.Debugf(o, "OPTS: %s", opts.ContentRange)
var response api.UploadFragmentResponse var response api.UploadFragmentResponse
var resp *http.Response var resp *http.Response
err = o.fs.pacer.Call(func() (bool, error) { err = o.fs.pacer.Call(func() (bool, error) {
@ -950,8 +947,7 @@ func (o *Object) uploadFragment(url string, start int64, totalSize int64, chunk
func (o *Object) cancelUploadSession(url string) (err error) { func (o *Object) cancelUploadSession(url string) (err error) {
opts := rest.Opts{ opts := rest.Opts{
Method: "DELETE", Method: "DELETE",
Path: url, RootURL: url,
Absolute: true,
NoResponse: true, NoResponse: true,
} }
var resp *http.Response var resp *http.Response

View file

@ -61,7 +61,8 @@ func (api *Client) SetErrorHandler(fn func(resp *http.Response) error) *Client {
return api return api
} }
// SetRoot sets the default root URL // SetRoot sets the default RootURL. You can override this on a per
// call basis using the RootURL field in Opts.
func (api *Client) SetRoot(RootURL string) *Client { func (api *Client) SetRoot(RootURL string) *Client {
api.mu.Lock() api.mu.Lock()
defer api.mu.Unlock() defer api.mu.Unlock()
@ -80,8 +81,7 @@ func (api *Client) SetHeader(key, value string) *Client {
// Opts contains parameters for Call, CallJSON etc // Opts contains parameters for Call, CallJSON etc
type Opts struct { type Opts struct {
Method string // GET, POST etc Method string // GET, POST etc
Path string // relative to RootURL unless Absolute set Path string // relative to RootURL
Absolute bool // Path is absolute - dont add RootURL
RootURL string // override RootURL passed into SetRoot() RootURL string // override RootURL passed into SetRoot()
Body io.Reader Body io.Reader
NoResponse bool // set to close Body NoResponse bool // set to close Body
@ -147,11 +147,7 @@ func (api *Client) Call(opts *Opts) (resp *http.Response, err error) {
if opts == nil { if opts == nil {
return nil, errors.New("call() called with nil opts") return nil, errors.New("call() called with nil opts")
} }
var url string url := api.rootURL
if opts.Absolute {
url = opts.Path
} else {
url = api.rootURL
if opts.RootURL != "" { if opts.RootURL != "" {
url = opts.RootURL url = opts.RootURL
} }
@ -159,7 +155,6 @@ func (api *Client) Call(opts *Opts) (resp *http.Response, err error) {
return nil, errors.New("RootURL not set") return nil, errors.New("RootURL not set")
} }
url += opts.Path url += opts.Path
}
if opts.Parameters != nil && len(opts.Parameters) > 0 { if opts.Parameters != nil && len(opts.Parameters) > 0 {
url += "?" + opts.Parameters.Encode() url += "?" + opts.Parameters.Encode()
} }