Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions qiita/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (

// Access token for Qiita API v2
type AccessToken struct {
ClientId string `json:"client_id"`
ClientID string `json:"client_id"`
Scopes []string `json:"scopes"`
Token string `json:"token"`
}

type Auth struct {
ClientId string `json:"client_id"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Code string `json:"code"`
}
Expand All @@ -28,8 +28,11 @@ type Auth struct {
POST /api/v2/access_tokens
*/
func (c *Client) CreateAccessToken(ctx context.Context, auth Auth) error {
b, _ := json.Marshal(auth)
res, err := c.post(ctx, "/api/v2/access_tokens", bytes.NewBuffer(b))
b, err := json.Marshal(auth)
if err != nil {
return err
}
res, err := c.post(ctx, "access_tokens", bytes.NewBuffer(b))
if err != nil {
return err
}
Expand All @@ -45,7 +48,7 @@ func (c *Client) CreateAccessToken(ctx context.Context, auth Auth) error {
Deactivate an access token.
*/
func (c *Client) DeleteAccessToken(ctx context.Context, accessToken string) error {
p := fmt.Sprintf("/api/v2/access_tokens/%s", accessToken)
p := fmt.Sprintf("access_tokens/%s", accessToken)
res, err := c.delete(ctx, p)
if err != nil {
return err
Expand Down
28 changes: 14 additions & 14 deletions qiita/access_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,62 @@ import (
"testing"
)

func TestCreateAccessToken(t *testing.T) {
// 201
func() {
func TestClient_CreateAccessToken(t *testing.T) {
t.Run("200", func(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
http.ServeFile(w, r, "")
}))
defer server.Close()
c, _ := mockClient(server)
ctx := context.TODO()
err := c.CreateAccessToken(ctx, Auth{})
if err != nil {
t.Fatal(err)
}
}()
})

// 400
func() {
t.Run("400", func(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
http.ServeFile(w, r, "")
}))
defer server.Close()
c, _ := mockClient(server)
ctx := context.TODO()
err := c.CreateAccessToken(ctx, Auth{})
if err == nil {
t.Fail()
}
}()
})
}

func TestDeleteAccessToken(t *testing.T) {
// 204
func() {
func TestClient_DeleteAccessToken(t *testing.T) {
t.Run("204", func(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
http.ServeFile(w, r, "")
}))
defer server.Close()
c, _ := mockClient(server)
ctx := context.TODO()
err := c.DeleteAccessToken(ctx, "")
if err != nil {
t.Fatal(err)
}
}()
})

// 400
func() {
t.Run("400", func(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
http.ServeFile(w, r, "")
}))
defer server.Close()
c, _ := mockClient(server)
ctx := context.TODO()
err := c.DeleteAccessToken(ctx, "")
if err == nil {
t.Fail()
}
}()
})
}
2 changes: 1 addition & 1 deletion qiita/authenticated_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type AuthenticatedUser struct {
GET /api/v2/authenticated_user
*/
func (c *Client) GetAuthenticatedUser(ctx context.Context) (*AuthenticatedUser, error) {
res, err := c.get(ctx, "/api/v2/authenticated_user", nil)
res, err := c.get(ctx, "authenticated_user", nil)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions qiita/authenticated_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@ import (
"testing"
)

func TestGetAuthenticatedUser(t *testing.T) {
// 200
func() {
func TestClient_GetAuthenticatedUser(t *testing.T) {
t.Run("200", func(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
http.ServeFile(w, r, "testdata/get_authenticated_user.json")
}))
defer server.Close()
c, _ := mockClient(server)
ctx := context.TODO()
_, err := c.GetAuthenticatedUser(ctx)
if err != nil {
t.Fatal(err)
}
}()
})

// 400
func() {
t.Run("400", func(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
http.ServeFile(w, r, "testdata/get_authenticated_user.json")
}))
defer server.Close()
c, _ := mockClient(server)
ctx := context.TODO()
_, err := c.GetAuthenticatedUser(ctx)
if err == nil {
t.Fail()
}
}()
})
}
39 changes: 26 additions & 13 deletions qiita/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ func decodeBody(res *http.Response, out interface{}) error {
return decoder.Decode(out)
}

func rawQuery(query map[string]interface{}) *string {
values := url.Values{}
for k, v := range query {
value := fmt.Sprint(v)
if value != "" {
values.Add(k, value)
}
}
q := values.Encode()
return &q
}

func (c *Client) url(endpoint string) string {
u := *c.URL
u.Path = path.Join(c.URL.Path, endpoint)
Expand All @@ -63,42 +75,43 @@ func (c *Client) do(ctx context.Context, req *http.Request) (*http.Response, err
return c.HTTPClient.Do(req)
}

func (c *Client) get(ctx context.Context, endpoint string, rawQuery *string) (*http.Response, error) {
func (c *Client) get(ctx context.Context, endpoint string, q map[string]interface{}) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, c.url(endpoint), nil)
if rawQuery != nil {
req.URL.RawQuery = *rawQuery
}
if err != nil {
return nil, err
}
query := rawQuery(q)
if query != nil {
req.URL.RawQuery = *query
}
return c.do(ctx, req)
}

func (client *Client) post(ctx context.Context, endpoint string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPost, client.url(endpoint), body)
func (c *Client) post(ctx context.Context, endpoint string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPost, c.url(endpoint), body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
return client.do(ctx, req)
return c.do(ctx, req)
}

func (client *Client) patch(ctx context.Context, endpoint string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPatch, client.url(endpoint), body)
func (c *Client) patch(ctx context.Context, endpoint string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPatch, c.url(endpoint), body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
return client.do(ctx, req)
return c.do(ctx, req)
}

func (client *Client) put(ctx context.Context, endpoint string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPut, client.url(endpoint), body)
func (c *Client) put(ctx context.Context, endpoint string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPut, c.url(endpoint), body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
return client.do(ctx, req)
return c.do(ctx, req)
}

func (c *Client) delete(ctx context.Context, endpoint string) (*http.Response, error) {
Expand Down
30 changes: 18 additions & 12 deletions qiita/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type Comment struct {
Body string `json:"body"`
CreatedAt string `json:"created_at,omitempty"`
Id string `json:"id,omitempty"`
ID string `json:"id,omitempty"`
RenderedBody string `json:"rendered_body,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
User User `json:"user,omitempty"`
Expand All @@ -26,8 +26,8 @@ type Comments []Comment

DELETE /api/v2/comments/:comment_id
*/
func (c *Client) DeleteComment(ctx context.Context, commentId string) error {
p := fmt.Sprintf("/api/v2/comments/%s", commentId)
func (c *Client) DeleteComment(ctx context.Context, commentID string) error {
p := fmt.Sprintf("comments/%s", commentID)
res, err := c.delete(ctx, p)
if err != nil {
return err
Expand All @@ -43,8 +43,8 @@ func (c *Client) DeleteComment(ctx context.Context, commentId string) error {

GET /api/v2/comments/:comment_id
*/
func (c *Client) GetComment(ctx context.Context, commentId string) (*Comment, error) {
p := fmt.Sprintf("/api/v2/comments/%s", commentId)
func (c *Client) GetComment(ctx context.Context, commentID string) (*Comment, error) {
p := fmt.Sprintf("comments/%s", commentID)
res, err := c.get(ctx, p, nil)
if err != nil {
return nil, err
Expand All @@ -65,8 +65,11 @@ func (c *Client) GetComment(ctx context.Context, commentId string) (*Comment, er
PATCH /api/v2/comments/:comment_id
*/
func (c *Client) UpdateComment(ctx context.Context, comment Comment) error {
b, _ := json.Marshal(comment)
p := fmt.Sprintf("/api/v2/comments/%s", comment.Id)
b, err := json.Marshal(comment)
if err != nil {
return err
}
p := fmt.Sprintf("comments/%s", comment.ID)
res, err := c.patch(ctx, p, bytes.NewBuffer(b))
if err != nil {
return err
Expand All @@ -82,8 +85,8 @@ func (c *Client) UpdateComment(ctx context.Context, comment Comment) error {

List comments on an item in newest order.
*/
func (c *Client) ListComments(ctx context.Context, itemId string) (*Comments, error) {
p := fmt.Sprintf("/api/v2/items/%s/comments", itemId)
func (c *Client) ListComments(ctx context.Context, itemID string) (*Comments, error) {
p := fmt.Sprintf("items/%s/comments", itemID)
res, err := c.get(ctx, p, nil)
if err != nil {
return nil, err
Expand All @@ -103,9 +106,12 @@ func (c *Client) ListComments(ctx context.Context, itemId string) (*Comments, er

POST /api/v2/items/:item_id/comments
*/
func (c *Client) PostComment(ctx context.Context, itemId string, comment Comment) error {
b, _ := json.Marshal(comment)
p := fmt.Sprintf("/api/v2/items/%s/comments", itemId)
func (c *Client) PostComment(ctx context.Context, itemID string, comment Comment) error {
b, err := json.Marshal(comment)
if err != nil {
return err
}
p := fmt.Sprintf("items/%s/comments", itemID)
res, err := c.post(ctx, p, bytes.NewBuffer(b))
if err != nil {
return err
Expand Down
Loading