Introduce RestoreApp(), bump library version
authorMikael Berthe <mikael@lilotux.net>
Mon, 17 Apr 2017 12:07:44 +0200
changeset 131 68ef6504637e
parent 130 c450bb73f59a
child 132 639bbcddb4fe
Introduce RestoreApp(), bump library version RestoreApp() lets the client provide an existing application ID/Secret pair. The library no longer falls back to mastodon.social, an instance name is required. The version is bumped to 0.1 (we have something that works!).
app.go
gondole.go
types.go
--- a/app.go	Mon Apr 17 10:28:10 2017 +0200
+++ b/app.go	Mon Apr 17 12:07:44 2017 +0200
@@ -8,6 +8,7 @@
 package gondole
 
 import (
+	"errors"
 	"net/url"
 	"strings"
 
@@ -20,26 +21,41 @@
 	ClientSecret string `json:"client_secret"`
 }
 
-// NewApp registers a new instance
-func NewApp(name string, scopes []string, redirectURI, instanceURL string) (g *Client, err error) {
-	if instanceURL == "" {
-		instanceURL = defaultInstanceURL
+// buildInstanceURL creates the URL from the instance name or cleans up the
+// provided URL
+func buildInstanceURL(instanceName string) (string, error) {
+	if instanceName == "" {
+		return "", errors.New("no instance provided")
+	}
+
+	instanceURL := instanceName
+	if !strings.Contains(instanceURL, "/") {
+		instanceURL = "https://" + instanceName
 	}
 
-	if !strings.Contains(instanceURL, "://") {
-		instanceURL = "https://" + instanceURL
+	u, err := url.ParseRequestURI(instanceURL)
+	if err != nil {
+		return "", err
 	}
 
-	apiPath := instanceURL + defaultAPIPath
+	u.Path = ""
+	u.RawPath = ""
+	u.RawQuery = ""
+	u.Fragment = ""
+	return u.String(), nil
+}
 
-	if _, err := url.ParseRequestURI(apiPath); err != nil {
+// NewApp registers a new application with a given instance
+func NewApp(name string, scopes []string, redirectURI, instanceName string) (g *Client, err error) {
+	instanceURL, err := buildInstanceURL(instanceName)
+	if err != nil {
 		return nil, err
 	}
 
 	g = &Client{
 		Name:        name,
-		APIBase:     apiPath,
 		InstanceURL: instanceURL,
+		APIBase:     instanceURL + currentAPIPath,
 	}
 
 	params := make(apiCallParams)
@@ -61,3 +77,20 @@
 
 	return
 }
+
+// RestoreApp recreates an application client with existing secrets
+func RestoreApp(name, instanceName, appID, appSecret string, userToken *UserToken) (g *Client, err error) {
+	instanceURL, err := buildInstanceURL(instanceName)
+	if err != nil {
+		return nil, err
+	}
+
+	return &Client{
+		Name:        name,
+		InstanceURL: instanceURL,
+		APIBase:     instanceURL + currentAPIPath,
+		ID:          appID,
+		Secret:      appSecret,
+		UserToken:   userToken,
+	}, nil
+}
--- a/gondole.go	Mon Apr 17 10:28:10 2017 +0200
+++ b/gondole.go	Mon Apr 17 12:07:44 2017 +0200
@@ -16,11 +16,11 @@
 
 const (
 	// GondoleVersion contains the version of the Gondole implementation
-	GondoleVersion = "0.0"
+	GondoleVersion = "0.1"
 
-	defaultInstanceURL = "https://mastodon.social"
-	apiVersion         = "v1" // That is not overridable
-	defaultAPIPath     = "/api/" + apiVersion
+	// API version implemented in this library
+	apiVersion     = "v1"
+	currentAPIPath = "/api/" + apiVersion
 
 	// NoRedirect is the URI for no redirection in the App registration
 	NoRedirect = "urn:ietf:wg:oauth:2.0:oob"
--- a/types.go	Mon Apr 17 10:28:10 2017 +0200
+++ b/types.go	Mon Apr 17 12:07:44 2017 +0200
@@ -13,13 +13,13 @@
 
 // Client contains data for a gondole client application
 type Client struct {
-	Name        string
-	ID          string
-	Secret      string
-	APIBase     string
-	InstanceURL string
+	Name        string // Name of the client
+	ID          string // Application ID
+	Secret      string // Application secret
+	APIBase     string // API prefix URL
+	InstanceURL string // Instance base URL
 
-	UserToken *UserToken
+	UserToken *UserToken // User token
 }
 
 /*