Add a baseURL parameter to NewApp().
This allow for overriding the URL of the instance you are going to
talk to during the first call to NewApp(). We have a chicken-and-egg
issue otherwise. We need to register before doing anything.
--- a/app.go Tue Apr 11 11:51:47 2017 +0200
+++ b/app.go Tue Apr 11 17:04:40 2017 +0200
@@ -21,7 +21,7 @@
ClientSecret string `json:"client_secret"`
}
-func registerApplication(name string, scopes []string, redirectURI string) (g *Gondole, err error) {
+func registerApplication(name string, scopes []string, redirectURI, baseURL string) (g *Gondole, err error) {
g = &Gondole{
Name: name,
}
@@ -54,6 +54,7 @@
ID: g.ID,
Name: name,
BearerToken: g.Secret,
+ BaseURL: baseURL,
}
err = server.WriteToken(name)
if err != nil {
@@ -63,7 +64,12 @@
}
// NewApp registers a new instance
-func NewApp(name string, scopes []string, redirectURI string) (g *Gondole, err error) {
+func NewApp(name string, scopes []string, redirectURI, baseURL string) (g *Gondole, err error) {
+
+ if baseURL != "" {
+ APIEndpoint = baseURL
+ }
+
// Load configuration, will register if none is found
cnf, err := LoadConfig(name)
if err != nil {
@@ -81,7 +87,7 @@
scopes = ourScopes
}
- g, err = registerApplication(name, scopes, redirectURI)
+ g, err = registerApplication(name, scopes, redirectURI, baseURL)
} else {
g = &Gondole{
--- a/cmd/gondole-cli/main.go Tue Apr 11 11:51:47 2017 +0200
+++ b/cmd/gondole-cli/main.go Tue Apr 11 17:04:40 2017 +0200
@@ -9,13 +9,13 @@
var (
fVerbose bool
+ fBaseURL string
instance *gondole.Gondole
cnf *gondole.Config
)
func Register(c *cli.Context) (err error) {
-
- instance, err = gondole.NewApp("gondole-cli", nil, gondole.NoRedirect)
+ instance, err = gondole.NewApp("gondole-cli", nil, gondole.NoRedirect, fBaseURL)
return err
}
@@ -44,6 +44,11 @@
Usage: "verbose mode",
Destination: &fVerbose,
},
+ cli.StringFlag{
+ Name: "instance,i",
+ Usage: "use that instance",
+ Destination: &fBaseURL,
+ },
}
app.Run(os.Args)
}
--- a/config.go Tue Apr 11 11:51:47 2017 +0200
+++ b/config.go Tue Apr 11 17:04:40 2017 +0200
@@ -36,6 +36,7 @@
ID string
Name string
BearerToken string
+ BaseURL string // This allow for overriding the APIEndpoint on registration
}
type Config struct {
--- a/config_test.go Tue Apr 11 11:51:47 2017 +0200
+++ b/config_test.go Tue Apr 11 17:04:40 2017 +0200
@@ -55,6 +55,7 @@
ID: "666",
Name: "foo",
BearerToken: "d3b07384d113edec49eaa6238ad5ff00",
+ BaseURL: "https://mastodon.social",
}
file = filepath.Join("test", "foo")
s, err := loadInstance(file)
--- a/gondole.go Tue Apr 11 11:51:47 2017 +0200
+++ b/gondole.go Tue Apr 11 17:04:40 2017 +0200
@@ -9,25 +9,35 @@
const (
APIVersion = "0.0"
- APIEndpoint = "https://mastodon.social/api/v1"
+ defAPIEndpoint = "https://mastodon.social/api/v1"
NoRedirect = "urn:ietf:wg:oauth:2.0:oob"
)
var (
+ APIEndpoint string
ErrAlreadyRegistered = errors.New("App already registered")
)
// prepareRequest insert all pre-defined stuff
func (g *Gondole) prepareRequest(what string) (req rest.Request) {
- endPoint := APIEndpoint + fmt.Sprintf("/%s", what)
+ var endPoint string
+ // Allow for overriding for registration
+ if APIEndpoint == "" {
+ endPoint = defAPIEndpoint
+ } else {
+ endPoint = APIEndpoint
+ }
+
+ endPoint = endPoint + fmt.Sprintf("/%s", what)
// Add at least one option, the APIkey if present
hdrs := make(map[string]string)
opts := make(map[string]string)
// Insert our sig
hdrs["User-Agent"] = fmt.Sprintf("Gondole/%s", APIVersion)
+ hdrs["Authorization"] = fmt.Sprintf("Bearer %s", g.Secret)
req = rest.Request{
BaseURL: endPoint,
--- a/gondole_test.go Tue Apr 11 11:51:47 2017 +0200
+++ b/gondole_test.go Tue Apr 11 17:04:40 2017 +0200
@@ -1,5 +1,16 @@
package gondole
import (
+ "testing"
+ "github.com/stretchr/testify/assert"
)
+func TestPrepareRequest(t *testing.T) {
+ g, err := NewApp("foo", nil, NoRedirect)
+ assert.NoError(t, err, "no error")
+
+ req := g.prepareRequest("bar")
+ assert.NotNil(t, req.Headers, "not nil")
+ assert.NotNil(t, req.QueryParams, "not nil")
+}
+
--- a/test/foo.token Tue Apr 11 11:51:47 2017 +0200
+++ b/test/foo.token Tue Apr 11 17:04:40 2017 +0200
@@ -1,3 +1,4 @@
id = "666"
name = "foo"
bearer_token = "d3b07384d113edec49eaa6238ad5ff00"
+baseurl = "https://mastodon.social"
\ No newline at end of file