Skip to content
Permalink
Browse files

Add in the client package

  • Loading branch information...
paultag committed Apr 29, 2016
1 parent c1bde81 commit 9e7916de1b8adfcc8cccf53560f06b0aa2f8bf2a
Showing with 71 additions and 0 deletions.
  1. +8 −0 README.md
  2. +31 −0 client/README.md
  3. +28 −0 client/client.go
  4. +4 −0 debian/copyright
@@ -3,3 +3,11 @@ api

This is the backend that runs the [Open Source API](https://api.opensource.org/)
by reading the [machine readable license list](https://github.com/opensourceorg/licenses)


See also
--------

- [client](https://github.com/OpenSourceOrg/api/blob/master/client/README.md)
- [license list](https://github.com/opensourceorg/licenses)
- [api](https://api.opensource.org/)
@@ -0,0 +1,31 @@
Open Source API Client
======================

[godoc](https://godoc.org/github.com/OpenSourceOrg/api/client)

This package is used to connect and query the Open Source API. This uses
the internal data modeling of the API to preform requests over the line.

Example
-------

```go
package main
import (
"github.com/opensourceorg/api/client"
"log"
)
func ohshit(err error) {
if err != nil {
panic(err)
}
}
func main() {
license, err := client.Get("Apache-2.0")
ohshit(err)
log.Printf("%s\n", license.Name)
}
```
@@ -25,6 +25,7 @@ import (

"encoding/json"
"net/http"
"strings"
)

var baseURL = "https://api.opensource.org"
@@ -34,25 +35,52 @@ func createURIString(resource string) string {
return baseURL + "/" + resource
}

type BadRequest struct {
errors []map[string]string
}

func (b BadRequest) messages() []string {
ret := []string{}
for _, err := range b.errors {
ret = append(ret, err["message"])
}
return ret
}

func (b BadRequest) Error() string {
return strings.Join(b.messages(), ", ")
}

func request(uri string, target interface{}) error {
resp, err := http.Get(createURIString(uri))
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode/100 != 2 {
errors := map[string][]map[string]string{}
if err := json.NewDecoder(resp.Body).Decode(&errors); err != nil {
return err
}
return BadRequest{errors: errors["errors"]}
}

if err := json.NewDecoder(resp.Body).Decode(target); err != nil {
return err
}

return nil
}

// Return a list of all known Licenses.
func All() (license.Licenses, error) {
ret := license.Licenses{}
return ret, request("licenses/", &ret)
}

// Return a list of all licenses which contain the keyword that was
// passed in.
func Tagged(keyword string) (license.Licenses, error) {
ret := license.Licenses{}
return ret, request("licenses/"+keyword, &ret)
@@ -10,6 +10,10 @@ Files: license/*
Copyright: 2015-2016 Open Source Initiative
License: Expat

Files: client/*
Copyright: 2015-2016 Open Source Initiative
License: Expat

Files: debian/*
Copyright: 2015 Paul Tagliamonte <paultag@opensource.org>
License: AGPL-3.0

0 comments on commit 9e7916d

Please sign in to comment.
You can’t perform that action at this time.