Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new functions #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
19 changes: 17 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package godisco
import (
"bytes"
"fmt"
"github.com/Sirupsen/logrus"
"io/ioutil"
"net/http"
"net/url"
"strings"

"github.com/Sirupsen/logrus"
eminetto marked this conversation as resolved.
Show resolved Hide resolved
)

// Requester interface allowing testing further down the line
type Requester interface {
do(*http.Request) ([]byte, int, error)
Get(string) ([]byte, int, error)
Post(string, []byte) ([]byte, int, error)
Put(string, []byte) ([]byte, int, error)
}

// Client struct to keep track of new client
Expand Down Expand Up @@ -94,3 +94,18 @@ func (c *Client) Post(resource string, data []byte) ([]byte, int, error) {

return c.do(req)
}

// Put to resource string the data provided
func (c *Client) Put(resource string, data []byte) ([]byte, int, error) {
apiAuth := url.Values{}
apiAuth.Set("api_key", c.key)
apiAuth.Add("api_username", c.user)
url := fmt.Sprintf("%s%s?%s", c.domain, resource, apiAuth.Encode())
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(data))
if err != nil {
return nil, 0, err
}
req.Header.Set("Content-Type", "application/json")

return c.do(req)
}
56 changes: 56 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package godisco
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WIP :)


import (
"net/http"
"net/http/httptest"
"testing"
)

type testClient struct {
client *http.Client
domain string
key string
user string
}

func newTestClient() *testClient {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(`OK`))
}))
client := &testClient{
client: server.Client(),
domain: server.URL,
key: "key",
user: "user",
}
return client
}

// Get resource string
func (c *testClient) Get(resource string) ([]byte, int, error) {
return []byte{}, 0, nil
}

func (c *testClient) do(req *http.Request) ([]byte, int, error) {
return []byte{}, 0, nil
}

// Post to resource string the data provided
func (c *testClient) Post(resource string, data []byte) ([]byte, int, error) {
return []byte{}, 0, nil
}

// Put to resource string the data provided
func (c *testClient) Put(resource string, data []byte) ([]byte, int, error) {
return []byte{}, 0, nil
}

func TestPut(t *testing.T) {
client := newTestClient()
_, _, err := client.Put("/resource", []byte{})

if err != nil {
t.Errorf("expected: %v actual: %v", nil, err)
}

}
95 changes: 90 additions & 5 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ type UserResponse struct {
Moderator bool `json:"moderator"`
Admin bool `json:"admin"`
} `json:"users,omitempty"`
User userInfo `json:"user"`
User UserInfo `json:"user"`
Errors []string `json:"errors,omitempty"`
ErrorType string `json:"error_type,omitempty"`
}

type userInfo struct {
//UserInfo user information
type UserInfo struct {
ID int `json:"id"`
Username string `json:"username"`
Avatar string `json:"avatar_template"`
Expand Down Expand Up @@ -88,8 +89,8 @@ type userInfo struct {
Messages bool `json:"has_messages"` // false,
Mentionable bool `json:"mentionable"` // false
} `json:"groups"` //
Featured []string `json:"featured_user_badge_ids"` // [],
Card string `json:"card_badge"` // null
Featured []int64 `json:"featured_user_badge_ids"` // [],
Card string `json:"card_badge"` // null
}

type user struct {
Expand All @@ -98,6 +99,13 @@ type user struct {
Email string `json:"email"`
Password string `json:"password"`
Active bool `json:"active"`
Approved bool `json:"approved"`
}

type invite struct {
Email string `json:"email"`
GroupNames string `json:"group_names"`
CustomMessage string `json:"custom_message"`
}

// CreateResp typical response after user update
Expand All @@ -111,6 +119,12 @@ type CreateResp struct {
Developer bool `json:"is_developer"`
}

// InviteResp typical response after invite created
type InviteResp struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
}

// GetUser sends a request for information about the user
func GetUser(req Requester, user string) (userInfo *UserResponse, err error) {
endpoint := fmt.Sprintf("/users/%s.json", user)
Expand All @@ -123,13 +137,14 @@ func GetUser(req Requester, user string) (userInfo *UserResponse, err error) {
}

// CreateUser creates a new user based on details provided
func CreateUser(req Requester, name string, username string, email string, password string, active bool) (response *CreateResp, err error) {
func CreateUser(req Requester, name string, username string, email string, password string, active bool, approved bool) (response *CreateResp, err error) {
update := &user{
Name: name,
Username: username,
Email: email,
Password: password,
Active: active,
Approved: approved,
}
data, err := json.Marshal(update)
endpoint := "/users"
Expand All @@ -140,3 +155,73 @@ func CreateUser(req Requester, name string, username string, email string, passw
err = json.Unmarshal(body, &response)
return response, err
}

//DeactivateUser user
func DeactivateUser(req Requester, userID int) error {
endpoint := fmt.Sprintf("/admin/users/%d/deactivate.json", userID)
var data []byte
_, _, err := req.Put(endpoint, data)
return err
}

//ActivateUser user
func ActivateUser(req Requester, userID int) error {
endpoint := fmt.Sprintf("/admin/users/%d/activate.json", userID)
var data []byte
_, _, err := req.Put(endpoint, data)
return err
}

// InviteUser invite a new user
func InviteUser(req Requester, email string, message string) (*InviteResp, error) {
i := &invite{
Email: email,
CustomMessage: message,
}
data, err := json.Marshal(i)
endpoint := "/invites"
body, _, err := req.Post(endpoint, data)
if err != nil {
return nil, err
}
type response struct {
Success string `json:"success"`
}
var r response
err = json.Unmarshal(body, &r)
invR := &InviteResp{}
if r.Success == "OK" {
invR.Success = true
}
invR.Message = r.Success
return invR, err
}

// SendPasswordResetEmail Send password reset email
func SendPasswordResetEmail(req Requester, user string) error {
type parameters struct {
Login string `json:"login"`
}
type response struct {
Result string `json:"result"`
UserFound bool `json:"user_found"`
}
p := parameters{
Login: user,
}
data, err := json.Marshal(p)
endpoint := "/session/forgot_password"
body, _, err := req.Post(endpoint, data)
if err != nil {
return err
}
var r response
err = json.Unmarshal(body, &r)
if err != nil {
return err
}
if !r.UserFound {
return fmt.Errorf("User %s not found", user)
}
return err
}
15 changes: 15 additions & 0 deletions user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package godisco
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WIP :)


import (
"testing"
)

func TestDeactivateUser(t *testing.T) {
client := newTestClient()
err := DeactivateUser(client, 1)

if err != nil {
t.Errorf("expected: %v actual: %v", nil, err)
}

}