Browse files

Merge pull request #3 from grennis/master

Parse response and return to caller
  • Loading branch information...
2 parents 0d5dc58 + 2596e7b commit 92d049cd3dd88beb13d0322c1d8e68b94cbd439a @mncaudill committed Jul 16, 2014
Showing with 32 additions and 11 deletions.
  1. +10 −4 README
  2. +22 −7 flickr.go
View
14 README
@@ -14,8 +14,7 @@ I don't really know Go so if you see something that could be more idiomatic or w
The godoc below is the best place to learn how to use this library.
To compile and install:
-make
-make install
+go build
To test:
gotest
@@ -39,15 +38,22 @@ type Request struct {
Args map[string]string
}
+type Response struct {
+ Status string `xml:"stat,attr"`
+ Error *ResponseError `xml:"err"`
+ Payload map[string]bool
+}
+
func (request *Request) Execute() (response string, ret os.Error)
-func (request *Request) Replace(filename string, filetype string) (response string, err os.Error)
+func (request *Request) Replace(filename string, filetype string) (response Response, err os.Error)
func (request *Request) Sign(secret string)
func (request *Request) URL() string
-func (request *Request) Upload(filename string, filetype string) (response string, err os.Error)
+func (request *Request) Upload(filename string, filetype string) (response Response, err os.Error)
+
Example:
r.Upload("thumb.jpg", "image/jpeg")
View
29 flickr.go
@@ -3,6 +3,7 @@ package flickr
import (
"bytes"
"crypto/md5"
+ "encoding/xml"
"fmt"
"io"
"io/ioutil"
@@ -25,6 +26,17 @@ type Request struct {
Args map[string]string
}
+type Response struct {
+ Status string `xml:"stat,attr"`
+ Error *ResponseError `xml:"err"`
+ Payload string `xml:",innerxml"`
+}
+
+type ResponseError struct {
+ Code string `xml:"code,attr"`
+ Message string `xml:"msg,attr"`
+}
+
type nopCloser struct {
io.Reader
}
@@ -182,32 +194,35 @@ func (request *Request) buildPost(url_ string, filename string, filetype string)
// Example:
// r.Upload("thumb.jpg", "image/jpeg")
-func (request *Request) Upload(filename string, filetype string) (response string, err error) {
+func (request *Request) Upload(filename string, filetype string) (response *Response, err error) {
postRequest, err := request.buildPost(uploadEndpoint, filename, filetype)
if err != nil {
- return "", err
+ return nil, err
}
return sendPost(postRequest)
}
-func (request *Request) Replace(filename string, filetype string) (response string, err error) {
+func (request *Request) Replace(filename string, filetype string) (response *Response, err error) {
postRequest, err := request.buildPost(replaceEndpoint, filename, filetype)
if err != nil {
- return "", err
+ return nil, err
}
return sendPost(postRequest)
}
-func sendPost(postRequest *http.Request) (body string, err error) {
+func sendPost(postRequest *http.Request) (response *Response, err error) {
// Create and use TCP connection (lifted mostly wholesale from http.send)
client := &http.DefaultClient
resp, err := client.Do(postRequest)
if err != nil {
- return "", err
+ return nil, err
}
rawBody, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
- return string(rawBody), nil
+ var r Response
+ err = xml.Unmarshal(rawBody, &r)
+
+ return &r, err
}

0 comments on commit 92d049c

Please sign in to comment.