app.go
changeset 83 adc39ae774c0
parent 80 d6e8807818c4
child 84 519be52bfced
equal deleted inserted replaced
81:ba90955d2d56 83:adc39ae774c0
     1 package gondole
     1 package gondole
     2 
     2 
     3 import (
     3 import (
     4 	"encoding/json"
     4 	"encoding/json"
       
     5 	"log"
       
     6 	"net/url"
       
     7 	"strings"
       
     8 
     5 	"github.com/sendgrid/rest"
     9 	"github.com/sendgrid/rest"
     6 	"log"
       
     7 	"strings"
       
     8 )
    10 )
     9 
    11 
    10 var ()
       
    11 
       
    12 type registerApp struct {
    12 type registerApp struct {
    13 	ID           string `json:"id"`
    13 	ID           int    `json:"id"`
    14 	ClientID     string `json:"client_id"`
    14 	ClientID     string `json:"client_id"`
    15 	ClientSecret string `json:"client_secret"`
    15 	ClientSecret string `json:"client_secret"`
    16 }
    16 }
    17 
    17 
    18 // NewApp registers a new instance
    18 // NewApp registers a new instance
    19 func NewApp(name string, scopes []string, redirectURI, baseURL string) (g *Client, err error) {
    19 func NewApp(name string, scopes []string, redirectURI, instanceURL string) (g *Client, err error) {
    20 	var endpoint string
    20 	if instanceURL == "" {
       
    21 		instanceURL = defaultInstanceURL
       
    22 	}
    21 
    23 
    22 	if baseURL != "" {
    24 	if !strings.Contains(instanceURL, "://") {
    23 		endpoint = baseURL
    25 		instanceURL = "https://" + instanceURL
       
    26 	}
       
    27 
       
    28 	apiPath := instanceURL + defaultAPIPath
       
    29 
       
    30 	if _, err := url.ParseRequestURI(apiPath); err != nil {
       
    31 		return nil, err
    24 	}
    32 	}
    25 
    33 
    26 	g = &Client{
    34 	g = &Client{
    27 		Name: name,
    35 		Name:    name,
    28 		APIBase: endpoint,
    36 		APIBase: apiPath,
    29 	}
    37 	}
    30 
    38 
    31 	req := g.prepareRequest("apps")
    39 	req := g.prepareRequest("apps")
    32 	if redirectURI != "" {
    40 	if redirectURI != "" {
    33 		req.QueryParams["redirect_uris"] = redirectURI
    41 		req.QueryParams["redirect_uris"] = redirectURI
    52 
    60 
    53 	if err != nil {
    61 	if err != nil {
    54 		log.Fatalf("error: can not write token for %s", name)
    62 		log.Fatalf("error: can not write token for %s", name)
    55 	}
    63 	}
    56 
    64 
    57 	g = &Client{
    65 	g.ID = resp.ClientID
    58 		Name:    name,
    66 	g.Secret = resp.ClientSecret
    59 		ID:      resp.ClientID,
       
    60 		Secret:  resp.ClientSecret,
       
    61 		APIBase: endpoint,
       
    62 	}
       
    63 
    67 
    64 	return
    68 	return
    65 }
    69 }