summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/rest/client.go
blob: 0e37123e1cafcd17e269b278d8effc8f11b034b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//Package rest provides a RocketChat rest client.
package rest

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
)

var (
	ResponseErr = fmt.Errorf("got false response")
)

type Response interface {
	OK() error
}

type Client struct {
	Protocol string
	Host     string
	Path     string
	Port     string
	Version  string

	// Use this switch to see all network communication.
	Debug bool

	auth *authInfo
}

type Status struct {
	Success bool   `json:"success"`
	Error   string `json:"error"`

	Status  string `json:"status"`
	Message string `json:"message"`
}

type authInfo struct {
	token string
	id    string
}

func (s Status) OK() error {
	if s.Success {
		return nil
	}

	if len(s.Error) > 0 {
		return fmt.Errorf(s.Error)
	}

	if s.Status == "success" {
		return nil
	}

	if len(s.Message) > 0 {
		return fmt.Errorf("status: %s, message: %s", s.Status, s.Message)
	}
	return ResponseErr
}

// StatusResponse The base for the most of the json responses
type StatusResponse struct {
	Status
	Channel string `json:"channel"`
}

func NewClient(serverUrl *url.URL, debug bool) *Client {
	protocol := "http"
	port := "80"

	if serverUrl.Scheme == "https" {
		protocol = "https"
		port = "443"
	}

	if len(serverUrl.Port()) > 0 {
		port = serverUrl.Port()
	}

	return &Client{Host: serverUrl.Hostname(), Path: serverUrl.Path, Port: port, Protocol: protocol, Version: "v1", Debug: debug}
}

func (c *Client) getUrl() string {
	if len(c.Version) == 0 {
		c.Version = "v1"
	}
	return fmt.Sprintf("%v://%v:%v%s/api/%s", c.Protocol, c.Host, c.Port, c.Path, c.Version)
}

// Get call Get
func (c *Client) Get(api string, params url.Values, response Response) error {
	return c.doRequest(http.MethodGet, api, params, nil, response)
}

// Post call as JSON
func (c *Client) Post(api string, body io.Reader, response Response) error {
	return c.doRequest(http.MethodPost, api, nil, body, response)
}

// PostForm call as Form Data
func (c *Client) PostForm(api string, params url.Values, response Response) error {
	return c.doRequest(http.MethodPost, api, params, nil, response)
}

func (c *Client) doRequest(method, api string, params url.Values, body io.Reader, response Response) error {
	contentType := "application/x-www-form-urlencoded"
	if method == http.MethodPost {
		if body != nil {
			contentType = "application/json"
		} else if len(params) > 0 {
			body = bytes.NewBufferString(params.Encode())
		}
	}

	request, err := http.NewRequest(method, c.getUrl()+"/"+api, body)
	if err != nil {
		return err
	}

	if method == http.MethodGet {
		if len(params) > 0 {
			request.URL.RawQuery = params.Encode()
		}
	} else {
		request.Header.Set("Content-Type", contentType)
	}

	if c.auth != nil {
		request.Header.Set("X-Auth-Token", c.auth.token)
		request.Header.Set("X-User-Id", c.auth.id)
	}

	if c.Debug {
		log.Println(request)
	}

	resp, err := http.DefaultClient.Do(request)

	if err != nil {
		return err
	}

	defer resp.Body.Close()
	bodyBytes, err := ioutil.ReadAll(resp.Body)

	if c.Debug {
		log.Println(string(bodyBytes))
	}

	var parse bool
	if err == nil {
		if e := json.Unmarshal(bodyBytes, response); e == nil {
			parse = true
		}
	}
	if resp.StatusCode != http.StatusOK {
		if parse {
			return response.OK()
		}
		return errors.New("Request error: " + resp.Status)
	}

	if err != nil {
		return err
	}

	return response.OK()
}