From 7eeefd5c6a434a50625e087ad41418a79ae1d415 Mon Sep 17 00:00:00 2001 From: Jon Malmaud Date: Fri, 29 Jan 2016 18:11:09 -0600 Subject: [PATCH 1/2] Adjustments for IO refactoring --- REQUIRE | 2 +- src/Requests.jl | 8 ++++---- src/streaming.jl | 7 ++++--- test/runtests.jl | 12 ++++++------ 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/REQUIRE b/REQUIRE index d637467..d69cebc 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,4 +1,4 @@ -julia 0.4 +julia 0.5- HttpCommon 0.2.4 HttpParser URIParser 0.1.1 diff --git a/src/Requests.jl b/src/Requests.jl index f107c3c..54b3c66 100644 --- a/src/Requests.jl +++ b/src/Requests.jl @@ -7,7 +7,7 @@ export view, save export set_proxy, set_https_proxy, get_request_settings import Base: get, write -import Base.FS: File +import Base.Filesystem: File import URIParser: URI import HttpCommon: Cookie @@ -81,7 +81,7 @@ for kind in [:Response, :Request] @eval text(r::$kind) = utf8(bytes(r)) @eval Base.bytestring(r::$kind) = text(r) @eval Base.readall(r::$kind) = text(r) - @eval Base.readbytes(r::$kind) = bytes(r) + @eval Base.read(r::$kind) = bytes(r) @eval json(r::$kind; kwargs...) = JSON.parse(text(r); kwargs...) ## Response getters to future-proof against changes to the Response type @@ -278,10 +278,10 @@ end function do_request(uri::URI, verb; kwargs...) response_stream = do_stream_request(uri, verb; kwargs...) response = response_stream.response - response.data = readbytes(response_stream) + response.data = read(response_stream) if get(response.headers, "Content-Encoding", "") ∈ ("gzip", "deflate") if !isempty(response.data) - response.data = response.data |> ZlibInflateInputStream |> readbytes + response.data = response.data |> ZlibInflateInputStream |> read end end response diff --git a/src/streaming.jl b/src/streaming.jl index 1ec1e88..8d98607 100644 --- a/src/streaming.jl +++ b/src/streaming.jl @@ -111,10 +111,11 @@ function Base.eof(stream::ResponseStream) eof(stream.buffer) && (stream.state==BodyDone || eof(stream.socket)) end -for T in [BitArray, AbstractArray, UInt8] +for T in [BitArray, Vector{UInt8}, UInt8] @eval Base.write(stream::ResponseStream, data::$T) = write(stream.socket, data) end + function Base.readbytes!(stream::ResponseStream, data::Vector{UInt8}, sz) while stream.state < BodyDone && nb_available(stream) < sz wait(stream) @@ -122,7 +123,7 @@ function Base.readbytes!(stream::ResponseStream, data::Vector{UInt8}, sz) readbytes!(stream.buffer, data, sz) end -function Base.readbytes(stream::ResponseStream) +function Base.read(stream::ResponseStream) while stream.state < BodyDone wait(stream) end @@ -140,7 +141,7 @@ function Base.readavailable(stream::ResponseStream) while nb_available(stream) == 0 wait(stream) end - readbytes(stream, nb_available(stream)) + read(stream, nb_available(stream)) end Base.close(stream::ResponseStream) = close(stream.socket) diff --git a/test/runtests.jl b/test/runtests.jl index e8e7c67..6a804c6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -133,14 +133,14 @@ data = json(post("http://httpbin.org/post", filename = Base.source_path() files = [ - FileParam(readall(filename),"text/julia","file1","runtests.jl"), + FileParam(readstring(filename),"text/julia","file1","runtests.jl"), FileParam(open(filename,"r"),"text/julia","file2","runtests.jl",true), - FileParam(IOBuffer(readall(filename)),"text/julia","file3","runtests.jl"), + FileParam(IOBuffer(readstring(filename)),"text/julia","file3","runtests.jl"), ] res = post(URI("http://httpbin.org/post"); files = files) -filecontent = readall(filename) +filecontent = readstring(filename) data = json(res) @test data["files"]["file1"] == filecontent @test data["files"]["file2"] == filecontent @@ -194,7 +194,7 @@ let write_chunked(stream, "cde") write_chunked(stream, "") - response = JSON.parse(readall(stream)) + response = JSON.parse(readstring(stream)) @test response["data"] == "abcde" end @@ -220,7 +220,7 @@ end # Requires Docker and docker-machine (obtainable via Docker toolbox) if get(ENV, "REQUESTS_TEST_PROXY", "0") == "1" run(`docker-machine create -d virtualbox proxytest`) - cmds = readall(`docker-machine env proxytest`) + cmds = readstring(`docker-machine env proxytest`) for line in split(cmds, '\n') m = match(r"export (?.*?)=(?.*)", line) m===nothing && continue @@ -228,7 +228,7 @@ if get(ENV, "REQUESTS_TEST_PROXY", "0") == "1" end @show ENV run(`docker run --name squid -d --restart=always -p 3128:3128 quay.io/sameersbn/squid:3.3.8-2`) - ip = IPv4(readall(`docker-machine ip proxytest`)) + ip = IPv4(readstring(`docker-machine ip proxytest`)) proxy_vars = ["http_proxy", "https_proxy"] for var in proxy_vars ENV[var] = "http://$ip:3128" From 19938c314394fe1e5016bda0705d71b6ba88b3df Mon Sep 17 00:00:00 2001 From: Jon Malmaud Date: Tue, 9 Feb 2016 13:33:04 -0500 Subject: [PATCH 2/2] Use Compat --- REQUIRE | 5 +++-- src/Requests.jl | 9 ++++++++- test/runtests.jl | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/REQUIRE b/REQUIRE index d69cebc..7196546 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,8 +1,9 @@ -julia 0.5- +julia 0.4 HttpCommon 0.2.4 HttpParser URIParser 0.1.1 -MbedTLS 0.1.4 +MbedTLS 0.2.1 Codecs JSON Libz +Compat 0.7.9 diff --git a/src/Requests.jl b/src/Requests.jl index 54b3c66..30d9602 100644 --- a/src/Requests.jl +++ b/src/Requests.jl @@ -7,10 +7,17 @@ export view, save export set_proxy, set_https_proxy, get_request_settings import Base: get, write -import Base.Filesystem: File + +if VERSION < v"0.5.0-dev+1229" + import Base.FS: File +else + import Base.Filesystem: File +end + import URIParser: URI import HttpCommon: Cookie +using Compat using HttpParser using HttpCommon using URIParser diff --git a/test/runtests.jl b/test/runtests.jl index 6a804c6..356093b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,3 +1,4 @@ +using Compat using Requests using JSON using Base.Test