github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/api/client/objects.go (about) 1 package client 2 3 import ( 4 "io" 5 "net/http" 6 7 "github.com/hoffie/larasync/api/common" 8 ) 9 10 // putObjectRequest builds a request for uploading an object 11 func (c *Client) putObjectRequest(objectID string, content io.Reader) (*http.Request, error) { 12 req, err := http.NewRequest("PUT", c.BaseURL+"/blobs/"+objectID, 13 content) 14 if err != nil { 15 return nil, err 16 } 17 req.Header.Set("Content-Type", "application/octet-stream") 18 common.SignWithKey(req, c.signingPrivateKey) 19 return req, nil 20 } 21 22 // PutObject uploads an object to the server 23 func (c *Client) PutObject(objectID string, content io.Reader) error { 24 req, err := c.putObjectRequest(objectID, content) 25 if err != nil { 26 return err 27 } 28 _, err = c.doRequest(req, http.StatusCreated, http.StatusOK) 29 return err 30 } 31 32 // getObjectRequest builds a request for downloading an object 33 func (c *Client) getObjectRequest(objectID string) (*http.Request, error) { 34 req, err := http.NewRequest("GET", c.BaseURL+"/blobs/"+objectID, nil) 35 if err != nil { 36 return nil, err 37 } 38 common.SignWithKey(req, c.signingPrivateKey) 39 return req, nil 40 } 41 42 // GetObject downloads an object from the server 43 func (c *Client) GetObject(objectID string) (io.Reader, error) { 44 req, err := c.getObjectRequest(objectID) 45 if err != nil { 46 return nil, err 47 } 48 resp, err := c.doRequest(req, http.StatusCreated, http.StatusOK) 49 if err != nil { 50 return nil, err 51 } 52 return resp.Body, err 53 }