Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse response and return to caller #3

Merged
merged 1 commit into from Jul 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions README
Expand Up @@ -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
Expand All @@ -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")

29 changes: 22 additions & 7 deletions flickr.go
Expand Up @@ -3,6 +3,7 @@ package flickr
import (
"bytes"
"crypto/md5"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}