gondole.go
author Mikael Berthe <mikael@lilotux.net>
Thu, 13 Apr 2017 09:30:47 +0200
changeset 99 6ec2a44a1bd1
parent 89 8a7a33bec6e1
child 101 16d7500c19dd
permissions -rw-r--r--
Return error rather than empty entities

package gondole

import (
	"errors"
	"fmt"

	"github.com/sendgrid/rest"
)

const (
	// GondoleVersion contains the version of the Gondole implementation
	GondoleVersion = "0.0"

	defaultInstanceURL = "https://mastodon.social"
	apiVersion         = "v1" // That is not overridable
	defaultAPIPath     = "/api/" + apiVersion

	// NoRedirect is the URI for no redirection in the App registration
	NoRedirect = "urn:ietf:wg:oauth:2.0:oob"
)

// Error codes
var (
	ErrAlreadyRegistered = errors.New("app already registered")
	ErrEntityNotFound    = errors.New("entity not found")
)

// prepareRequest insert all pre-defined stuff
func (g *Client) prepareRequest(what string) (req rest.Request) {
	var endPoint string

	endPoint = g.APIBase + "/" + 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", GondoleVersion)
	if g.userToken != nil {
		hdrs["Authorization"] = fmt.Sprintf("Bearer %s", g.userToken.Access_token)
	}

	req = rest.Request{
		BaseURL:     endPoint,
		Headers:     hdrs,
		QueryParams: opts,
	}
	return
}