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

Add functions for setting default custom layers #608

Merged
merged 2 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "HTTP"
uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3"
authors = ["Jacob Quinn", "Sam O'Connor", "contributors: https://github.com/JuliaWeb/HTTP.jl/graphs/contributors"]
version = "0.8.19"
version = "0.8.20"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
14 changes: 9 additions & 5 deletions src/HTTP.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module HTTP

export startwrite, startread, closewrite, closeread, stack, insert, AWS4AuthLayer,
BasicAuthLayer, CanonicalizeLayer, ConnectionPoolLayer, ContentTypeDetectionLayer,
DebugLayer, ExceptionLayer, MessageLayer, RedirectLayer, RetryLayer, StreamLayer,
TimeoutLayer
export startwrite, startread, closewrite, closeread, stack, insert, insert_default!,
remove_default!, AWS4AuthLayer, BasicAuthLayer, CanonicalizeLayer, ConnectionPoolLayer,
ContentTypeDetectionLayer, DebugLayer, ExceptionLayer, MessageLayer, RedirectLayer,
RetryLayer, StreamLayer, TimeoutLayer

const DEBUG_LEVEL = Ref(0)

Expand Down Expand Up @@ -566,7 +566,7 @@ function stack(;redirect=true,

NoLayer = Union

(redirect ? RedirectLayer : NoLayer){
stack = (redirect ? RedirectLayer : NoLayer){
BasicAuthLayer{
(detect_content_type ? ContentTypeDetectionLayer : NoLayer){
(cookies === true || (cookies isa AbstractDict && !isempty(cookies)) ?
Expand All @@ -582,6 +582,10 @@ function stack(;redirect=true,
(readtimeout > 0 ? TimeoutLayer : NoLayer){
StreamLayer{Union{}}
}}}}}}}}}}}}

reduce(Layers.EXTRA_LAYERS; init=stack) do stack, (before, custom)
insert(stack, before, custom)
end
end

include("download.jl")
Expand Down
10 changes: 9 additions & 1 deletion src/layers.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Layers
export Layer, next, top_layer, insert
export Layer, next, top_layer, insert, insert_default!, remove_default!

const EXTRA_LAYERS = Set{Tuple{Union{UnionAll, Type{Union{}}}, UnionAll}}()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmm.....I don't quite understand the eltype of the Set here; why all the Tuple/Union/UnionAll dance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to keep all the type parameters as concrete as possible. The Set elements are Tuples with two elements: the first is the before layer, which can be either a Type{<:Layer} or Type{Union{}} (in the case that we insert a layer as the last layer), and the second is the custom layer, which is always a Type{<:Layer}. Since layer types are always parametric, a non-parameterized layer type is a UnionAll. Does that make sense? Maybe it's totally unnecessary to parametrize the Set like this.


include("exceptions.jl")

Expand Down Expand Up @@ -108,4 +110,10 @@ function insert(stack::Type{<:Layer}, layer_before::Type{<:Layer}, custom_layer:
throw(LayerNotFoundException("$layer_before not found in $stack"))
end

insert_default!(before::Type{<:Layer}, custom_layer::Type{<:Layer}) =
push!(EXTRA_LAYERS, (before, custom_layer))

remove_default!(before::Type{<:Layer}, custom_layer::Type{<:Layer}) =
delete!(EXTRA_LAYERS, (before, custom_layer))

end
10 changes: 10 additions & 0 deletions test/insert_layers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ using ..TestRequest
request(insert(stack(), Union{}, LastLayer), "GET", "https://httpbin.org/anything")
@test TestRequest.FLAG[]
end

@testset "Insert/remove default layers" begin
top = HTTP.top_layer(stack())
insert_default!(top, TestLayer)
@test HTTP.top_layer(stack()) <: TestLayer
remove_default!(top, TestLayer)
@test HTTP.top_layer(stack()) <: top
insert_default!(Union{}, TestLayer)
remove_default!(Union{}, TestLayer)
end
end