├── version.go ├── client ├── error.go ├── client_test.go └── client.go ├── models ├── build.go ├── author.go ├── account.go ├── book.go └── book_test.go ├── .gitignore ├── api ├── account_test.go ├── book_test.go ├── author.go ├── account.go ├── builds_test.go ├── books.go ├── book.go ├── books_test.go ├── leak_test.go └── builds.go ├── utils ├── targz.go ├── gitarchive.go └── cmdstream.go ├── api.go ├── README.md ├── streams └── streams.go └── LICENSE /version.go: -------------------------------------------------------------------------------- 1 | package gitbook 2 | 3 | const ( 4 | VERSION = "1.0.2" 5 | ) 6 | -------------------------------------------------------------------------------- /client/error.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | type Error struct { 4 | Msg string `json:"error"` 5 | Code int `json:"code"` 6 | } 7 | 8 | func (e *Error) Error() string { 9 | return e.Msg 10 | } 11 | -------------------------------------------------------------------------------- /models/build.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | type Build struct { 4 | Branch string `json:"branch"` 5 | Message string `json:"message"` 6 | Author BuildAuthor `json:"author"` 7 | } 8 | 9 | type BuildAuthor struct { 10 | Name string `json:"name"` 11 | Email string `json:"email"` 12 | } 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | -------------------------------------------------------------------------------- /models/author.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | // Author data as returned by the API 4 | // Author only contains public information 5 | type Author struct { 6 | Type string `json:"type"` 7 | Name string `json:"name"` 8 | Username string `json:"username"` 9 | Urls AuthorUrls `json:"urls"` 10 | } 11 | type AuthorUrls struct { 12 | Profile string `json:"profile"` 13 | Avatar string `json:"avatar"` 14 | } 15 | -------------------------------------------------------------------------------- /api/account_test.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/GitbookIO/go-gitbook-api/client" 7 | ) 8 | 9 | func TestAccount(t *testing.T) { 10 | c := client.NewClient(client.ClientOptions{ 11 | Host: "http://localhost:5000/api/", 12 | Username: "james", 13 | Password: "730e0de8-ca53-42f9-9ad3-49c547b0cdc0", 14 | }) 15 | a := Account{c} 16 | 17 | _, err := a.Get() 18 | if err != nil { 19 | t.Error(err) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /api/book_test.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/GitbookIO/go-gitbook-api/client" 7 | ) 8 | 9 | func TestBasic(t *testing.T) { 10 | c := client.NewClient(client.ClientOptions{ 11 | Host: "http://localhost:5000/api/", 12 | Username: "james", 13 | Password: "730e0de8-ca53-42f9-9ad3-49c547b0cdc0", 14 | }) 15 | b := Book{c} 16 | 17 | _, err := b.Get("james/test") 18 | if err != nil { 19 | t.Error(err) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /api/author.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/GitbookIO/go-gitbook-api/client" 7 | "github.com/GitbookIO/go-gitbook-api/models" 8 | ) 9 | 10 | type Author struct { 11 | Client *client.Client 12 | } 13 | 14 | func (a *Author) Get(username string) (models.Author, error) { 15 | author := models.Author{} 16 | 17 | _, err := a.Client.Get( 18 | fmt.Sprintf("/author/%s", username), 19 | nil, 20 | &author, 21 | ) 22 | 23 | return author, err 24 | } 25 | -------------------------------------------------------------------------------- /models/account.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | // Account data structure as returned by the API 4 | // Account extends Author with private fields 5 | type Account struct { 6 | Author 7 | 8 | Email string `json:"email"` 9 | Token string `json:"token"` 10 | GitHub *GitHubAccountInfo `json:"github"` 11 | } 12 | 13 | type GitHubAccountInfo struct { 14 | Username string `json:"username"` 15 | Token string `json:"token"` 16 | Scopes []string `json:"scopes"` 17 | } 18 | -------------------------------------------------------------------------------- /api/account.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "github.com/GitbookIO/go-gitbook-api/client" 5 | "github.com/GitbookIO/go-gitbook-api/models" 6 | ) 7 | 8 | type Account struct { 9 | Client *client.Client 10 | } 11 | 12 | // Get returns a books details for a given "bookId" 13 | // (for example "gitbookio/javascript") 14 | func (a *Account) Get() (models.Account, error) { 15 | account := models.Account{} 16 | 17 | _, err := a.Client.Get( 18 | "/account", 19 | nil, 20 | &account, 21 | ) 22 | 23 | return account, err 24 | } 25 | -------------------------------------------------------------------------------- /api/builds_test.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/GitbookIO/go-gitbook-api/client" 7 | ) 8 | 9 | func TestBuildsCreate(t *testing.T) { 10 | c := client.NewClient(client.ClientOptions{ 11 | Host: "stupid_host", 12 | Username: "badboy", 13 | Password: "password", 14 | }) 15 | b := Builds{c} 16 | 17 | err := b.BuildGit("james/test", "master", "/Users/aaron/git/documentation", "master", BuildOptions{ 18 | Branch: "master", 19 | }) 20 | if err != nil { 21 | t.Error(err) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /api/books.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "github.com/GitbookIO/go-gitbook-api/client" 5 | "github.com/GitbookIO/go-gitbook-api/models" 6 | ) 7 | 8 | type Books struct { 9 | Client *client.Client 10 | } 11 | 12 | type booksListResponse struct { 13 | List []models.Book `json:"list"` 14 | } 15 | 16 | func (b *Books) List() ([]models.Book, error) { 17 | resp := booksListResponse{} 18 | 19 | if _, err := b.Client.Get( 20 | "/books", 21 | nil, 22 | &resp, 23 | ); err != nil { 24 | return nil, err 25 | } 26 | 27 | return resp.List, nil 28 | } 29 | -------------------------------------------------------------------------------- /api/book.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/GitbookIO/go-gitbook-api/client" 7 | "github.com/GitbookIO/go-gitbook-api/models" 8 | ) 9 | 10 | type Book struct { 11 | Client *client.Client 12 | } 13 | 14 | // Get returns a books details for a given "bookId" 15 | // (for example "gitbookio/javascript") 16 | func (b *Book) Get(bookId string) (models.Book, error) { 17 | book := models.Book{} 18 | 19 | _, err := b.Client.Get( 20 | fmt.Sprintf("/book/%s", bookId), 21 | nil, 22 | &book, 23 | ) 24 | 25 | return book, err 26 | } 27 | -------------------------------------------------------------------------------- /api/books_test.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/GitbookIO/go-gitbook-api/client" 7 | ) 8 | 9 | func TestBooksList(t *testing.T) { 10 | c := client.NewClient(client.ClientOptions{ 11 | Host: "http://localhost:5000/api/", 12 | Username: "james", 13 | Password: "730e0de8-ca53-42f9-9ad3-49c547b0cdc0", 14 | }) 15 | b := Books{c} 16 | 17 | books, err := b.List() 18 | if err != nil { 19 | t.Error(err) 20 | } 21 | 22 | if len(books) < 1 { 23 | t.Errorf("Should have at least one book, found %d instead", len(books)) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /client/client_test.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestClientFork(t *testing.T) { 8 | // Create parent 9 | parent := NewClient(ClientOptions{}) 10 | 11 | // Set value in parent 12 | parent.Header.Set("a", "b") 13 | 14 | // Create child 15 | child := parent.Fork(ClientOptions{}) 16 | 17 | if child.Header.Get("a") != "b" { 18 | t.Errorf("Child should inherit headers from parent") 19 | } 20 | 21 | child.Header.Set("c", "d") 22 | if parent.Header.Get("c") == "d" { 23 | t.Errorf("Parent should share not child's headers") 24 | } 25 | 26 | if !(child.Header.Get("a") == "b" && child.Header.Get("c") == "d") { 27 | t.Errorf("Child should be able to keep it's own headers") 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /models/book.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | // Books data structure as returned by the API 4 | type Book struct { 5 | Id string `json:"id"` 6 | Name string `json:"name"` 7 | Title string `json:"title"` 8 | Description string `json:"description"` 9 | Urls struct { 10 | Access string `json:"access"` 11 | Homepage string `json:"homepage"` 12 | Read string `json:"read"` 13 | Reviews string `json:"reviews"` 14 | Subscribe string `json:"subscribe"` 15 | 16 | Download struct { 17 | Epub string `json:"epub"` 18 | Mobi string `json:"mobi"` 19 | Pdf string `json:"pdf"` 20 | } `json:"download"` 21 | } `json:"urls"` 22 | 23 | Author struct { 24 | Name string `json:"name"` 25 | Username string `json:"username"` 26 | } `json:"author"` 27 | 28 | Permissions struct { 29 | Read bool `json:"read"` 30 | Write bool `json:"write"` 31 | Manage bool `json:"manage"` 32 | } `json:"permissions"` 33 | 34 | LatestBuild struct { 35 | Version string `json:"version"` 36 | Finished string `json:"finished"` 37 | Started string `json:"started"` 38 | } `json:"latestBuild"` 39 | } 40 | -------------------------------------------------------------------------------- /utils/targz.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "io" 5 | "os/exec" 6 | ) 7 | 8 | var ( 9 | tarArgs = []string{"tar", "-cz", "-", "."} 10 | ) 11 | 12 | // TarGz returns a stream of tar.gz data of the directory 13 | func TarGz(dir string) (io.ReadCloser, error) { 14 | return tarCommand(tarArgs, dir) 15 | } 16 | 17 | // TarGzExclude returns a stream of tar.gz data of the directory 18 | // excluding the specified files 19 | func TarGzExclude(dir string, exclude ...string) (io.ReadCloser, error) { 20 | return tarCommand(tarExcludeArgs(exclude...), dir) 21 | } 22 | 23 | // Run tar command for a folder given the provided args 24 | func tarCommand(args []string, dir string) (io.ReadCloser, error) { 25 | cmd := exec.Command(args[0], args[1:]...) 26 | 27 | // Set target directory 28 | cmd.Dir = dir 29 | 30 | // Get stream 31 | return CmdStream(cmd, nil) 32 | } 33 | 34 | // Generate args for excluding files 35 | func tarExcludeArgs(files ...string) []string { 36 | excluding := []string{"tar"} 37 | 38 | for _, f := range files { 39 | excluding = append(excluding, "--exclude", f) 40 | } 41 | 42 | return append( 43 | excluding, 44 | "-cz", "-", ".", 45 | ) 46 | } 47 | -------------------------------------------------------------------------------- /api.go: -------------------------------------------------------------------------------- 1 | package gitbook 2 | 3 | import ( 4 | "github.com/GitbookIO/go-gitbook-api/api" 5 | "github.com/GitbookIO/go-gitbook-api/client" 6 | ) 7 | 8 | type API struct { 9 | // Author API client 10 | Author *api.Author 11 | // Authentication API client 12 | Account *api.Account 13 | // Individual book API client 14 | Book *api.Book 15 | // Book listing API client 16 | Books *api.Books 17 | // Builds API client 18 | Builds *api.Builds 19 | 20 | // Internal client 21 | Client *client.Client 22 | } 23 | 24 | type APIOptions client.ClientOptions 25 | 26 | func NewAPI(opts APIOptions) *API { 27 | c := client.NewClient(client.ClientOptions(opts)) 28 | return NewAPIFromClient(c) 29 | } 30 | 31 | func NewAPIFromClient(c *client.Client) *API { 32 | return &API{ 33 | Author: &api.Author{c}, 34 | Account: &api.Account{c}, 35 | Book: &api.Book{c}, 36 | Books: &api.Books{c}, 37 | Builds: &api.Builds{c}, 38 | 39 | Client: c, 40 | } 41 | } 42 | 43 | func (a *API) Fork(opts APIOptions) *API { 44 | forkedClient := a.Client.Fork(client.ClientOptions(opts)) 45 | return NewAPIFromClient(forkedClient) 46 | } 47 | 48 | func (a *API) AuthFork(username, password string) *API { 49 | forkedClient := a.Client.AuthFork(username, password) 50 | return NewAPIFromClient(forkedClient) 51 | } 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | go-gitbook-api 2 | ============== 3 | 4 | GitBook API client in GO (golang) 5 | 6 | ## Documentation 7 | 8 | See [![GoDoc](https://godoc.org/github.com/GitbookIO/go-gitbook-api?status.png)](https://godoc.org/github.com/GitbookIO/go-gitbook-api) 9 | for automatically generated API documentation. 10 | 11 | Check out the **examples** below for quick and simple ways to start. 12 | 13 | ### Simple Example 14 | 15 | ```go 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | "github.com/GitbookIO/go-gitbook-api" 21 | ) 22 | 23 | func main() { 24 | // Make API client 25 | api := gitbook.NewAPI(gitbook.APIOptions{}) 26 | 27 | // Get book 28 | book, err := api.Book.Get("gitbookio/javascript") 29 | 30 | // Print results 31 | fmt.Printf("book = %q\n", book) 32 | fmt.Printf("error = %q\n", err) 33 | } 34 | ``` 35 | 36 | ### Advanced Example 37 | 38 | ```go 39 | package main 40 | 41 | import ( 42 | "fmt" 43 | "github.com/GitbookIO/go-gitbook-api" 44 | ) 45 | 46 | func main() { 47 | // Make API client 48 | api := gitbook.NewAPI(gitbook.APIOptions{ 49 | // Custom host instead of "https://api.gitbook.com" 50 | Host: "http://localhost:5000/api/", 51 | 52 | // Hit API with a specific user 53 | Username: "username", 54 | Password: "token or password", 55 | }) 56 | 57 | // Get book 58 | book, err := api.Book.Get("gitbookio/javascript") 59 | 60 | // Print results 61 | fmt.Printf("book = %q\n", book) 62 | fmt.Printf("error = %q\n", err) 63 | } 64 | ``` 65 | -------------------------------------------------------------------------------- /utils/gitarchive.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "compress/gzip" 5 | "io" 6 | "os/exec" 7 | ) 8 | 9 | // GitTar returns a stream of tar data of the repo 10 | // at a specific ref 11 | func GitTar(dir, ref string) (io.ReadCloser, error) { 12 | return GitArchive(dir, ref, "tar") 13 | } 14 | 15 | // GitZip returns a stream of zip data of the repo 16 | // at a specific ref 17 | func GitZip(dir, ref string) (io.ReadCloser, error) { 18 | return GitArchive(dir, ref, "zip") 19 | } 20 | 21 | // GitArchive returns a stream of archive data of the repo 22 | // at a specific ref, for the specified archive format (if supported) 23 | func GitArchive(dir, ref, format string) (io.ReadCloser, error) { 24 | // Build archive using git-archive 25 | args := []string{"git", "archive", "--format=" + format, ref} 26 | 27 | cmd := exec.Command(args[0], args[1:]...) 28 | // Set directory to repo's 29 | cmd.Dir = dir 30 | 31 | // Get stream 32 | return CmdStream(cmd, nil) 33 | } 34 | 35 | // GitTarGz returns a stream tar.gz data of the repo 36 | func GitTarGz(dir, ref string) (io.ReadCloser, error) { 37 | reader, err := GitTar(dir, ref) 38 | if err != nil { 39 | return nil, err 40 | } 41 | 42 | // Create pipe for compression 43 | pipeReader, pipeWriter := io.Pipe() 44 | 45 | // Compress stuff 46 | gzipWriter := gzip.NewWriter(pipeWriter) 47 | 48 | // Flush data in async 49 | go func() { 50 | // Copy over data 51 | io.Copy(gzipWriter, reader) 52 | // Close writers 53 | gzipWriter.Close() 54 | pipeWriter.Close() 55 | }() 56 | 57 | return pipeReader, nil 58 | } 59 | -------------------------------------------------------------------------------- /utils/cmdstream.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "io/ioutil" 7 | "os/exec" 8 | ) 9 | 10 | // CmdStream executes a command, and returns its stdout as a stream. 11 | // If the command fails to run or doesn't complete successfully, an error 12 | // will be returned, including anything written on stderr. 13 | func CmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, error) { 14 | if input != nil { 15 | stdin, err := cmd.StdinPipe() 16 | if err != nil { 17 | return nil, err 18 | } 19 | // Write stdin if any 20 | go func() { 21 | io.Copy(stdin, input) 22 | stdin.Close() 23 | }() 24 | } 25 | stdout, err := cmd.StdoutPipe() 26 | if err != nil { 27 | return nil, err 28 | } 29 | stderr, err := cmd.StderrPipe() 30 | if err != nil { 31 | return nil, err 32 | } 33 | pipeR, pipeW := io.Pipe() 34 | errChan := make(chan []byte) 35 | // Collect stderr, we will use it in case of an error 36 | go func() { 37 | errText, e := ioutil.ReadAll(stderr) 38 | if e != nil { 39 | errText = []byte("(...couldn't fetch stderr: " + e.Error() + ")") 40 | } 41 | errChan <- errText 42 | }() 43 | // Copy stdout to the returned pipe 44 | go func() { 45 | _, err := io.Copy(pipeW, stdout) 46 | if err != nil { 47 | pipeW.CloseWithError(err) 48 | } 49 | errText := <-errChan 50 | if err := cmd.Wait(); err != nil { 51 | pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errText)) 52 | } else { 53 | pipeW.Close() 54 | } 55 | }() 56 | // Run the command and return the pipe 57 | if err := cmd.Start(); err != nil { 58 | return nil, err 59 | } 60 | return pipeR, nil 61 | } 62 | -------------------------------------------------------------------------------- /api/leak_test.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "net/http" 7 | "os" 8 | "os/exec" 9 | "sync" 10 | "time" 11 | 12 | "testing" 13 | 14 | "github.com/GitbookIO/go-gitbook-api/client" 15 | ) 16 | 17 | func TestLeaks(t *testing.T) { 18 | // Get current count 19 | c1 := openDescriptors() 20 | 21 | // Call in isolated function 22 | func() { 23 | wg := &sync.WaitGroup{} 24 | 25 | // Create one client 26 | c := client.NewClient(client.ClientOptions{ 27 | Host: "http://localhost:5000/api/", 28 | Username: "james", 29 | Password: "730e0de8-ca53-42f9-9ad3-49c547b0cdc0", 30 | }) 31 | 32 | // Do some work 33 | for i := 0; i < 10; i++ { 34 | go func() { 35 | wg.Add(1) 36 | c2 := c.Fork(client.ClientOptions{}) 37 | b := Book{c2} 38 | 39 | _, err := b.Get("james/test") 40 | if err != nil { 41 | t.Error(err) 42 | } 43 | 44 | wg.Done() 45 | }() 46 | 47 | t.Log(c1, "...", openDescriptors()) 48 | } 49 | 50 | wg.Wait() 51 | 52 | time.Sleep(time.Second) 53 | }() 54 | 55 | // Close pooled connections in net/http 56 | if transport, ok := http.DefaultTransport.(*http.Transport); ok { 57 | transport.CloseIdleConnections() 58 | } else { 59 | t.Errorf("Failed to get default transport") 60 | } 61 | 62 | // See how many files are open now 63 | c2 := openDescriptors() 64 | 65 | t.Log(c1, "=>", c2) 66 | 67 | // Check for leak 68 | if c2 > c1 { 69 | t.Errorf("Leak: %d to %d descriptors", c1, c2) 70 | } 71 | } 72 | 73 | func openDescriptors() int { 74 | out, err := lsof(os.Getpid()) 75 | if err != nil { 76 | return 0 77 | } 78 | return bytes.Count(out, []byte("\n")) 79 | } 80 | 81 | func lsof(pid int) ([]byte, error) { 82 | return exec.Command( 83 | "bash", "-c", 84 | fmt.Sprintf("lsof -p '%d'", pid), 85 | ).Output() 86 | } 87 | -------------------------------------------------------------------------------- /streams/streams.go: -------------------------------------------------------------------------------- 1 | package streams 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "os" 7 | "path" 8 | "path/filepath" 9 | "strings" 10 | 11 | "github.com/GitbookIO/go-gitbook-api/utils" 12 | ) 13 | 14 | type StreamFunc func(path string) (io.ReadCloser, error) 15 | 16 | func PickStream(p string) (io.ReadCloser, error) { 17 | basepath := filepath.Base(p) 18 | 19 | if !exists(p) { 20 | return nil, fmt.Errorf("PickStream: Path '%s' does not exist", p) 21 | } 22 | 23 | // Tar.gz 24 | if strings.HasSuffix(basepath, ".tar.gz") || strings.HasSuffix(basepath, ".tgz") { 25 | return File(p) 26 | } 27 | 28 | // Git repo 29 | if isGitDir(p) { 30 | return GitHead(p) 31 | } else if dir := path.Join(p, ".git"); isGitDir(dir) { 32 | return GitHead(dir) 33 | } 34 | 35 | // Standard folder 36 | return Folder(p) 37 | } 38 | 39 | func GitHead(p string) (io.ReadCloser, error) { 40 | return Git(p, "HEAD") 41 | } 42 | 43 | // Git returns an io.ReadCloser of a repo as a tar.gz 44 | func Git(p, ref string) (io.ReadCloser, error) { 45 | return utils.GitTarGz(p, ref) 46 | } 47 | 48 | func GitRef(ref string) StreamFunc { 49 | return func(p string) (io.ReadCloser, error) { 50 | return Git(p, ref) 51 | } 52 | } 53 | 54 | func Folder(p string) (io.ReadCloser, error) { 55 | return utils.TarGzExclude( 56 | p, 57 | 58 | // Excluded files & folders 59 | ".git", 60 | "node_modules", 61 | "bower", 62 | "_book", 63 | "book.pdf", 64 | "book.mobi", 65 | "book.epub", 66 | ) 67 | } 68 | 69 | func File(p string) (io.ReadCloser, error) { 70 | return os.Open(p) 71 | } 72 | 73 | func isGitDir(dirpath string) bool { 74 | return (exists(path.Join(dirpath, "HEAD")) && 75 | exists(path.Join(dirpath, "objects")) && 76 | exists(path.Join(dirpath, "refs"))) 77 | } 78 | 79 | // Does a file exist on disk ? 80 | func exists(path string) bool { 81 | _, err := os.Stat(path) 82 | if err == nil { 83 | return true 84 | } 85 | if os.IsNotExist(err) { 86 | return false 87 | } 88 | return false 89 | } 90 | -------------------------------------------------------------------------------- /models/book_test.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import ( 4 | "encoding/json" 5 | 6 | "testing" 7 | ) 8 | 9 | func TestUnmarshaling(t *testing.T) { 10 | book := Book{} 11 | err := json.Unmarshal([]byte(bookJSON), &book) 12 | 13 | if err != nil { 14 | t.Error(err) 15 | } 16 | } 17 | 18 | const bookJSON string = ` 19 | { 20 | "id":"aaronomullan/simple-book", 21 | "name":"simple-book", 22 | "title":"simple book", 23 | "description":"", 24 | "public":true, 25 | "price":0, 26 | "githubId":"", 27 | "categories":[ 28 | 29 | ], 30 | "cover":{ 31 | "large":"http://localhost:5000/cover/book/aaronomullan/simple-book?build=1416256968809", 32 | "small":"http://localhost:5000/cover/book/aaronomullan/simple-book?build=1416256968809" 33 | }, 34 | "urls":{ 35 | "access":"http://localhost:5000/book/aaronomullan/simple-book", 36 | "homepage":"http://localhost:5000/book/aaronomullan/simple-book", 37 | "read":"http://localhost:5000/read/book/aaronomullan/simple-book", 38 | "reviews":"http://localhost:5000/book/aaronomullan/simple-book/reviews", 39 | "subscribe":"http://localhost:5000/subscribe/book/aaronomullan/simple-book", 40 | "download":{ 41 | "epub":"http://localhost:5000/download/epub/book/aaronomullan/simple-book", 42 | "mobi":"http://localhost:5000/download/mobi/book/aaronomullan/simple-book", 43 | "pdf":"http://localhost:5000/download/pdf/book/aaronomullan/simple-book" 44 | } 45 | }, 46 | "author":{ 47 | "username":"aaronomullan", 48 | "name":"Aaron O'Mullan", 49 | "urls":{ 50 | "profile":"http://localhost:5000/@aaronomullan" 51 | }, 52 | "accounts":{ 53 | "twitter":"AaronOMullan" 54 | } 55 | }, 56 | "license":{ 57 | "id":"nolicense", 58 | "layout":"license", 59 | "permalink":"http://choosealicense.com/licenses/no-license/", 60 | "category":"No License", 61 | "class":"license-types", 62 | "title":"No License", 63 | "description":"You retain all rights and do not permit distribution, reproduction, or derivative works. You may grant some rights in cases where you publish your source code to a site that requires accepting terms of service. For example, publishing code in a public repository on GitHub requires that you allow others to view and fork your code.", 64 | "note":"This option may be subject to the Terms Of Use of the site where you publish your source code.", 65 | "how":"Simply do nothing, though including a copyright notice is recommended.", 66 | "required":[ 67 | "include-copyright" 68 | ], 69 | "permitted":[ 70 | "commercial-use", 71 | "private-use" 72 | ], 73 | "forbidden":[ 74 | "modifications", 75 | "distribution", 76 | "sublicense" 77 | ], 78 | "url":"http://choosealicense.com/licenses/no-license/", 79 | "content":"Copyright [year] [fullname]", 80 | "path":"licenses/no-license.html" 81 | }, 82 | "language":{ 83 | "code":"en", 84 | "name":"English", 85 | "nativeName":"English" 86 | }, 87 | "reviews":{ 88 | "count":0, 89 | "rating":0 90 | }, 91 | "transactions":{ 92 | "count":0, 93 | "donations":false 94 | }, 95 | "dates":{ 96 | "created":"2014-10-13T19:21:03.070Z" 97 | }, 98 | "permissions":{ 99 | "read":true, 100 | "write":true, 101 | "manage":true 102 | } 103 | } 104 | ` 105 | -------------------------------------------------------------------------------- /api/builds.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "fmt" 7 | "io" 8 | "io/ioutil" 9 | "net/http" 10 | "net/textproto" 11 | 12 | "github.com/GitbookIO/go-gitbook-api/client" 13 | "github.com/GitbookIO/go-gitbook-api/models" 14 | "github.com/GitbookIO/go-gitbook-api/streams" 15 | 16 | "mime/multipart" 17 | ) 18 | 19 | type Builds struct { 20 | Client *client.Client 21 | } 22 | 23 | // BuildOptions are optional data passed along when doing a build (e.g: branch, message, author, ...) 24 | type BuildOptions models.Build 25 | 26 | type postStream func(bookId, version, branch string, r io.Reader) error 27 | 28 | // Build should only be used by internal clients, Publish by others 29 | // Build starts a build and will not update the backing git repository 30 | func (b *Builds) Build(bookId, version, path string, opts BuildOptions) error { 31 | return b.doStreamPublish(bookId, version, path, opts, streams.PickStream) 32 | } 33 | 34 | // PublishGit packages a git repo as tar.gz and uploads it to gitbook.io 35 | func (b *Builds) BuildGit(bookId, version, path, ref string, opts BuildOptions) error { 36 | return b.doStreamPublish(bookId, version, path, opts, streams.GitRef(ref)) 37 | } 38 | 39 | // PublishFolder packages a folder as tar.gz and uploads it to gitbook.io 40 | func (b *Builds) BuildFolder(bookId, version, path string, opts BuildOptions) error { 41 | return b.doStreamPublish(bookId, version, path, opts, streams.Folder) 42 | } 43 | 44 | // PublishTarGz publishes a book based on a tar.gz file 45 | func (b *Builds) BuildTarGz(bookId, version, path string, opts BuildOptions) error { 46 | return b.doStreamPublish(bookId, version, path, opts, streams.File) 47 | } 48 | 49 | func (b *Builds) doStreamPublish(bookId, version, path string, opts BuildOptions, streamfn streams.StreamFunc) error { 50 | stream, err := streamfn(path) 51 | if err != nil { 52 | return err 53 | } 54 | defer stream.Close() 55 | 56 | return b.PublishBuildStream(bookId, version, stream, opts) 57 | } 58 | 59 | func (b *Builds) PublishBuildStream(bookId, version string, reader io.Reader, opts BuildOptions) error { 60 | return b.publishStream( 61 | fmt.Sprintf("/book/%s/build/%s", bookId, version), 62 | version, 63 | reader, 64 | opts, 65 | ) 66 | } 67 | 68 | // PublishStream 69 | func (b *Builds) publishStream(_url, version string, reader io.Reader, opts BuildOptions) error { 70 | // Build request 71 | req, err := newfileUploadRequest( 72 | b.Client.Url(_url), 73 | opts, 74 | reader, 75 | ) 76 | if err != nil { 77 | return err 78 | } 79 | 80 | uinfo := b.Client.Userinfo 81 | 82 | // Auth 83 | pwd, _ := uinfo.Password() 84 | req.SetBasicAuth(uinfo.Username(), pwd) 85 | 86 | // Execute request 87 | response, err := b.Client.Client.Do(req) 88 | if err != nil { 89 | return err 90 | } 91 | // Close body immediately to avoid leaks 92 | defer response.Body.Close() 93 | 94 | if response.StatusCode >= 400 { 95 | data, _ := ioutil.ReadAll(response.Body) 96 | return fmt.Errorf(string(data[:])) 97 | } 98 | 99 | // Some error to code 100 | if response.StatusCode >= 400 { 101 | errMsg, err := client.DecodeError(response.Body) 102 | if err != nil { 103 | return err 104 | } 105 | return errMsg 106 | } 107 | 108 | return nil 109 | } 110 | 111 | // Creates a new file upload http request with optional extra params 112 | func newfileUploadRequest(uri string, opts BuildOptions, reader io.Reader) (*http.Request, error) { 113 | // Buffer for body 114 | body := &bytes.Buffer{} 115 | // Multipart data 116 | writer := multipart.NewWriter(body) 117 | 118 | // Write JSON metadata 119 | metadataPart, err := writer.CreatePart(textproto.MIMEHeader{ 120 | "Content-Disposition": {"form-data; name=metadata"}, 121 | "Content-Type": {"application/json"}, 122 | }) 123 | if err != nil { 124 | return nil, err 125 | } 126 | metadataPart.Write([]byte(jsonString(opts))) 127 | 128 | // File part 129 | part, err := writer.CreateFormFile("book", "book.tar.gz") 130 | if err != nil { 131 | return nil, err 132 | } 133 | 134 | // Copy over data for file 135 | _, err = io.Copy(part, reader) 136 | if err != nil { 137 | return nil, err 138 | } 139 | 140 | // Close writer 141 | err = writer.Close() 142 | if err != nil { 143 | return nil, err 144 | } 145 | 146 | req, err := http.NewRequest("PUT", uri, body) 147 | if err != nil { 148 | return nil, err 149 | } 150 | 151 | // Set header 152 | req.Header.Set("Content-Type", writer.FormDataContentType()) 153 | 154 | return req, nil 155 | } 156 | 157 | func jsonString(v interface{}) string { 158 | if data, err := json.Marshal(v); err == nil { 159 | return string(data) 160 | } 161 | return "" 162 | } 163 | -------------------------------------------------------------------------------- /client/client.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "encoding/json" 5 | "io" 6 | "io/ioutil" 7 | "net/http" 8 | "net/url" 9 | "path" 10 | 11 | "gopkg.in/jmcvetta/napping.v2" 12 | ) 13 | 14 | type Client struct { 15 | *napping.Session 16 | *ClientOptions 17 | } 18 | 19 | type ClientOptions struct { 20 | // Hostname of gitbookio endpoint 21 | Host string 22 | 23 | // Auth info 24 | Username string 25 | Password string 26 | } 27 | 28 | func NewClient(opts ClientOptions) *Client { 29 | // Default hostname 30 | if opts.Host == "" { 31 | opts.Host = "https://api.gitbook.com" 32 | } 33 | 34 | // Setup session 35 | // for authentication and custom headers 36 | session := &napping.Session{ 37 | Userinfo: url.UserPassword(opts.Username, opts.Password), 38 | Header: &http.Header{}, 39 | Client: &http.Client{}, 40 | } 41 | 42 | // We want JSON responses (for errors especially) 43 | session.Header.Set("Accept", "application/json") 44 | 45 | return &Client{ 46 | Session: session, 47 | ClientOptions: &opts, 48 | } 49 | } 50 | 51 | // Fork creates a new client off of the base client 52 | // however it shares the same http.Client for efficiency reasons 53 | // this prevents socket leaks from happening etc ... 54 | func (c *Client) Fork(opts ClientOptions) *Client { 55 | if opts.Host == "" { 56 | opts.Host = c.Host 57 | } 58 | if opts.Username == "" { 59 | opts.Username = c.Username 60 | } 61 | if opts.Password == "" { 62 | opts.Password = c.Password 63 | } 64 | 65 | // Create copy of current headers for child 66 | header := http.Header{} 67 | copyHeader(header, *c.Session.Header) 68 | 69 | session := &napping.Session{ 70 | Userinfo: url.UserPassword(opts.Username, opts.Password), 71 | Header: &header, 72 | Client: c.Session.Client, 73 | } 74 | 75 | return &Client{ 76 | Session: session, 77 | ClientOptions: &opts, 78 | } 79 | } 80 | 81 | // AuthFork is a shorthand of Fork, when you simply want to change the auth 82 | func (c *Client) AuthFork(username, password string) *Client { 83 | return c.Fork(ClientOptions{ 84 | Username: username, 85 | Password: password, 86 | }) 87 | } 88 | 89 | func (c *Client) Delete(url string, result interface{}) (*napping.Response, error) { 90 | return errorPatch(func(errMsg *Error) (*napping.Response, error) { 91 | return c.Session.Delete(c.Url(url), result, errMsg) 92 | }) 93 | } 94 | 95 | func (c *Client) Get(url string, params *url.Values, result interface{}) (*napping.Response, error) { 96 | return errorPatch(func(errMsg *Error) (*napping.Response, error) { 97 | return c.Session.Get(c.Url(url), params, result, errMsg) 98 | }) 99 | } 100 | 101 | func (c *Client) Head(url string, result interface{}) (*napping.Response, error) { 102 | return errorPatch(func(errMsg *Error) (*napping.Response, error) { 103 | return c.Session.Head(c.Url(url), result, errMsg) 104 | }) 105 | } 106 | 107 | func (c *Client) Options(url string, result interface{}) (*napping.Response, error) { 108 | return errorPatch(func(errMsg *Error) (*napping.Response, error) { 109 | return c.Session.Options(c.Url(url), result, errMsg) 110 | }) 111 | } 112 | 113 | func (c *Client) Patch(url string, payload, result interface{}) (*napping.Response, error) { 114 | return errorPatch(func(errMsg *Error) (*napping.Response, error) { 115 | return c.Session.Patch(c.Url(url), payload, result, errMsg) 116 | }) 117 | } 118 | 119 | func (c *Client) Post(url string, payload, result interface{}) (*napping.Response, error) { 120 | return errorPatch(func(errMsg *Error) (*napping.Response, error) { 121 | return c.Session.Post(c.Url(url), payload, result, errMsg) 122 | }) 123 | } 124 | 125 | func (c *Client) Put(url string, payload, result interface{}) (*napping.Response, error) { 126 | return errorPatch(func(errMsg *Error) (*napping.Response, error) { 127 | return c.Session.Put(c.Url(url), payload, result, errMsg) 128 | }) 129 | } 130 | 131 | // Url returns the full http url including host 132 | func (c *Client) Url(urlpath string) string { 133 | // Ignore errors for now 134 | parsed, _ := url.Parse(c.Host) 135 | 136 | // Rewrite path 137 | parsed.Path = path.Join(parsed.Path, urlpath) 138 | 139 | // Return string URL 140 | return parsed.String() 141 | } 142 | 143 | // This is so we include API errors as well as protocol errors here 144 | func errorPatch(f func(err *Error) (*napping.Response, error)) (*napping.Response, error) { 145 | errMsg := &Error{} 146 | resp, err := f(errMsg) 147 | // API error 148 | if err == nil && errMsg.Code != 0 { 149 | return resp, errMsg 150 | } 151 | // Normal or protcol error 152 | return resp, err 153 | } 154 | 155 | func DecodeError(reader io.Reader) (*Error, error) { 156 | errMsg := &Error{} 157 | decoder := json.NewDecoder(reader) 158 | err := decoder.Decode(errMsg) 159 | if err != nil { 160 | // Failed to decode, error must be string not JSON 161 | data, err := ioutil.ReadAll(decoder.Buffered()) 162 | if err != nil { 163 | return nil, err 164 | } 165 | return &Error{ 166 | Msg: string(data[:]), 167 | Code: 500, 168 | }, nil 169 | } 170 | return errMsg, nil 171 | } 172 | 173 | // Copied from go's source 174 | func copyHeader(dst, src http.Header) { 175 | for k, vv := range src { 176 | for _, v := range vv { 177 | dst.Add(k, v) 178 | } 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------