Butter is a library that helps ease HTTP requests through a stack of middleware.
Butter is a replacement for web::WebClient and provides an extensible chain of middleware for making HTTP requests and processing the response. The adoption of the Middleware pattern allows you to seamlessly enhance and modify the behaviour of your HTTP requests.
An instance of Butter wraps a stack of Middleware classes. When a HTTP request is made through Butter, each piece of middleware is called in turn. Middleware classes may either pass the request on to the next piece of middleware, or return a response. At each step, the middleware classes have the option of modifying the request and / or response objects.
The ordering of the middleware stack is important.
The last piece of middleware MUST return a response. These middleware classes are called Terminators. The default terminator is the HttpTerminator which makes an actual HTTP request to the interweb. (When testing this could be substituted with a mock terminator that returns set / canned responses.)
To create a Butter instance, call the static Butter.churnOut() method, optionally passing in a custom list of middleware:
Or to use the default stack of middleware bundled with Butter, just churn and go:
html := Butter.churnOut.get(`http://www.fantomfactory.org/`).asStr
Butter Dishes
Because functionality is encapsulated in the middleware, you need to access these classes to configure them. Use the Butter.findMiddleware() method to do this:
As you can see, this code is quite verbose. To combat this, there are two alternative means of getting hold of middleware:
Dynamic Stylie
If you make dynamic invocation method calls on the Butter class, you can retrieve instances of middleware. The dynamic methods have the same simple name as the middleware type. If the type name ends with Middleware, it may be omitted. Example:
Should instances of the same middleware class be in the stack more than once (or should it contain 2 middleware classes with the same name from different pods) then the just first one is returned.
Obviously, dynamic invocation should be used with caution.
Static Stylie
To call the middleware in a statically typed fashion, use a ButterDish class that holds your Butter instance and contains helper methods. There is a default ButterDish class with methods to access middleware in the default stack. Example:
SlimerDude Thu 16 Jan 2014
Butter v0.0.2 Released!
Butter is a library that helps ease HTTP requests through a stack of middleware.
Butter
is a replacement forweb::WebClient
and provides an extensible chain of middleware for making HTTP requests and processing the response. The adoption of the Middleware pattern allows you to seamlessly enhance and modify the behaviour of your HTTP requests.fanr install -r http://repo.status302.com/fanr/ afButter
Quick Start
1). Create a text file called
Example.fan
:2). Run Example.fan as a Fantom script from the command line:
Usage
An instance of
Butter
wraps a stack ofMiddleware
classes. When a HTTP request is made throughButter
, each piece of middleware is called in turn. Middleware classes may either pass the request on to the next piece of middleware, or return a response. At each step, the middleware classes have the option of modifying the request and / or response objects.The ordering of the middleware stack is important.
The last piece of middleware MUST return a response. These middleware classes are called Terminators. The default terminator is the
HttpTerminator
which makes an actual HTTP request to the interweb. (When testing this could be substituted with a mock terminator that returns set / canned responses.)To create a
Butter
instance, call the staticButter.churnOut()
method, optionally passing in a custom list of middleware:Or to use the default stack of middleware bundled with
Butter
, just churn and go:Butter Dishes
Because functionality is encapsulated in the middleware, you need to access these classes to configure them. Use the
Butter.findMiddleware()
method to do this:As you can see, this code is quite verbose. To combat this, there are two alternative means of getting hold of middleware:
Dynamic Stylie
If you make dynamic invocation method calls on the
Butter
class, you can retrieve instances of middleware. The dynamic methods have the same simple name as the middleware type. If the type name ends withMiddleware
, it may be omitted. Example:Should instances of the same middleware class be in the stack more than once (or should it contain 2 middleware classes with the same name from different pods) then the just first one is returned.
Obviously, dynamic invocation should be used with caution.
Static Stylie
To call the middleware in a statically typed fashion, use a
ButterDish
class that holds yourButter
instance and contains helper methods. There is a defaultButterDish
class with methods to access middleware in the default stack. Example:When using other middleware, you are encouraged to create your own
ButterDish
that extends the default one.Have fun!
:)