Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNon200NameApiResponse occurs when we receive a status code that is not 200 or 429. ErrNon200NameApiResponse = errors.New("general error in names API response") ErrNamesApiTooManyRequests = errors.New("too many requests to names API") )
var ( ErrNamesChanUninitialized = errors.New("the server's names channel is uninitialized, please submit an issue") ErrNoNamesAvailable = errors.New("the server has no names to provide") )
var ErrUnsuccessfulJokeQuery = errors.New("general error getting new joke")
Functions ¶
Types ¶
type BudgetNameReq ¶
type BudgetNameReq struct { // MinDiff is the minimum amount of time between now and the oldest names API request allowed before // we are "over budget", after which we cannot make any more requests. // // When creating a budget where x operations can be run in y time, this should be set as the y value. MinDiff time.Duration // NameClient is used to request new names. NameClient NameRequester // NameChan is populated with the results of each names API request. NameChan chan Name // contains filtered or unexported fields }
TODO: this implementation is fairly specific to the problem it solves. We could break this out to be a more general TODO: budget executor, but probably not necessary until we have to use this pattern in more than one place. BudgetNameReq is a budgeted names API requester which will make no more requests than the external API will tolerate.
func (*BudgetNameReq) RequestOften ¶
func (b *BudgetNameReq) RequestOften()
RequestOften gets new names from the names API and pushes them to the names channel, as often as possible. If the timestamp of the oldest call is more than MinDiff then we wait until we are with the budget to make the next call.
type Joke ¶
type Joke struct { Type string `json:"type"` Value struct { ID int `json:"id"` Joke string `json:"joke"` } `json:"value"` }
Joke maps to the Internet Chuck Norris database API response.
func (*Joke) Successful ¶
Successful returns true when a populated Joke response was successful.
type JokeClient ¶
type JokeClient struct { // ApiUrl is the base URL of the jokes API to query ApiUrl url.URL // HttpClient is a http client which can be reused across multiple requests. HttpClient *http.Client }
JokeClient can request jokes from a joke server.
func NewJokeClient ¶
func NewJokeClient(baseUrl url.URL) *JokeClient
NewJokeClient creates a JokeClient with default values where baseUrl is the API URL without any parameters.
func (*JokeClient) JokeWithCustomName ¶
func (c *JokeClient) JokeWithCustomName(fName, lName string) (string, error)
JokeWithCustomName gets a new joke using the first and last name passed in.
type NameClient ¶
type NameClient struct { // ApiUrl is the full URL of the names server from which we can request new names. ApiUrl url.URL // HttpClient is a http client which can be reused across multiple requests. HttpClient *http.Client // contains filtered or unexported fields }
NameClient can request random names from a names server.
func NewNameClient ¶
func NewNameClient(baseUrl url.URL) *NameClient
NewNameClient creates a NameClient with default values where baseUrl is the API URL to query.
func (*NameClient) Names ¶
func (c *NameClient) Names() ([]Name, error)
Names gets several names from the names API. Names is intelligent as it relates to the restrictions of the name API and will short circuit if too many requests are made. If Names is called more often than the API will allow an ErrTooManyNameRequests error will be returned.
type NameRequester ¶
type Server ¶
type Server struct { // Port is the port where the server will listen. Port int32 // JokeClient requests new jokes using a customized name, if given. JokeClient *JokeClient // Names is a buffered channel where names will be retrieved from. The server expects for this // to be populated ahead of time by another thread. We are basically using this as a queue, but the // implementation is more simple and more easily supports handling timeouts. Names chan Name }
func (*Server) GetCustomJoke ¶
func (s *Server) GetCustomJoke(w http.ResponseWriter, req *http.Request)