Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit f42eaf1121a807a709104dbeaac7d4252f1c85fb @mncaudill committed Jan 11, 2011
Showing with 147 additions and 0 deletions.
  1. +19 −0 LICENSE
  2. +15 −0 README
  3. +93 −0 flickr.go
  4. +20 −0 flickr_test.go
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (C) 2011 by Matthew Nolan Caudill (nolan@nolancaudill.com)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
15 README
@@ -0,0 +1,15 @@
+===================================================
+go-flickr: A interface to Flickr's API in Google Go
+Author: Nolan Caudill
+Date: 2011-01-10
+License: MIT
+===================================================
+
+This is a simple wrapper in Go to talk to Flickr.
+
+I wrote this mainly as an exercise to learn Go but it is functional.
+
+The best documentation is in flickr_test.go on how to use this package.
+
+TODO:
+- Upload
93 flickr.go
@@ -0,0 +1,93 @@
+package flickr
+
+import (
+ "os"
+ "fmt"
+ "http"
+ "sort"
+ "io/ioutil"
+ "crypto/md5"
+)
+
+const (
+ endpoint = "http://api.flickr.com/services/rest/?"
+)
+
+type Request struct {
+ ApiKey string
+ Method string
+ Args map[string]string
+}
+
+// So we can return custom errors
+type Error string
+
+func (e Error) String() string {
+ return string(e)
+}
+
+func (request *Request) Sign(secret string) {
+ args := request.Args
+
+ sorted_keys := make([]string, len(args)+2)
+
+ args["api_key"] = request.ApiKey
+ args["method"] = request.Method
+
+ // Sort array keys
+ i := 0
+ for k, _ := range args {
+ sorted_keys[i] = k
+ i++
+ }
+ sort.SortStrings(sorted_keys)
+
+ // Build out ordered key-value string prefixed by secret
+ s := secret
+ for _, key := range sorted_keys {
+ s += fmt.Sprintf("%s%s", key, args[key])
+ }
+
+ // Since we're only adding two keys, it's easier
+ // and more space-efficient to just delete them
+ // them copy the whole map
+ args["api_key"] = "", false
+ args["method"] = "", false
+
+ // 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())
+}
+
+func (request *Request) Execute() (response string, ret os.Error) {
+ args := request.Args
+
+ if request.ApiKey == "" || request.Method == "" {
+ return "", Error("Need both API key and method")
+ }
+
+ args["api_key"] = request.ApiKey
+ args["method"] = request.Method
+
+ s := endpoint
+ i := 0
+ for k, v := range args {
+ if i != 0 {
+ s += "&"
+ }
+ i++
+ s += fmt.Sprintf("%s=%s", k, v)
+ }
+
+ res, _, err := http.Get(s)
+ if err != nil {
+ return "", err
+ }
+ defer res.Body.Close()
+
+ body, _ := ioutil.ReadAll(res.Body)
+ return string(body), nil
+}
20 flickr_test.go
@@ -0,0 +1,20 @@
+package main
+
+import "./flickr"
+import "fmt"
+
+func main() {
+ r := new(flickr.Request)
+ r.ApiKey = "APIKEYGOESHERE"
+ r.Method = "flickr.photos.getInfo"
+
+ r.Args = make(map[string]string, 10)
+ r.Args["photo_id"] = "5336400553"
+
+ r.Sign("APISECRETGOESHERE")
+ body, err := r.Execute()
+ if err != nil {
+ fmt.Println("Something went wrong:", err)
+ }
+ fmt.Println(body)
+}

0 comments on commit f42eaf1

Please sign in to comment.