Skip to content

Commit

Permalink
Add user agent to client
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
minodisk committed Feb 2, 2016
1 parent 442cc61 commit 73abc3f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 19 deletions.
28 changes: 15 additions & 13 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/http"
"net/url"
"os"

"github.com/minodisk/qiitactl/info"
)

const (
Expand Down Expand Up @@ -49,9 +51,8 @@ func NewClient(buildURL func(string, string) string) (c Client, err error) {
return
}

func (c Client) process(method string, subDomain string, path string, data interface{}) (respBody []byte, err error) {
func (c Client) Process(method string, subDomain string, path string, data interface{}) (respBody []byte, err error) {
url := c.BuildURL(subDomain, path)
// fmt.Println("->", method, url)

var reqBody io.Reader
if data != nil {
Expand All @@ -65,6 +66,7 @@ func (c Client) process(method string, subDomain string, path string, data inter
if err != nil {
return
}
req.Header.Add("User-Agent", fmt.Sprintf("%s/%s", info.Name, info.Version))
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token))
resp, err := c.httpClient.Do(req)
if err != nil {
Expand All @@ -77,37 +79,37 @@ func (c Client) process(method string, subDomain string, path string, data inter
return
}

if resp.StatusCode/100 != 2 {
e, err := NewError(respBody)
if err != nil {
return nil, err
}
err = e.Error()
return nil, err
if resp.StatusCode/100 == 2 {
return
}

e, err := NewError(respBody)
if err != nil {
return
}
err = e.Error()
return
}

func (c Client) Post(subDomain string, path string, data interface{}) (body []byte, err error) {
body, err = c.process("Post", subDomain, path, data)
body, err = c.Process("Post", subDomain, path, data)
return
}

func (c Client) Get(subDomain string, path string, v *url.Values) (body []byte, err error) {
if v != nil {
path = fmt.Sprintf("%s?%s", path, v.Encode())
}
body, err = c.process("GET", subDomain, path, nil)
body, err = c.Process("GET", subDomain, path, nil)
return
}

func (c Client) Patch(subDomain string, path string, data interface{}) (body []byte, err error) {
body, err = c.process("PATCH", subDomain, path, data)
body, err = c.Process("PATCH", subDomain, path, data)
return
}

func (c Client) Delete(subDomain string, path string, data interface{}) (body []byte, err error) {
body, err = c.process("Delete", subDomain, path, data)
body, err = c.Process("Delete", subDomain, path, data)
return
}
77 changes: 77 additions & 0 deletions api/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package api_test

import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"regexp"
"testing"

"github.com/minodisk/qiitactl/api"
)

var (
server *httptest.Server
client api.Client
rUserAgent = regexp.MustCompile(`qiitactl/\d+\.\d+\.\d+`)
)

func TestMain(m *testing.M) {
mux := http.NewServeMux()
mux.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if auth != "Bearer XXXXXXXXXXXX" {
b, _ := json.Marshal(api.Error{
Type: "unauthorized",
Message: "Unauthorized",
})
w.WriteHeader(401)
w.Write(b)
return
}
ua := r.Header.Get("User-Agent")
if !rUserAgent.MatchString(ua) {
b, _ := json.Marshal(api.Error{
Type: "bad_request",
Message: "Bad Request",
})
w.WriteHeader(400)
w.Write(b)
return
}
})

server = httptest.NewServer(mux)
defer server.Close()

var err error

err = os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
log.Fatal(err)
}

client, err = api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
})
if err != nil {
log.Fatal(err)
}

code := m.Run()

// clean up

os.Exit(code)
}

func TestProcess(t *testing.T) {
_, err := client.Process("OPTIONS", "", "", nil)
if err != nil {
t.Fatal(err)
}
}
7 changes: 7 additions & 0 deletions info/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package info

const (
Name string = "qiitactl"
Version string = "0.1.0"
Author string = "minodisk"
)
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (

"github.com/codegangsta/cli"
"github.com/joho/godotenv"
"github.com/minodisk/qiitactl/info"
)

func main() {
godotenv.Load()

app := cli.NewApp()
app.Name = "qiitactl"
app.Version = Version
app.Author = "minodisk"
app.Name = info.Name
app.Version = info.Version
app.Author = info.Author
app.Usage = "Controls the Qiita posts"

app.Flags = GlobalFlags
Expand Down
3 changes: 0 additions & 3 deletions version.go

This file was deleted.

0 comments on commit 73abc3f

Please sign in to comment.