Skip to content

Commit

Permalink
a Google API -> Go code generator and base library
Browse files Browse the repository at this point in the history
  • Loading branch information
bradfitz committed Aug 24, 2011
0 parents commit 12b21e3
Show file tree
Hide file tree
Showing 16 changed files with 738 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .hgignore
@@ -0,0 +1,12 @@
_obj
_testmain.go
_test
clientid.dat
clientsecret.dat
google-api-go-gen

syntax:glob
*.6
*.8
*~
*.out
27 changes: 27 additions & 0 deletions LICENSE
@@ -0,0 +1,27 @@
Copyright (c) 2011 Google Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4 changes: 4 additions & 0 deletions Makefile
@@ -0,0 +1,4 @@
all:
make -C google-api install
make -C google-api-go-generator install
google-api-go-generator/google-api-go-gen -cache -install -api=*
13 changes: 13 additions & 0 deletions NOTES
@@ -0,0 +1,13 @@
Discovery Service:
http://code.google.com/apis/discovery/
http://code.google.com/apis/discovery/v1/reference.html

The "type" key:
http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.1

The "format" key:
http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.23
http://code.google.com/apis/discovery/v1/reference.html#parameter-format-summary

Google JSON format docs:
http://google-styleguide.googlecode.com/svn/trunk/jsoncstyleguide.xml
10 changes: 10 additions & 0 deletions README
@@ -0,0 +1,10 @@
Most of this project is auto-generated.

The notable directories which are not auto-generated:

google-api-go-generator/ -- the generator itself
google-api/ -- shared common code, used by auto-generated code
examples/ -- sample code

When changing the generator, re-compile all APIs and submit the
modified APIs in the same CL as the generator changes itself.
8 changes: 8 additions & 0 deletions TODO
@@ -0,0 +1,8 @@
-- stringified ints (type "string", format "uint64", etc)
http://codereview.appspot.com/4918051/

-- more auto-generate docs

-- enums (check them? document them?)

-- more examples
13 changes: 13 additions & 0 deletions build-examples.sh
@@ -0,0 +1,13 @@
#!/bin/sh

set -e

make -C google-api install
make -C google-api-go-generator install
google-api-go-generator/google-api-go-gen -cache -api=buzz:v1
google-api-go-generator/google-api-go-gen -cache -api=tasks:v1
google-api-go-generator/google-api-go-gen -cache -api=urlshortener:v1
make -C buzz/v1 install
make -C tasks/v1 install
make -C urlshortener/v1 install
make -C examples
15 changes: 15 additions & 0 deletions examples/Makefile
@@ -0,0 +1,15 @@
include $(GOROOT)/src/Make.inc

PREREQ=$(QUOTED_GOROOT)/pkg/$(GOOS)_$(GOARCH)/google-api-go-client.googlecode.com/hg/buzz/v1.a\
$(QUOTED_GOROOT)/pkg/$(GOOS)_$(GOARCH)/google-api-go-client.googlecode.com/hg/tasks/v1.a\
$(QUOTED_GOROOT)/pkg/$(GOOS)_$(GOARCH)/google-api-go-client.googlecode.com/hg/urlshortener/v1.a\

TARG=go-api-demo

GOFILES=main.go\
debug.go\
buzz.go\
tasks.go\
urlshortener.go\

include $(GOROOT)/src/Make.cmd
45 changes: 45 additions & 0 deletions examples/buzz.go
@@ -0,0 +1,45 @@
package main

import (
"flag"
"http"
"os"
"log"

buzz "google-api-go-client.googlecode.com/hg/buzz/v1"
)

func init() {
registerDemo("buzz", buzz.BuzzScope, buzzMain)
}

func buzzMain(client *http.Client, argv []string) {
fs := flag.NewFlagSet("buzz", flag.ExitOnError)
includeMedia := fs.Bool("media", false, "Include an image")
fs.Parse(argv)

svc, _ := buzz.New(client)
call := svc.Activities.Insert("@me", &buzz.Activity{
Title: "a buzz post",
Object: &buzz.ActivityObject{
Type: "http://activitystrea.ms/schema/1.0/note",
Content: "the content of a buzz post",
},
})
if *includeMedia {
if f, err := findGopherFile(); err == nil {
call.Media(f)
defer f.Close()
}
}
activity, err := call.Do()
log.Printf("Buzz Activities.Insert: (%#v, %v)", activity, err)
}

func findGopherFile() (*os.File, os.Error) {
f, err := os.Open("gopher.png")
if err == nil {
return f, nil
}
return os.Open("examples/gopher.png")
}
72 changes: 72 additions & 0 deletions examples/debug.go
@@ -0,0 +1,72 @@
package main

import (
"bytes"
"fmt"
"http"
"io"
"io/ioutil"
"os"
)

type logTransport struct {
rt http.RoundTripper
}

func (t *logTransport) RoundTrip(req *http.Request) (*http.Response, os.Error) {
var buf bytes.Buffer

os.Stdout.Write([]byte("\n[request]\n"))
if req.Body != nil {
req.Body = ioutil.NopCloser(&readButCopy{req.Body, &buf})
}
req.Write(os.Stdout)
if req.Body != nil {
req.Body = ioutil.NopCloser(&buf)
}
os.Stdout.Write([]byte("\n[/request]\n"))

res, err := t.rt.RoundTrip(req)

fmt.Printf("[response]\n")
if err != nil {
fmt.Printf("ERROR: %v", err)
} else {
body := res.Body
res.Body = nil
res.Write(os.Stdout)
if body != nil {
res.Body = ioutil.NopCloser(&echoAsRead{body})
}
}

return res, err
}

type echoAsRead struct {
src io.Reader
}

func (r *echoAsRead) Read(p []byte) (int, os.Error) {
n, err := r.src.Read(p)
if n > 0 {
os.Stdout.Write(p[:n])
}
if err == os.EOF {
fmt.Printf("\n[/response]\n")
}
return n, err
}

type readButCopy struct {
src io.Reader
dst io.Writer
}

func (r *readButCopy) Read(p []byte) (int, os.Error) {
n, err := r.src.Read(p)
if n > 0 {
r.dst.Write(p[:n])
}
return n, err
}
Binary file added examples/gopher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 12b21e3

Please sign in to comment.