From a1d54f563d0473024dc0fab24568175fa5778252 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Thu, 13 Apr 2017 21:45:44 +0900 Subject: [PATCH 1/2] Add a application registration --- mastodon.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/mastodon.go b/mastodon.go index 7932408..35264fa 100644 --- a/mastodon.go +++ b/mastodon.go @@ -116,3 +116,70 @@ func (c *client) GetTimeline(path string) ([]Timeline, error) { } return timeline, nil } + +type AppConfig struct { + 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 +} + +type appClient struct { + http.Client + appConfig *AppConfig +} + +// NewAppClient returns a new AppClient. +func NewAppClient(appConfig *AppConfig) *appClient { + return &appClient{ + Client: *http.DefaultClient, + appConfig: appConfig, + } +} + +// App is mastodon app. +type App struct { + ID int `json:"id"` + RedirectURI string `json:"redirect_uri"` + ClientID string `json:"client_id"` + ClientSecret string `json:"client_secret"` +} + +func (c *appClient) RegistrationApp() (*App, error) { + params := url.Values{} + params.Set("client_name", c.appConfig.ClientName) + params.Set("redirect_uris", c.appConfig.RedirectURIs) + params.Set("scopes", c.appConfig.Scopes) + params.Set("website", c.appConfig.Website) + + url, err := url.Parse(c.appConfig.Server) + if err != nil { + return nil, err + } + url.Path = "/api/v1/apps" + + req, err := http.NewRequest("POST", url.String(), strings.NewReader(params.Encode())) + if err != nil { + return nil, err + } + resp, err := c.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + app := &App{} + err = json.NewDecoder(resp.Body).Decode(app) + if err != nil { + return nil, err + } + + return app, nil +} From 45acfc9ca12549651a9f9e0aefe3760fa4edb053 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Fri, 14 Apr 2017 00:12:04 +0900 Subject: [PATCH 2/2] Fix application register func to simple --- mastodon.go | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/mastodon.go b/mastodon.go index 35264fa..7f1c878 100644 --- a/mastodon.go +++ b/mastodon.go @@ -117,7 +117,9 @@ func (c *client) GetTimeline(path string) ([]Timeline, error) { return timeline, nil } +// AppConfig is a setting for registering applications. type AppConfig struct { + http.Client Server string ClientName string @@ -131,35 +133,23 @@ type AppConfig struct { Website string } -type appClient struct { - http.Client - appConfig *AppConfig -} - -// NewAppClient returns a new AppClient. -func NewAppClient(appConfig *AppConfig) *appClient { - return &appClient{ - Client: *http.DefaultClient, - appConfig: appConfig, - } -} - -// App is mastodon app. -type App struct { +// Application is mastodon application. +type Application struct { ID int `json:"id"` RedirectURI string `json:"redirect_uri"` ClientID string `json:"client_id"` ClientSecret string `json:"client_secret"` } -func (c *appClient) RegistrationApp() (*App, error) { +// RegisterApp returns the mastodon application. +func RegisterApp(appConfig *AppConfig) (*Application, error) { params := url.Values{} - params.Set("client_name", c.appConfig.ClientName) - params.Set("redirect_uris", c.appConfig.RedirectURIs) - params.Set("scopes", c.appConfig.Scopes) - params.Set("website", c.appConfig.Website) + 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(c.appConfig.Server) + url, err := url.Parse(appConfig.Server) if err != nil { return nil, err } @@ -169,13 +159,13 @@ func (c *appClient) RegistrationApp() (*App, error) { if err != nil { return nil, err } - resp, err := c.Do(req) + resp, err := appConfig.Do(req) if err != nil { return nil, err } defer resp.Body.Close() - app := &App{} + app := &Application{} err = json.NewDecoder(resp.Body).Decode(app) if err != nil { return nil, err