Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interceptors #73

Merged
merged 1 commit into from Apr 19, 2016
Merged

Interceptors #73

merged 1 commit into from Apr 19, 2016

Conversation

kittinunf
Copy link
Owner

@kittinunf kittinunf commented Apr 16, 2016

Introducing Interceptor's concept into Fuel. There are two types of interceptors which are RequestInterceptor and ResponseInterceptor.

RequestInterceptor is in type of (Request) -> Request) -> ((Request) -> Request).

Simple usage of RequestInterceptor is cUrlLoggingInterceptor can be simply implemented as such

fun cUrlLoggingRequestInterceptor() =
        { next: (Request) -> Request ->
            { r: Request ->
                println(r.cUrlString())
                next(r)
            }
        }

And usage will be

//Initialization phase
manager.addRequestInterceptor(cUrlLoggingInterceptor())

manager.request(Method.GET, "https://httpbin.org/get").header("User-Agent", "Fuel").response()

// print
curl -i -H "User-Agent:Fuel" "https://httpbin.org/get"

next is a way to let user customize the behavior of interceptor even more. Without calling next, next interceptor chain will not be called.

For ResponseInterceptor, it is in type of (Request, Response) -> Response -> (Request, Response) -> Response. This makes Redirection to be supported with one type of ResponseInterceptor by checking whether Response type is either 301 or 302. Then, re-request with new URL in location header.

@yoavst yoavst mentioned this pull request Apr 16, 2016
@kittinunf kittinunf merged commit a702a39 into master Apr 19, 2016
@kittinunf kittinunf deleted the interceptors branch April 19, 2016 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant