/
users.go
64 lines (57 loc) · 1.78 KB
/
users.go
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
package http
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/shurcooL/users"
"golang.org/x/net/context/ctxhttp"
)
// Users implements users.Service remotely over HTTP.
type Users struct{}
func (Users) GetAuthenticated(ctx context.Context) (users.User, error) {
resp, err := ctxhttp.Get(ctx, nil, "/api/user")
if err != nil {
return users.User{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return users.User{}, fmt.Errorf("did not get acceptable status code: %v body: %q", resp.Status, body)
}
var u users.User
err = json.NewDecoder(resp.Body).Decode(&u)
return u, err
}
func (Users) GetAuthenticatedSpec(ctx context.Context) (users.UserSpec, error) {
resp, err := ctxhttp.Get(ctx, nil, "/api/userspec")
if err != nil {
return users.UserSpec{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return users.UserSpec{}, fmt.Errorf("did not get acceptable status code: %v body: %q", resp.Status, body)
}
var us users.UserSpec
err = json.NewDecoder(resp.Body).Decode(&us)
return us, err
}
func (Users) Get(ctx context.Context, user users.UserSpec) (users.User, error) {
resp, err := ctxhttp.Get(ctx, nil, fmt.Sprintf("/api/user/%d@%s", user.ID, user.Domain))
if err != nil {
return users.User{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return users.User{}, fmt.Errorf("did not get acceptable status code: %v body: %q", resp.Status, body)
}
var u users.User
err = json.NewDecoder(resp.Body).Decode(&u)
return u, err
}
func (Users) Edit(_ context.Context, er users.EditRequest) (users.User, error) {
return users.User{}, fmt.Errorf("Edit: not implemented")
}