Skip to content

Loading…

Parse response and return to caller #3

Merged
merged 1 commit into from

2 participants

@grennis

No description provided.

@mncaudill mncaudill merged commit 92d049c into mncaudill:master
@mncaudill
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 15, 2014
  1. @grennis
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
}
Something went wrong with that request. Please try again.