diff -r c450bb73f59a -r 68ef6504637e app.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 +}