From 2a795f081a1b780357d4809f3b1f0941f473e9b9 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Mon, 21 Dec 2015 04:12:20 -0800 Subject: [PATCH] user - follow --- gogs.go | 4 ++-- user_follow.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 user_follow.go diff --git a/gogs.go b/gogs.go index bd263a1..232e6ab 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.6.0" + return "0.7.0" } // Client represents a Gogs API client. @@ -66,7 +66,7 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re return nil, errors.New("404 Not Found") } - if resp.StatusCode != 200 && resp.StatusCode != 201 { + if resp.StatusCode%100 != 2 { errMap := make(map[string]interface{}) if err = json.Unmarshal(data, &errMap); err != nil { return nil, err diff --git a/user_follow.go b/user_follow.go new file mode 100644 index 0000000..36cc65d --- /dev/null +++ b/user_follow.go @@ -0,0 +1,47 @@ +// Copyright 2015 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package gogs + +import "fmt" + +func (c *Client) ListMyFollowers(page int) ([]*User, error) { + users := make([]*User, 0, 10) + return users, c.getParsedResponse("GET", fmt.Sprintf("/user/followers?page=%d", page), nil, nil, &users) +} + +func (c *Client) ListFollowers(user string, page int) ([]*User, error) { + users := make([]*User, 0, 10) + return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/followers?page=%d", user, page), nil, nil, &users) +} + +func (c *Client) ListMyFollowing(page int) ([]*User, error) { + users := make([]*User, 0, 10) + return users, c.getParsedResponse("GET", fmt.Sprintf("/user/following?page=%d", page), nil, nil, &users) +} + +func (c *Client) ListFollowing(user string, page int) ([]*User, error) { + users := make([]*User, 0, 10) + return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/following?page=%d", user, page), nil, nil, &users) +} + +func (c *Client) IsFollowing(target string) bool { + _, err := c.getResponse("GET", fmt.Sprintf("/user/following/%s", target), nil, nil) + return err == nil +} + +func (c *Client) IsUserFollowing(user, target string) bool { + _, err := c.getResponse("GET", fmt.Sprintf("/users/%s/following/%s", user, target), nil, nil) + return err == nil +} + +func (c *Client) Follow(target string) error { + _, err := c.getResponse("PUT", fmt.Sprintf("/user/following/%s", target), nil, nil) + return err +} + +func (c *Client) Unfollow(target string) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/user/following/%s", target), nil, nil) + return err +}