-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_profile.go
61 lines (51 loc) · 1.92 KB
/
user_profile.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
package pixela
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
)
// A UserProfile manages communication with the Pixela user profile API.
type UserProfile struct {
UserName string
Token string
}
// Update updates the profile information for the user corresponding to username.
func (u *UserProfile) Update(input *UserProfileUpdateInput) (*Result, error) {
return u.UpdateWithContext(context.Background(), input)
}
// UpdateWithContext updates the profile information for the user corresponding to username.
func (u *UserProfile) UpdateWithContext(ctx context.Context, input *UserProfileUpdateInput) (*Result, error) {
param, err := u.createUpdateRequestParameter(input)
if err != nil {
return &Result{}, errors.Wrapf(err, "failed to create user profile update parameter")
}
return doRequestAndParseResponse(ctx, param)
}
// UserProfileUpdateInput is input of UserProfile.Update().
type UserProfileUpdateInput struct {
DisplayName *string `json:"displayName,omitempty"`
GravatarIconEmail *string `json:"gravatarIconEmail,omitempty"`
Title *string `json:"title,omitempty"`
Timezone *string `json:"timezone,omitempty"`
AboutURL *string `json:"aboutURL,omitempty"`
ContributeURLs []string `json:"contributeURLs,omitempty"`
PinnedGraphID *string `json:"pinnedGraphID,omitempty"`
}
func (u *UserProfile) createUpdateRequestParameter(input *UserProfileUpdateInput) (*requestParameter, error) {
b, err := json.Marshal(input)
if err != nil {
return &requestParameter{}, errors.Wrap(err, "failed to marshal json")
}
return &requestParameter{
Method: http.MethodPut,
URL: fmt.Sprintf(APIBaseURL+"/@%s", u.UserName),
Header: map[string]string{userToken: u.Token},
Body: b,
}, nil
}
// URL outputs the profile of the user specified by username in html format.
func (u *UserProfile) URL() string {
return fmt.Sprintf(APIBaseURL+"/@%s", u.UserName)
}