gondole.go
changeset 83 adc39ae774c0
parent 77 603dd20de7e1
child 87 1b4653f07bfc
equal deleted inserted replaced
81:ba90955d2d56 83:adc39ae774c0
     1 package gondole
     1 package gondole
     2 
     2 
     3 import (
     3 import (
       
     4 	"errors"
       
     5 	"fmt"
       
     6 
     4 	"github.com/sendgrid/rest"
     7 	"github.com/sendgrid/rest"
     5 	"fmt"
       
     6 	"errors"
       
     7 )
     8 )
     8 
     9 
     9 const (
    10 const (
    10 	APIVersion = "0.0"
    11 	// GondoleVersion contains the version of the Gondole implementation
       
    12 	GondoleVersion = "0.0"
    11 
    13 
    12 	// That is not overridable
    14 	defaultInstanceURL = "https://mastodon.social"
    13 	apiURL = "/api/v1"
    15 	apiVersion         = "v1" // That is not overridable
       
    16 	defaultAPIPath     = "/api/" + apiVersion
    14 
    17 
    15 	// Fallback instance
    18 	// NoRedirect is the URI for no redirection in the App registration
    16 	FallBackURL = "https://mastodon.social"
       
    17 
       
    18 	NoRedirect = "urn:ietf:wg:oauth:2.0:oob"
    19 	NoRedirect = "urn:ietf:wg:oauth:2.0:oob"
    19 )
    20 )
    20 
    21 
    21 var (
    22 var (
    22 	APIEndpoint string
       
    23 	ErrAlreadyRegistered = errors.New("App already registered")
    23 	ErrAlreadyRegistered = errors.New("App already registered")
    24 )
    24 )
    25 
    25 
    26 // prepareRequest insert all pre-defined stuff
    26 // prepareRequest insert all pre-defined stuff
    27 func (g *Client) prepareRequest(what string) (req rest.Request) {
    27 func (g *Client) prepareRequest(what string) (req rest.Request) {
    28 	var APIBase string
    28 	var endPoint string
    29 
    29 
    30 	// Allow for overriding for registration
    30 	endPoint = g.APIBase + "/" + what
    31 	if g.APIBase == "" {
       
    32 		APIBase = FallBackURL
       
    33 	} else {
       
    34 		APIBase = g.APIBase
       
    35 	}
       
    36 
       
    37 	APIEndpoint = fmt.Sprintf("%s%s/%", APIBase, apiURL, what)
       
    38 
       
    39 	// Add at least one option, the APIkey if present
    31 	// Add at least one option, the APIkey if present
    40 	hdrs := make(map[string]string)
    32 	hdrs := make(map[string]string)
    41 	opts := make(map[string]string)
    33 	opts := make(map[string]string)
    42 
    34 
    43 	// Insert our sig
    35 	// Insert our sig
    44 	hdrs["User-Agent"] = fmt.Sprintf("Client/%s", APIVersion)
    36 	hdrs["User-Agent"] = fmt.Sprintf("Gondole/%s", GondoleVersion)
    45 	hdrs["Authorization"] = fmt.Sprintf("Bearer %s", g.Secret)
    37 	hdrs["Authorization"] = fmt.Sprintf("Bearer %s", g.Secret)
    46 
    38 
    47 	req = rest.Request{
    39 	req = rest.Request{
    48 		BaseURL:     APIEndpoint,
    40 		BaseURL:     endPoint,
    49 		Headers:     hdrs,
    41 		Headers:     hdrs,
    50 		QueryParams: opts,
    42 		QueryParams: opts,
    51 	}
    43 	}
    52 	return
    44 	return
    53 }
    45 }