2016-10-05 22:47:54 +00:00
|
|
|
package writeas
|
|
|
|
|
2021-11-15 15:30:35 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2023-03-16 18:25:51 +00:00
|
|
|
"net/http"
|
2021-11-15 15:30:35 +00:00
|
|
|
"time"
|
|
|
|
)
|
2016-10-05 22:47:54 +00:00
|
|
|
|
|
|
|
type (
|
|
|
|
// AuthUser represents a just-authenticated user. It contains information
|
|
|
|
// that'll only be returned once (now) per user session.
|
|
|
|
AuthUser struct {
|
|
|
|
AccessToken string `json:"access_token,omitempty"`
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
User *User `json:"user"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// User represents a registered Write.as user.
|
|
|
|
User struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Created time.Time `json:"created"`
|
2018-02-15 20:14:37 +00:00
|
|
|
|
|
|
|
// Optional properties
|
|
|
|
Subscription *UserSubscription `json:"subscription"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserSubscription contains information about a user's Write.as
|
|
|
|
// subscription.
|
|
|
|
UserSubscription struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Begin time.Time `json:"begin"`
|
|
|
|
End time.Time `json:"end"`
|
|
|
|
AutoRenew bool `json:"auto_renew"`
|
|
|
|
Active bool `json:"is_active"`
|
|
|
|
Delinquent bool `json:"is_delinquent"`
|
2016-10-05 22:47:54 +00:00
|
|
|
}
|
|
|
|
)
|
2021-11-15 15:30:35 +00:00
|
|
|
|
|
|
|
// GetMe retrieves the authenticated User's information.
|
|
|
|
// See: https://developers.write.as/docs/api/#retrieve-authenticated-user
|
|
|
|
func (c *Client) GetMe(verbose bool) (*User, error) {
|
|
|
|
if c.Token() == "" {
|
|
|
|
return nil, fmt.Errorf("Unable to get user; no access token given.")
|
|
|
|
}
|
|
|
|
|
|
|
|
params := ""
|
|
|
|
if verbose {
|
|
|
|
params = "?verbose=true"
|
|
|
|
}
|
|
|
|
env, err := c.get("/me"+params, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-16 18:25:51 +00:00
|
|
|
status := env.Code
|
|
|
|
if status == http.StatusUnauthorized {
|
|
|
|
return nil, fmt.Errorf("invalid or expired token")
|
|
|
|
}
|
|
|
|
|
2021-11-15 15:30:35 +00:00
|
|
|
var u *User
|
|
|
|
var ok bool
|
|
|
|
if u, ok = env.Data.(*User); !ok {
|
|
|
|
return nil, fmt.Errorf("Wrong data returned from API.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|