From 106cec241e0199220587ff9121e60a7797d86367 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 14 Apr 2017 09:03:32 +0900 Subject: [PATCH] cosmetic change --- mastodon.go | 114 ++++++++++++++++++++++++++-------------------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/mastodon.go b/mastodon.go index ed445e3..bbd6e9c 100644 --- a/mastodon.go +++ b/mastodon.go @@ -64,6 +64,63 @@ func (c *client) Authenticate(username, password string) error { return nil } +// AppConfig is a setting for registering applications. +type AppConfig struct { + http.Client + Server string + ClientName string + + // Where the user should be redirected after authorization (for no redirect, use urn:ietf:wg:oauth:2.0:oob) + RedirectURIs string + + // This can be a space-separated list of the following items: "read", "write" and "follow". + Scopes string + + // Optional. + Website string +} + +// Application is mastodon application. +type Application struct { + ID int64 `json:"id"` + RedirectURI string `json:"redirect_uri"` + ClientID string `json:"client_id"` + ClientSecret string `json:"client_secret"` +} + +// RegisterApp returns the mastodon application. +func RegisterApp(appConfig *AppConfig) (*Application, error) { + params := url.Values{} + params.Set("client_name", appConfig.ClientName) + params.Set("redirect_uris", appConfig.RedirectURIs) + params.Set("scopes", appConfig.Scopes) + params.Set("website", appConfig.Website) + + url, err := url.Parse(appConfig.Server) + if err != nil { + return nil, err + } + url.Path = path.Join(url.Path, "/api/v1/apps") + + req, err := http.NewRequest("POST", url.String(), strings.NewReader(params.Encode())) + if err != nil { + return nil, err + } + resp, err := appConfig.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + app := &Application{} + err = json.NewDecoder(resp.Body).Decode(app) + if err != nil { + return nil, err + } + + return app, nil +} + type Visibility int64 type Toot struct { @@ -171,60 +228,3 @@ func (c *client) PostStatus(toot *Toot) (*Status, error) { } return &status, nil } - -// AppConfig is a setting for registering applications. -type AppConfig struct { - http.Client - Server string - ClientName string - - // Where the user should be redirected after authorization (for no redirect, use urn:ietf:wg:oauth:2.0:oob) - RedirectURIs string - - // This can be a space-separated list of the following items: "read", "write" and "follow". - Scopes string - - // Optional. - Website string -} - -// Application is mastodon application. -type Application struct { - ID int64 `json:"id"` - RedirectURI string `json:"redirect_uri"` - ClientID string `json:"client_id"` - ClientSecret string `json:"client_secret"` -} - -// RegisterApp returns the mastodon application. -func RegisterApp(appConfig *AppConfig) (*Application, error) { - params := url.Values{} - params.Set("client_name", appConfig.ClientName) - params.Set("redirect_uris", appConfig.RedirectURIs) - params.Set("scopes", appConfig.Scopes) - params.Set("website", appConfig.Website) - - url, err := url.Parse(appConfig.Server) - if err != nil { - return nil, err - } - url.Path = path.Join(url.Path, "/api/v1/apps") - - req, err := http.NewRequest("POST", url.String(), strings.NewReader(params.Encode())) - if err != nil { - return nil, err - } - resp, err := appConfig.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - app := &Application{} - err = json.NewDecoder(resp.Body).Decode(app) - if err != nil { - return nil, err - } - - return app, nil -}