# HG changeset patch # User Ollivier Robert # Date 1491923080 -7200 # Node ID 1ff7afce37fe302d4647b6c0652f6b85fbd8d580 # Parent 965586c1e3ede224d24073ae3407787dcebb1bea 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. diff -r 965586c1e3ed -r 1ff7afce37fe app.go --- 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{ diff -r 965586c1e3ed -r 1ff7afce37fe cmd/gondole-cli/main.go --- 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) } diff -r 965586c1e3ed -r 1ff7afce37fe config.go --- 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 { diff -r 965586c1e3ed -r 1ff7afce37fe config_test.go --- 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) diff -r 965586c1e3ed -r 1ff7afce37fe gondole.go --- 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, diff -r 965586c1e3ed -r 1ff7afce37fe gondole_test.go --- 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") +} + diff -r 965586c1e3ed -r 1ff7afce37fe test/foo.token --- 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