Skip to content

Commit

Permalink
new UploadFile and UploadReader functions
Browse files Browse the repository at this point in the history
  • Loading branch information
masci committed Jul 30, 2015
1 parent bdd5068 commit 3d512dc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ Different methods may return different kind of responses.

There are a number of functions that don't map any actual Flickr Api method
(see below for the detailed list). For example, to upload a photo, you call the
`UploadPhoto` function in the `flickr` package:
`UploadFile` or `UploadReader` functions in the `flickr` package:

```go
import "github.com/masci/flickr.go/flickr"


// upload the image file with default (nil) options
resp, err := flickr.UploadPhoto(client, "/path/to/image", nil)
resp, err := flickr.UploadFile(client, "/path/to/image", nil)
```

### Authentication (or how to retrieve OAuth credentials)
Expand Down
4 changes: 2 additions & 2 deletions examples/upload/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {
path, _ := filepath.Abs("examples/upload/gopher.jpg")
params := flickr.NewUploadParams()
params.Title = "A Gopher"
resp, err := flickr.UploadPhoto(client, path, params)
resp, err := flickr.UploadFile(client, path, params)
if err != nil {
fmt.Println("Failed uploading:", err, resp.ErrorMsg())
os.Exit(1)
Expand All @@ -60,7 +60,7 @@ func main() {

// upload another photo using default params
path, _ = filepath.Abs("examples/upload/gophers.jpg")
resp, err = flickr.UploadPhoto(client, path, nil)
resp, err = flickr.UploadFile(client, path, nil)
if err != nil {
fmt.Println("Failed uploading:", err, resp.ErrorMsg())
os.Exit(1)
Expand Down
21 changes: 13 additions & 8 deletions flickr/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,18 @@ func fillArgsWithParams(client *FlickrClient, params *UploadParams) {
// no parameters will be added to the request and Flickr will set User's
// default preferences.
// This call must be signed with write permissions
func UploadPhoto(client *FlickrClient, path string, optionalParams *UploadParams) (*UploadResponse, error) {
func UploadFile(client *FlickrClient, path string, optionalParams *UploadParams) (*UploadResponse, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()

return UploadReader(client, file, file.Name(), optionalParams)
}

//
func UploadReader(client *FlickrClient, photoReader io.Reader, name string, optionalParams *UploadParams) (*UploadResponse, error) {
client.EndpointUrl = UPLOAD_ENDPOINT
client.HTTPVerb = "POST"
client.SetOAuthDefaults()
Expand All @@ -113,13 +124,7 @@ func UploadPhoto(client *FlickrClient, path string, optionalParams *UploadParams

client.OAuthSign()

file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()

body, ctype, err := getUploadBody(client, file, file.Name())
body, ctype, err := getUploadBody(client, photoReader, name)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions flickr/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,34 @@ func TestGetUploadBody(t *testing.T) {
Expect(t, strings.Contains(body.String(), "foo"), true)
}

func TestUploadPhoto(t *testing.T) {
func TestUploadFile(t *testing.T) {
fclient := GetTestClient()
server, client := FlickrMock(200, `<?xml version="1.0" encoding="utf-8" ?><rsp stat="ok"></rsp>`, "")
defer server.Close()
fclient.HTTPClient = client
params := NewUploadParams()

resp, err := UploadPhoto(fclient, "", params)
resp, err := UploadFile(fclient, "", params)
Expect(t, resp == nil, true) // comparing nil interfaces would fail
_, ok := err.(*os.PathError)
Expect(t, ok, true)

fooFile, err := ioutil.TempFile("", "flickr.go")
defer fooFile.Close()
Expect(t, err, nil)
resp, err = UploadPhoto(fclient, fooFile.Name(), params)
resp, err = UploadFile(fclient, fooFile.Name(), params)
Expect(t, resp.HasErrors(), false)
}

func TestUploadPhotoKo(t *testing.T) {
func TestUploadFileKo(t *testing.T) {
fclient := GetTestClient()
server, client := FlickrMock(200, `<?xml version="1.0" encoding="utf-8" ?><rsp stat="fail"></rsp>`, "")
defer server.Close()
fclient.HTTPClient = client

fooFile, err := ioutil.TempFile("", "flickr.go")
defer fooFile.Close()
resp, err := UploadPhoto(fclient, fooFile.Name(), nil)
resp, err := UploadFile(fclient, fooFile.Name(), nil)
_, ok := err.(*flickErr.Error)
Expect(t, ok, true)
Expect(t, resp.HasErrors(), true)
Expand Down

0 comments on commit 3d512dc

Please sign in to comment.