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

Fix panic, use standard errors package and return more errors. #7

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions flickr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"crypto/md5"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -43,14 +44,17 @@ type nopCloser struct {

func (nopCloser) Close() error { return nil }

type Error string
var ErrNeedBothAPIKeyAndMethod = errors.New("Need both API key and method")

func (e Error) Error() string {
return string(e)
func (request *Request) GetArgs() map[string]string {
if request.Args == nil {
request.Args = make(map[string]string)
}
return request.Args
}

func (request *Request) Sign(secret string) {
args := request.Args
args := request.GetArgs()

// Remove api_sig
delete(args, "api_sig")
Expand Down Expand Up @@ -82,16 +86,12 @@ func (request *Request) Sign(secret string) {
delete(args, "api_key")
delete(args, "method")

// Have the full string, now hash
hash := md5.New()
hash.Write([]byte(s))

// Add api_sig as one of the args
args["api_sig"] = fmt.Sprintf("%x", hash.Sum(nil))
args["api_sig"] = fmt.Sprintf("%x", md5.Sum([]byte(s)))
}

func (request *Request) URL() string {
args := request.Args
args := request.GetArgs()

args["api_key"] = request.ApiKey
args["method"] = request.Method
Expand All @@ -102,19 +102,19 @@ func (request *Request) URL() string {

func (request *Request) Execute() (response string, ret error) {
if request.ApiKey == "" || request.Method == "" {
return "", Error("Need both API key and method")
return "", ErrNeedBothAPIKeyAndMethod
}

s := request.URL()

res, err := http.Get(s)
defer res.Body.Close()
if err != nil {
return "", err
}
defer res.Body.Close()

body, _ := ioutil.ReadAll(res.Body)
return string(body), nil
body, err := ioutil.ReadAll(res.Body)
return string(body), err
}

func encodeQuery(args map[string]string) string {
Expand All @@ -131,7 +131,10 @@ func encodeQuery(args map[string]string) string {
}

func (request *Request) buildPost(url_ string, filename string, filetype string) (*http.Request, error) {
real_url, _ := url.Parse(url_)
real_url, err := url.Parse(url_)
if err != nil {
return nil, err
}

f, err := os.Open(filename)
if err != nil {
Expand All @@ -144,13 +147,14 @@ func (request *Request) buildPost(url_ string, filename string, filetype string)
}
f_size := stat.Size()

request.Args["api_key"] = request.ApiKey
args := request.GetArgs()
args["api_key"] = request.ApiKey

boundary, end := "----###---###--flickr-go-rules", "\r\n"

// Build out all of POST body sans file
header := bytes.NewBuffer(nil)
for k, v := range request.Args {
for k, v := range args {
header.WriteString("--" + boundary + end)
header.WriteString("Content-Disposition: form-data; name=\"" + k + "\"" + end + end)
header.WriteString(v + end)
Expand Down Expand Up @@ -218,8 +222,11 @@ func sendPost(postRequest *http.Request) (response *Response, err error) {
if err != nil {
return nil, err
}
rawBody, _ := ioutil.ReadAll(resp.Body)
rawBody, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}

var r Response
err = xml.Unmarshal(rawBody, &r)
Expand Down