--- a/app.go Wed Apr 12 14:27:09 2017 +0200
+++ b/app.go Wed Apr 12 16:40:02 2017 +0200
@@ -2,30 +2,38 @@
import (
"encoding/json"
+ "log"
+ "net/url"
+ "strings"
+
"github.com/sendgrid/rest"
- "log"
- "strings"
)
-var ()
-
type registerApp struct {
- ID string `json:"id"`
+ ID int `json:"id"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}
// NewApp registers a new instance
-func NewApp(name string, scopes []string, redirectURI, baseURL string) (g *Client, err error) {
- var endpoint string
+func NewApp(name string, scopes []string, redirectURI, instanceURL string) (g *Client, err error) {
+ if instanceURL == "" {
+ instanceURL = defaultInstanceURL
+ }
- if baseURL != "" {
- endpoint = baseURL
+ if !strings.Contains(instanceURL, "://") {
+ instanceURL = "https://" + instanceURL
+ }
+
+ apiPath := instanceURL + defaultAPIPath
+
+ if _, err := url.ParseRequestURI(apiPath); err != nil {
+ return nil, err
}
g = &Client{
- Name: name,
- APIBase: endpoint,
+ Name: name,
+ APIBase: apiPath,
}
req := g.prepareRequest("apps")
@@ -54,12 +62,8 @@
log.Fatalf("error: can not write token for %s", name)
}
- g = &Client{
- Name: name,
- ID: resp.ClientID,
- Secret: resp.ClientSecret,
- APIBase: endpoint,
- }
+ g.ID = resp.ClientID
+ g.Secret = resp.ClientSecret
return
}