route/proto/route/client.go

56 lines
1.2 KiB
Go

// Package route is a higher level client for routed suitable to embed into
// Go programs.
package route
import (
"net/http"
"git.xeserv.us/xena/route/proto"
)
// New creates a new instance of the routed client with a given token and service
// URL.
func New(routedURL, token string, underlying *http.Client) *Client {
c := &Client{
underlying: underlying,
authToken: token,
serverURL: routedURL,
}
c.Backends = proto.NewBackendsProtobufClient(routedURL, c.hClient())
c.Routes = proto.NewRoutesProtobufClient(routedURL, c.hClient())
c.Tokens = proto.NewTokensProtobufClient(routedURL, c.hClient())
return c
}
// Client is a higher level client for routed
type Client struct {
Backends proto.Backends
Routes proto.Routes
Tokens proto.Tokens
underlying *http.Client
authToken string
serverURL string
}
// RoundTrip executes a HTTP request, adding authentication headers and then
// executing it using the underlying http client.
func (c *Client) RoundTrip(r *http.Request) (*http.Response, error) {
ck := &http.Cookie{
Name: "routed",
Value: c.authToken,
}
r.AddCookie(ck)
return c.underlying.Do(r)
}
func (c *Client) hClient() *http.Client {
return &http.Client{
Transport: c,
}
}