client.go 28.9 KB
Newer Older
1
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
=Corey Hulen's avatar
=Corey Hulen committed
2 3 4 5 6 7
// See License.txt for license information.

package model

import (
	"bytes"
=Corey Hulen's avatar
=Corey Hulen committed
8
	l4g "code.google.com/p/log4go"
=Corey Hulen's avatar
=Corey Hulen committed
9 10 11 12 13 14
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strconv"
	"strings"
15
	"time"
=Corey Hulen's avatar
=Corey Hulen committed
16 17 18
)

const (
=Corey Hulen's avatar
=Corey Hulen committed
19 20 21 22 23 24 25 26 27 28 29 30
	HEADER_REQUEST_ID            = "X-Request-ID"
	HEADER_VERSION_ID            = "X-Version-ID"
	HEADER_ETAG_SERVER           = "ETag"
	HEADER_ETAG_CLIENT           = "If-None-Match"
	HEADER_FORWARDED             = "X-Forwarded-For"
	HEADER_REAL_IP               = "X-Real-IP"
	HEADER_FORWARDED_PROTO       = "X-Forwarded-Proto"
	HEADER_TOKEN                 = "token"
	HEADER_BEARER                = "BEARER"
	HEADER_AUTH                  = "Authorization"
	HEADER_MM_SESSION_TOKEN_HASH = "X-MM-TokenHash"
	API_URL_SUFFIX               = "/api/v1"
=Corey Hulen's avatar
=Corey Hulen committed
31 32 33 34 35 36 37 38 39
)

type Result struct {
	RequestId string
	Etag      string
	Data      interface{}
}

type Client struct {
40 41
	Url        string       // The location of the server like "http://localhost:8065"
	ApiUrl     string       // The api location of the server like "http://localhost:8065/api/v1"
=Corey Hulen's avatar
=Corey Hulen committed
42 43
	HttpClient *http.Client // The http client
	AuthToken  string
44
	AuthType   string
=Corey Hulen's avatar
=Corey Hulen committed
45 46 47 48 49
}

// NewClient constructs a new client with convienence methods for talking to
// the server.
func NewClient(url string) *Client {
50
	return &Client{url, url + API_URL_SUFFIX, &http.Client{}, "", ""}
=Corey Hulen's avatar
=Corey Hulen committed
51 52
}

JoramWilander's avatar
JoramWilander committed
53
func (c *Client) DoPost(url, data, contentType string) (*http.Response, *AppError) {
=Corey Hulen's avatar
=Corey Hulen committed
54
	rq, _ := http.NewRequest("POST", c.Url+url, strings.NewReader(data))
55 56 57 58 59 60 61 62 63 64 65 66 67
	rq.Header.Set("Content-Type", contentType)

	if rp, err := c.HttpClient.Do(rq); err != nil {
		return nil, NewAppError(url, "We encountered an error while connecting to the server", err.Error())
	} else if rp.StatusCode >= 300 {
		return nil, AppErrorFromJson(rp.Body)
	} else {
		return rp, nil
	}
}

func (c *Client) DoApiPost(url string, data string) (*http.Response, *AppError) {
	rq, _ := http.NewRequest("POST", c.ApiUrl+url, strings.NewReader(data))
=Corey Hulen's avatar
=Corey Hulen committed
68 69

	if len(c.AuthToken) > 0 {
70
		rq.Header.Set(HEADER_AUTH, c.AuthType+" "+c.AuthToken)
=Corey Hulen's avatar
=Corey Hulen committed
71 72 73 74 75 76 77 78 79 80 81
	}

	if rp, err := c.HttpClient.Do(rq); err != nil {
		return nil, NewAppError(url, "We encountered an error while connecting to the server", err.Error())
	} else if rp.StatusCode >= 300 {
		return nil, AppErrorFromJson(rp.Body)
	} else {
		return rp, nil
	}
}

82 83
func (c *Client) DoApiGet(url string, data string, etag string) (*http.Response, *AppError) {
	rq, _ := http.NewRequest("GET", c.ApiUrl+url, strings.NewReader(data))
=Corey Hulen's avatar
=Corey Hulen committed
84 85 86 87 88 89

	if len(etag) > 0 {
		rq.Header.Set(HEADER_ETAG_CLIENT, etag)
	}

	if len(c.AuthToken) > 0 {
90
		rq.Header.Set(HEADER_AUTH, c.AuthType+" "+c.AuthToken)
=Corey Hulen's avatar
=Corey Hulen committed
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
	}

	if rp, err := c.HttpClient.Do(rq); err != nil {
		return nil, NewAppError(url, "We encountered an error while connecting to the server", err.Error())
	} else if rp.StatusCode == 304 {
		return rp, nil
	} else if rp.StatusCode >= 300 {
		return rp, AppErrorFromJson(rp.Body)
	} else {
		return rp, nil
	}
}

func getCookie(name string, resp *http.Response) *http.Cookie {
	for _, cookie := range resp.Cookies() {
		if cookie.Name == name {
			return cookie
		}
	}

	return nil
}

func (c *Client) Must(result *Result, err *AppError) *Result {
	if err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
116
		l4g.Close()
117
		time.Sleep(time.Second)
=Corey Hulen's avatar
=Corey Hulen committed
118 119 120 121 122 123
		panic(err)
	}

	return result
}

124
func (c *Client) SignupTeam(email string, displayName string) (*Result, *AppError) {
=Corey Hulen's avatar
=Corey Hulen committed
125 126
	m := make(map[string]string)
	m["email"] = email
127
	m["display_name"] = displayName
128
	if r, err := c.DoApiPost("/teams/signup", MapToJson(m)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
129 130 131 132 133 134 135 136
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

func (c *Client) CreateTeamFromSignup(teamSignup *TeamSignup) (*Result, *AppError) {
137
	if r, err := c.DoApiPost("/teams/create_from_signup", teamSignup.ToJson()); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
138 139 140 141 142 143 144 145
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), TeamSignupFromJson(r.Body)}, nil
	}
}

func (c *Client) CreateTeam(team *Team) (*Result, *AppError) {
146
	if r, err := c.DoApiPost("/teams/create", team.ToJson()); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
147 148 149 150 151 152 153
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), TeamFromJson(r.Body)}, nil
	}
}

154 155 156 157 158 159 160 161 162 163
func (c *Client) GetAllTeams() (*Result, *AppError) {
	if r, err := c.DoApiGet("/teams/all", "", ""); err != nil {
		return nil, err
	} else {

		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), TeamMapFromJson(r.Body)}, nil
	}
}

164
func (c *Client) FindTeamByName(name string, allServers bool) (*Result, *AppError) {
=Corey Hulen's avatar
=Corey Hulen committed
165
	m := make(map[string]string)
166
	m["name"] = name
=Corey Hulen's avatar
=Corey Hulen committed
167
	m["all"] = fmt.Sprintf("%v", allServers)
168
	if r, err := c.DoApiPost("/teams/find_team_by_name", MapToJson(m)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
		return nil, err
	} else {
		val := false
		if body, _ := ioutil.ReadAll(r.Body); string(body) == "true" {
			val = true
		}

		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), val}, nil
	}
}

func (c *Client) FindTeams(email string) (*Result, *AppError) {
	m := make(map[string]string)
	m["email"] = email
184
	if r, err := c.DoApiPost("/teams/find_teams", MapToJson(m)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
185 186 187 188
		return nil, err
	} else {

		return &Result{r.Header.Get(HEADER_REQUEST_ID),
189
			r.Header.Get(HEADER_ETAG_SERVER), TeamMapFromJson(r.Body)}, nil
=Corey Hulen's avatar
=Corey Hulen committed
190 191 192 193 194 195
	}
}

func (c *Client) FindTeamsSendEmail(email string) (*Result, *AppError) {
	m := make(map[string]string)
	m["email"] = email
196
	if r, err := c.DoApiPost("/teams/email_teams", MapToJson(m)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
197 198 199 200 201 202 203 204
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), ArrayFromJson(r.Body)}, nil
	}
}

func (c *Client) InviteMembers(invites *Invites) (*Result, *AppError) {
205
	if r, err := c.DoApiPost("/teams/invite_members", invites.ToJson()); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
206 207 208 209 210 211 212
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), InvitesFromJson(r.Body)}, nil
	}
}

213
func (c *Client) UpdateTeamDisplayName(data map[string]string) (*Result, *AppError) {
214
	if r, err := c.DoApiPost("/teams/update_name", MapToJson(data)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
215 216 217 218 219 220 221 222
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

func (c *Client) CreateUser(user *User, hash string) (*Result, *AppError) {
223
	if r, err := c.DoApiPost("/users/create", user.ToJson()); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
224 225 226 227 228 229 230 231
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), UserFromJson(r.Body)}, nil
	}
}

func (c *Client) CreateUserFromSignup(user *User, data string, hash string) (*Result, *AppError) {
232
	if r, err := c.DoApiPost("/users/create?d="+data+"&h="+hash, user.ToJson()); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
233 234 235 236 237 238 239 240
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), UserFromJson(r.Body)}, nil
	}
}

func (c *Client) GetUser(id string, etag string) (*Result, *AppError) {
241
	if r, err := c.DoApiGet("/users/"+id, "", etag); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
242 243 244 245 246 247 248 249
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), UserFromJson(r.Body)}, nil
	}
}

func (c *Client) GetMe(etag string) (*Result, *AppError) {
250
	if r, err := c.DoApiGet("/users/me", "", etag); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
251 252 253 254 255 256 257 258
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), UserFromJson(r.Body)}, nil
	}
}

func (c *Client) GetProfiles(teamId string, etag string) (*Result, *AppError) {
259
	if r, err := c.DoApiGet("/users/profiles/"+teamId, "", etag); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), UserMapFromJson(r.Body)}, nil
	}
}

func (c *Client) LoginById(id string, password string) (*Result, *AppError) {
	m := make(map[string]string)
	m["id"] = id
	m["password"] = password
	return c.login(m)
}

274
func (c *Client) LoginByEmail(name string, email string, password string) (*Result, *AppError) {
=Corey Hulen's avatar
=Corey Hulen committed
275
	m := make(map[string]string)
276
	m["name"] = name
=Corey Hulen's avatar
=Corey Hulen committed
277 278 279 280 281
	m["email"] = email
	m["password"] = password
	return c.login(m)
}

282
func (c *Client) LoginByEmailWithDevice(name string, email string, password string, deviceId string) (*Result, *AppError) {
=Corey Hulen's avatar
=Corey Hulen committed
283
	m := make(map[string]string)
284
	m["name"] = name
=Corey Hulen's avatar
=Corey Hulen committed
285 286 287 288 289 290 291
	m["email"] = email
	m["password"] = password
	m["device_id"] = deviceId
	return c.login(m)
}

func (c *Client) login(m map[string]string) (*Result, *AppError) {
292
	if r, err := c.DoApiPost("/users/login", MapToJson(m)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
293 294 295
		return nil, err
	} else {
		c.AuthToken = r.Header.Get(HEADER_TOKEN)
296
		c.AuthType = HEADER_BEARER
=Corey Hulen's avatar
=Corey Hulen committed
297
		sessionToken := getCookie(SESSION_COOKIE_TOKEN, r)
=Corey Hulen's avatar
=Corey Hulen committed
298

299
		if c.AuthToken != sessionToken.Value {
=Corey Hulen's avatar
=Corey Hulen committed
300 301 302 303 304 305 306 307 308
			NewAppError("/users/login", "Authentication tokens didn't match", "")
		}

		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), UserFromJson(r.Body)}, nil
	}
}

func (c *Client) Logout() (*Result, *AppError) {
309
	if r, err := c.DoApiPost("/users/logout", ""); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
310 311 312
		return nil, err
	} else {
		c.AuthToken = ""
313
		c.AuthType = HEADER_BEARER
=Corey Hulen's avatar
=Corey Hulen committed
314 315 316 317 318 319

		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

320 321 322 323 324 325 326 327 328 329
func (c *Client) SetOAuthToken(token string) {
	c.AuthToken = token
	c.AuthType = HEADER_TOKEN
}

func (c *Client) ClearOAuthToken() {
	c.AuthToken = ""
	c.AuthType = HEADER_BEARER
}

=Corey Hulen's avatar
=Corey Hulen committed
330 331 332 333
func (c *Client) RevokeSession(sessionAltId string) (*Result, *AppError) {
	m := make(map[string]string)
	m["id"] = sessionAltId

334
	if r, err := c.DoApiPost("/users/revoke_session", MapToJson(m)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
335 336 337 338 339 340 341 342
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

func (c *Client) GetSessions(id string) (*Result, *AppError) {
343
	if r, err := c.DoApiGet("/users/"+id+"/sessions", "", ""); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
344 345 346 347 348 349 350 351 352 353 354 355
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), SessionsFromJson(r.Body)}, nil
	}
}

func (c *Client) Command(channelId string, command string, suggest bool) (*Result, *AppError) {
	m := make(map[string]string)
	m["command"] = command
	m["channelId"] = channelId
	m["suggest"] = strconv.FormatBool(suggest)
356
	if r, err := c.DoApiPost("/command", MapToJson(m)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
357 358 359 360 361 362 363 364
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), CommandFromJson(r.Body)}, nil
	}
}

func (c *Client) GetAudits(id string, etag string) (*Result, *AppError) {
365
	if r, err := c.DoApiGet("/users/"+id+"/audits", "", etag); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
366 367 368 369 370 371 372
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), AuditsFromJson(r.Body)}, nil
	}
}

=Corey Hulen's avatar
=Corey Hulen committed
373
func (c *Client) GetLogs() (*Result, *AppError) {
374
	if r, err := c.DoApiGet("/admin/logs", "", ""); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
375 376 377 378 379 380 381
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), ArrayFromJson(r.Body)}, nil
	}
}

382
func (c *Client) GetClientProperties() (*Result, *AppError) {
383
	if r, err := c.DoApiGet("/admin/client_props", "", ""); err != nil {
384 385 386 387 388 389 390
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
func (c *Client) GetConfig() (*Result, *AppError) {
	if r, err := c.DoApiGet("/admin/config", "", ""); err != nil {
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), ConfigFromJson(r.Body)}, nil
	}
}

func (c *Client) SaveConfig(config *Config) (*Result, *AppError) {
	if r, err := c.DoApiPost("/admin/save_config", config.ToJson()); err != nil {
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), ConfigFromJson(r.Body)}, nil
	}
}

=Corey Hulen's avatar
=Corey Hulen committed
409 410 411 412 413 414 415 416 417
func (c *Client) TestEmail(config *Config) (*Result, *AppError) {
	if r, err := c.DoApiPost("/admin/test_email", config.ToJson()); err != nil {
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

=Corey Hulen's avatar
=Corey Hulen committed
418
func (c *Client) CreateChannel(channel *Channel) (*Result, *AppError) {
419
	if r, err := c.DoApiPost("/channels/create", channel.ToJson()); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
420 421 422 423 424 425 426 427
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), ChannelFromJson(r.Body)}, nil
	}
}

func (c *Client) CreateDirectChannel(data map[string]string) (*Result, *AppError) {
428
	if r, err := c.DoApiPost("/channels/create_direct", MapToJson(data)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
429 430 431 432 433 434 435 436
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), ChannelFromJson(r.Body)}, nil
	}
}

func (c *Client) UpdateChannel(channel *Channel) (*Result, *AppError) {
437
	if r, err := c.DoApiPost("/channels/update", channel.ToJson()); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
438 439 440 441 442 443 444 445
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), ChannelFromJson(r.Body)}, nil
	}
}

func (c *Client) UpdateChannelDesc(data map[string]string) (*Result, *AppError) {
446
	if r, err := c.DoApiPost("/channels/update_desc", MapToJson(data)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
447 448 449 450 451 452 453
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), ChannelFromJson(r.Body)}, nil
	}
}

454 455
func (c *Client) UpdateNotifyProps(data map[string]string) (*Result, *AppError) {
	if r, err := c.DoApiPost("/channels/update_notify_props", MapToJson(data)); err != nil {
456
		return nil, err
=Corey Hulen's avatar
=Corey Hulen committed
457 458 459 460 461 462 463
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

func (c *Client) GetChannels(etag string) (*Result, *AppError) {
464
	if r, err := c.DoApiGet("/channels/", "", etag); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
465 466 467 468 469 470 471
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), ChannelListFromJson(r.Body)}, nil
	}
}

472
func (c *Client) GetChannel(id, etag string) (*Result, *AppError) {
473
	if r, err := c.DoApiGet("/channels/"+id+"/", "", etag); err != nil {
474 475 476 477 478 479 480
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), ChannelDataFromJson(r.Body)}, nil
	}
}

=Corey Hulen's avatar
=Corey Hulen committed
481
func (c *Client) GetMoreChannels(etag string) (*Result, *AppError) {
482
	if r, err := c.DoApiGet("/channels/more", "", etag); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
483 484 485 486 487 488 489
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), ChannelListFromJson(r.Body)}, nil
	}
}

490
func (c *Client) GetChannelCounts(etag string) (*Result, *AppError) {
491
	if r, err := c.DoApiGet("/channels/counts", "", etag); err != nil {
492 493 494 495 496 497 498
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), ChannelCountsFromJson(r.Body)}, nil
	}
}

=Corey Hulen's avatar
=Corey Hulen committed
499
func (c *Client) JoinChannel(id string) (*Result, *AppError) {
500
	if r, err := c.DoApiPost("/channels/"+id+"/join", ""); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
501 502 503 504 505 506 507 508
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), nil}, nil
	}
}

func (c *Client) LeaveChannel(id string) (*Result, *AppError) {
509
	if r, err := c.DoApiPost("/channels/"+id+"/leave", ""); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
510 511 512 513 514 515 516 517
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), nil}, nil
	}
}

func (c *Client) DeleteChannel(id string) (*Result, *AppError) {
518
	if r, err := c.DoApiPost("/channels/"+id+"/delete", ""); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
519 520 521 522 523 524 525 526 527 528
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), nil}, nil
	}
}

func (c *Client) AddChannelMember(id, user_id string) (*Result, *AppError) {
	data := make(map[string]string)
	data["user_id"] = user_id
529
	if r, err := c.DoApiPost("/channels/"+id+"/add", MapToJson(data)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
530 531 532 533 534 535 536 537 538 539
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), nil}, nil
	}
}

func (c *Client) RemoveChannelMember(id, user_id string) (*Result, *AppError) {
	data := make(map[string]string)
	data["user_id"] = user_id
540
	if r, err := c.DoApiPost("/channels/"+id+"/remove", MapToJson(data)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
541 542 543 544 545 546 547 548
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), nil}, nil
	}
}

func (c *Client) UpdateLastViewedAt(channelId string) (*Result, *AppError) {
549
	if r, err := c.DoApiPost("/channels/"+channelId+"/update_last_viewed_at", ""); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
550 551 552 553 554 555 556
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), nil}, nil
	}
}

557
func (c *Client) GetChannelExtraInfo(id string, etag string) (*Result, *AppError) {
558
	if r, err := c.DoApiGet("/channels/"+id+"/extra_info", "", etag); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
559 560 561 562 563 564 565 566
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), ChannelExtraFromJson(r.Body)}, nil
	}
}

func (c *Client) CreatePost(post *Post) (*Result, *AppError) {
567
	if r, err := c.DoApiPost("/channels/"+post.ChannelId+"/create", post.ToJson()); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
568 569 570 571 572 573 574 575
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), PostFromJson(r.Body)}, nil
	}
}

func (c *Client) UpdatePost(post *Post) (*Result, *AppError) {
576
	if r, err := c.DoApiPost("/channels/"+post.ChannelId+"/update", post.ToJson()); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
577 578 579 580 581 582 583 584
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), PostFromJson(r.Body)}, nil
	}
}

func (c *Client) GetPosts(channelId string, offset int, limit int, etag string) (*Result, *AppError) {
585
	if r, err := c.DoApiGet(fmt.Sprintf("/channels/%v/posts/%v/%v", channelId, offset, limit), "", etag); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
586 587 588 589 590 591 592
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), PostListFromJson(r.Body)}, nil
	}
}

593
func (c *Client) GetPostsSince(channelId string, time int64) (*Result, *AppError) {
594
	if r, err := c.DoApiGet(fmt.Sprintf("/channels/%v/posts/%v", channelId, time), "", ""); err != nil {
595 596 597 598 599 600 601
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), PostListFromJson(r.Body)}, nil
	}
}

=Corey Hulen's avatar
=Corey Hulen committed
602
func (c *Client) GetPost(channelId string, postId string, etag string) (*Result, *AppError) {
603
	if r, err := c.DoApiGet(fmt.Sprintf("/channels/%v/post/%v", channelId, postId), "", etag); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
604 605 606 607 608 609 610 611
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), PostListFromJson(r.Body)}, nil
	}
}

func (c *Client) DeletePost(channelId string, postId string) (*Result, *AppError) {
612
	if r, err := c.DoApiPost(fmt.Sprintf("/channels/%v/post/%v/delete", channelId, postId), ""); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
613 614 615 616 617 618 619 620
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

func (c *Client) SearchPosts(terms string) (*Result, *AppError) {
621
	if r, err := c.DoApiGet("/posts/search?terms="+url.QueryEscape(terms), "", ""); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
622 623 624 625 626 627 628 629
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), PostListFromJson(r.Body)}, nil
	}
}

func (c *Client) UploadFile(url string, data []byte, contentType string) (*Result, *AppError) {
630
	rq, _ := http.NewRequest("POST", c.ApiUrl+url, bytes.NewReader(data))
=Corey Hulen's avatar
=Corey Hulen committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
	rq.Header.Set("Content-Type", contentType)

	if len(c.AuthToken) > 0 {
		rq.Header.Set(HEADER_AUTH, "BEARER "+c.AuthToken)
	}

	if rp, err := c.HttpClient.Do(rq); err != nil {
		return nil, NewAppError(url, "We encountered an error while connecting to the server", err.Error())
	} else if rp.StatusCode >= 300 {
		return nil, AppErrorFromJson(rp.Body)
	} else {
		return &Result{rp.Header.Get(HEADER_REQUEST_ID),
			rp.Header.Get(HEADER_ETAG_SERVER), FileUploadResponseFromJson(rp.Body)}, nil
	}
}

func (c *Client) GetFile(url string, isFullUrl bool) (*Result, *AppError) {
	var rq *http.Request
	if isFullUrl {
		rq, _ = http.NewRequest("GET", url, nil)
	} else {
652
		rq, _ = http.NewRequest("GET", c.ApiUrl+"/files/get"+url, nil)
=Corey Hulen's avatar
=Corey Hulen committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	}

	if len(c.AuthToken) > 0 {
		rq.Header.Set(HEADER_AUTH, "BEARER "+c.AuthToken)
	}

	if rp, err := c.HttpClient.Do(rq); err != nil {
		return nil, NewAppError(url, "We encountered an error while connecting to the server", err.Error())
	} else if rp.StatusCode >= 300 {
		return nil, AppErrorFromJson(rp.Body)
	} else {
		return &Result{rp.Header.Get(HEADER_REQUEST_ID),
			rp.Header.Get(HEADER_ETAG_SERVER), rp.Body}, nil
	}
}

669 670
func (c *Client) GetFileInfo(url string) (*Result, *AppError) {
	var rq *http.Request
671
	rq, _ = http.NewRequest("GET", c.ApiUrl+"/files/get_info"+url, nil)
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686

	if len(c.AuthToken) > 0 {
		rq.Header.Set(HEADER_AUTH, "BEARER "+c.AuthToken)
	}

	if rp, err := c.HttpClient.Do(rq); err != nil {
		return nil, NewAppError(url, "We encountered an error while connecting to the server", err.Error())
	} else if rp.StatusCode >= 300 {
		return nil, AppErrorFromJson(rp.Body)
	} else {
		return &Result{rp.Header.Get(HEADER_REQUEST_ID),
			rp.Header.Get(HEADER_ETAG_SERVER), MapFromJson(rp.Body)}, nil
	}
}

=Corey Hulen's avatar
=Corey Hulen committed
687
func (c *Client) GetPublicLink(data map[string]string) (*Result, *AppError) {
688
	if r, err := c.DoApiPost("/files/get_public_link", MapToJson(data)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
689 690 691 692 693 694 695 696
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

func (c *Client) UpdateUser(user *User) (*Result, *AppError) {
697
	if r, err := c.DoApiPost("/users/update", user.ToJson()); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
698 699 700 701 702 703 704 705
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), UserFromJson(r.Body)}, nil
	}
}

func (c *Client) UpdateUserRoles(data map[string]string) (*Result, *AppError) {
706
	if r, err := c.DoApiPost("/users/update_roles", MapToJson(data)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
707 708 709 710 711 712 713 714 715 716 717
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), UserFromJson(r.Body)}, nil
	}
}

func (c *Client) UpdateActive(userId string, active bool) (*Result, *AppError) {
	data := make(map[string]string)
	data["user_id"] = userId
	data["active"] = strconv.FormatBool(active)
718
	if r, err := c.DoApiPost("/users/update_active", MapToJson(data)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
719 720 721 722 723 724 725 726
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), UserFromJson(r.Body)}, nil
	}
}

func (c *Client) UpdateUserNotify(data map[string]string) (*Result, *AppError) {
727
	if r, err := c.DoApiPost("/users/update_notify", MapToJson(data)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
728 729 730 731 732 733 734 735 736 737 738 739 740
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), UserFromJson(r.Body)}, nil
	}
}

func (c *Client) UpdateUserPassword(userId, currentPassword, newPassword string) (*Result, *AppError) {
	data := make(map[string]string)
	data["current_password"] = currentPassword
	data["new_password"] = newPassword
	data["user_id"] = userId

741
	if r, err := c.DoApiPost("/users/newpassword", MapToJson(data)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
742 743 744 745 746 747 748 749
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), UserFromJson(r.Body)}, nil
	}
}

func (c *Client) SendPasswordReset(data map[string]string) (*Result, *AppError) {
750
	if r, err := c.DoApiPost("/users/send_password_reset", MapToJson(data)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
751 752 753 754 755 756 757 758
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

func (c *Client) ResetPassword(data map[string]string) (*Result, *AppError) {
759
	if r, err := c.DoApiPost("/users/reset_password", MapToJson(data)); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
760 761 762 763 764 765 766 767
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

func (c *Client) GetStatuses() (*Result, *AppError) {
768
	if r, err := c.DoApiGet("/users/status", "", ""); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
769 770 771 772 773 774 775
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

776
func (c *Client) GetMyTeam(etag string) (*Result, *AppError) {
777
	if r, err := c.DoApiGet("/teams/me", "", etag); err != nil {
778 779 780 781 782 783 784
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), TeamFromJson(r.Body)}, nil
	}
}

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
func (c *Client) RegisterApp(app *OAuthApp) (*Result, *AppError) {
	if r, err := c.DoApiPost("/oauth/register", app.ToJson()); err != nil {
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), OAuthAppFromJson(r.Body)}, nil
	}
}

func (c *Client) AllowOAuth(rspType, clientId, redirect, scope, state string) (*Result, *AppError) {
	if r, err := c.DoApiGet("/oauth/allow?response_type="+rspType+"&client_id="+clientId+"&redirect_uri="+url.QueryEscape(redirect)+"&scope="+scope+"&state="+url.QueryEscape(state), "", ""); err != nil {
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

func (c *Client) GetAccessToken(data url.Values) (*Result, *AppError) {
	if r, err := c.DoPost("/oauth/access_token", data.Encode(), "application/x-www-form-urlencoded"); err != nil {
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), AccessResponseFromJson(r.Body)}, nil
	}
}

JoramWilander's avatar
JoramWilander committed
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
func (c *Client) CreateIncomingWebhook(hook *IncomingWebhook) (*Result, *AppError) {
	if r, err := c.DoApiPost("/hooks/incoming/create", hook.ToJson()); err != nil {
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), IncomingWebhookFromJson(r.Body)}, nil
	}
}

func (c *Client) PostToWebhook(id, payload string) (*Result, *AppError) {
	if r, err := c.DoPost("/hooks/"+id, payload, "application/x-www-form-urlencoded"); err != nil {
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), nil}, nil
	}
}

func (c *Client) DeleteIncomingWebhook(data map[string]string) (*Result, *AppError) {
	if r, err := c.DoApiPost("/hooks/incoming/delete", MapToJson(data)); err != nil {
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

func (c *Client) ListIncomingWebhooks() (*Result, *AppError) {
	if r, err := c.DoApiGet("/hooks/incoming/list", "", ""); err != nil {
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), IncomingWebhookListFromJson(r.Body)}, nil
	}
}

848 849 850 851 852 853 854 855 856
func (c *Client) GetAllPreferences() (*Result, *AppError) {
	if r, err := c.DoApiGet("/preferences/", "", ""); err != nil {
		return nil, err
	} else {
		preferences, _ := PreferencesFromJson(r.Body)
		return &Result{r.Header.Get(HEADER_REQUEST_ID), r.Header.Get(HEADER_ETAG_SERVER), preferences}, nil
	}
}

857 858
func (c *Client) SetPreferences(preferences *Preferences) (*Result, *AppError) {
	if r, err := c.DoApiPost("/preferences/save", preferences.ToJson()); err != nil {
859 860 861
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
862
			r.Header.Get(HEADER_ETAG_SERVER), preferences}, nil
863 864 865
	}
}

hmhealey's avatar
hmhealey committed
866
func (c *Client) GetPreference(category string, name string) (*Result, *AppError) {
867 868
	if r, err := c.DoApiGet("/preferences/"+category+"/"+name, "", ""); err != nil {
		return nil, err
hmhealey's avatar
hmhealey committed
869 870 871 872 873 874 875 876
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID), r.Header.Get(HEADER_ETAG_SERVER), PreferenceFromJson(r.Body)}, nil
	}
}

func (c *Client) GetPreferenceCategory(category string) (*Result, *AppError) {
	if r, err := c.DoApiGet("/preferences/"+category, "", ""); err != nil {
		return nil, err
877
	} else {
878 879
		preferences, _ := PreferencesFromJson(r.Body)
		return &Result{r.Header.Get(HEADER_REQUEST_ID), r.Header.Get(HEADER_ETAG_SERVER), preferences}, nil
880 881 882
	}
}

883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
func (c *Client) CreateOutgoingWebhook(hook *OutgoingWebhook) (*Result, *AppError) {
	if r, err := c.DoApiPost("/hooks/outgoing/create", hook.ToJson()); err != nil {
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), OutgoingWebhookFromJson(r.Body)}, nil
	}
}

func (c *Client) DeleteOutgoingWebhook(data map[string]string) (*Result, *AppError) {
	if r, err := c.DoApiPost("/hooks/outgoing/delete", MapToJson(data)); err != nil {
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
	}
}

func (c *Client) ListOutgoingWebhooks() (*Result, *AppError) {
	if r, err := c.DoApiGet("/hooks/outgoing/list", "", ""); err != nil {
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), OutgoingWebhookListFromJson(r.Body)}, nil
	}
}

func (c *Client) RegenOutgoingWebhookToken(data map[string]string) (*Result, *AppError) {
	if r, err := c.DoApiPost("/hooks/outgoing/regen_token", MapToJson(data)); err != nil {
		return nil, err
	} else {
		return &Result{r.Header.Get(HEADER_REQUEST_ID),
			r.Header.Get(HEADER_ETAG_SERVER), OutgoingWebhookFromJson(r.Body)}, nil
	}
}

=Corey Hulen's avatar
=Corey Hulen committed
919 920
func (c *Client) MockSession(sessionToken string) {
	c.AuthToken = sessionToken
921
	c.AuthType = HEADER_BEARER
=Corey Hulen's avatar
=Corey Hulen committed
922
}