Skip to content

Commit dc0f837

Browse files
Update HTTP.jl compat (#19)
* Update compat * Adjust to new StatusCode handler * Fix subscriptions and limit compat to 1.0 * Fix tests * Ooops * Update src/subscriptions.jl Co-authored-by: Mal Miller <59854849+mmiller-max@users.noreply.github.com> * Simplify PR * remove ws * docs: clarify HTTP refers to HTTP.jl Co-authored-by: Mal Miller <59854849+mmiller-max@users.noreply.github.com> * chore: remove unused default * chore: update type for consistency * bump version Co-authored-by: Mal Miller <59854849+mmiller-max@users.noreply.github.com>
1 parent d6e6ac2 commit dc0f837

File tree

4 files changed

+34
-34
lines changed

4 files changed

+34
-34
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "GraphQLClient"
22
uuid = "09d831e3-9c21-47a9-bfd8-076871817219"
3-
version = "0.7.5"
3+
version = "0.7.6"
44

55
[deps]
66
GraphQLParser = "0ae10fbf-af58-4883-b66b-ff0ac82d20dd"
@@ -10,7 +10,7 @@ StructTypes = "856f2bd8-1eba-4b0a-8007-ebc267875bd4"
1010

1111
[compat]
1212
GraphQLParser = "0.1.1"
13-
HTTP = "0.8.17, 0.9"
13+
HTTP = "1"
1414
JSON3 = "1.1.2"
1515
StructTypes = "1.5"
1616
julia = "1.6"

src/subscriptions.jl

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ function open_subscription(fn::Function,
9393
"payload" => payload
9494
)
9595
message_str = JSON3.write(message)
96-
96+
throw_if_assigned = Ref{GraphQLError}()
9797
HTTP.WebSockets.open(client.ws_endpoint; retry=retry, headers=client.headers) do ws
9898
# Start sub
9999
output_info(verbose) && println("Starting $(get_name(subscription_name)) subscription with ID $sub_id")
100-
write(ws, message_str)
100+
HTTP.send(ws, message_str)
101101
subscription_tracker[][sub_id] = "open"
102102

103103
# Init function
@@ -120,11 +120,12 @@ function open_subscription(fn::Function,
120120
output_info(verbose) && println("Subscription $sub_id stopped by the stop function supplied")
121121
break
122122
end
123-
response = JSON3.read(data::Vector{UInt8}, GQLSubscriptionResponse{output_type})
123+
response = JSON3.read(data, GQLSubscriptionResponse{output_type})
124124
payload = response.payload
125125
if !isnothing(payload.errors) && !isempty(payload.errors) && throw_on_execution_error
126126
subscription_tracker[][sub_id] = "errored"
127-
throw(GraphQLError("Error during subscription.", payload))
127+
throw_if_assigned[] = GraphQLError("Error during subscription.", payload)
128+
break
128129
end
129130
# Handle multiple subs, do we need this?
130131
if response.id == string(sub_id)
@@ -137,6 +138,8 @@ function open_subscription(fn::Function,
137138
end
138139
end
139140
end
141+
# We can't throw errors from the ws handle function in HTTP.jl 1.0, as they get digested.
142+
isassigned(throw_if_assigned) && throw(throw_if_assigned[])
140143
output_debug(verbose) && println("Finished. Closing subscription")
141144
subscription_tracker[][sub_id] = "closed"
142145
return
@@ -155,7 +158,7 @@ function clear_subscriptions()
155158
end
156159
end
157160

158-
function async_reader_with_timeout(io::IO, subtimeout)::Channel
161+
function async_reader_with_timeout(ws::HTTP.WebSockets.WebSocket, subtimeout)::Channel
159162
ch = Channel(1)
160163
task = @async begin
161164
reader_task = current_task()
@@ -164,15 +167,15 @@ function async_reader_with_timeout(io::IO, subtimeout)::Channel
164167
Base.throwto(reader_task, InterruptException())
165168
end
166169
timeout = Timer(timeout_cb, subtimeout)
167-
data = readavailable(io)
170+
data = HTTP.receive(ws)
168171
subtimeout > 0 && close(timeout) # Cancel the timeout
169172
put!(ch, data)
170173
end
171174
bind(ch, task)
172175
return ch
173176
end
174177

175-
function async_reader_with_stopfn(io::IO, stopfn, checktime)::Channel
178+
function async_reader_with_stopfn(ws::HTTP.WebSockets.WebSocket, stopfn, checktime)::Channel
176179
ch = Channel(1) # Could we make this channel concretely typed?
177180
task = @async begin
178181
reader_task = current_task()
@@ -185,7 +188,7 @@ function async_reader_with_stopfn(io::IO, stopfn, checktime)::Channel
185188
end
186189
end
187190
timeout = Timer(timeout_cb, checktime)
188-
data = readavailable(io)
191+
data = HTTP.WebSockets.receive(ws)
189192
close(timeout) # Cancel the timeout
190193
put!(ch, data)
191194
end
@@ -209,7 +212,7 @@ A channel is returned with the data. If `stopfn` stops the websocket,
209212
the data will be `:stopfn`. If the timeout stops the websocket,
210213
the data will be `:timeout`
211214
"""
212-
function readfromwebsocket(ws::IO, stopfn, subtimeout)
215+
function readfromwebsocket(ws::HTTP.WebSockets.WebSocket, stopfn, subtimeout)
213216
if isnothing(stopfn) && subtimeout > 0
214217
ch_out = async_reader_with_timeout(ws, subtimeout)
215218
data = take!(ch_out)
@@ -218,7 +221,7 @@ function readfromwebsocket(ws::IO, stopfn, subtimeout)
218221
ch_out = async_reader_with_stopfn(ws, stopfn, checktime)
219222
data = take!(ch_out)
220223
else
221-
data = readavailable(ws)
224+
data = HTTP.receive(ws)
222225
end
223226
return data
224-
end
227+
end

test/http_execution.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ end
1717

1818
# handle_error
1919
@test_throws ArgumentError test_error_handler(GraphQLClient.handle_error, ArgumentError("msg"))
20-
@test_throws HTTP.StatusError test_error_handler(GraphQLClient.handle_error, HTTP.StatusError(404, HTTP.Response(404;request=HTTP.Request(), body="{}")))
21-
@test_throws GraphQLClient.GraphQLError test_error_handler(GraphQLClient.handle_error, HTTP.StatusError(400, HTTP.Response(400;request=HTTP.Request(), body="{}")))
20+
@test_throws HTTP.StatusError test_error_handler(GraphQLClient.handle_error, HTTP.StatusError(404, "POST", "", HTTP.Response(404;request=HTTP.Request(), body="{}")))
21+
@test_throws GraphQLClient.GraphQLError test_error_handler(GraphQLClient.handle_error, HTTP.StatusError(400, "POST", "", HTTP.Response(400;request=HTTP.Request(), body="{}")))
2222

2323
# handle_deserialisation_error
2424
@test_throws MethodError test_error_handler(GraphQLClient.handle_deserialisation_error, MethodError(""), "", "")

test/subscriptions.jl

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
function listen_localhost()
22
@async HTTP.listen(HTTP.Sockets.localhost, 8080) do http
3-
if HTTP.WebSockets.is_upgrade(http.message)
3+
if HTTP.WebSockets.isupgrade(http.message)
44
HTTP.WebSockets.upgrade(http) do ws
5-
while !eof(ws)
6-
data = readavailable(ws)
7-
write(ws, data)
5+
for data in ws
6+
HTTP.send(ws, data)
87
end
98
end
109
end
@@ -13,10 +12,10 @@ end
1312

1413
function do_nothing_localhost()
1514
@async HTTP.listen(HTTP.Sockets.localhost, 8081) do http
16-
if HTTP.WebSockets.is_upgrade(http.message)
15+
if HTTP.WebSockets.isupgrade(http.message)
1716
HTTP.WebSockets.upgrade(http) do ws
18-
while !eof(ws)
19-
data = readavailable(ws)
17+
for data in ws
18+
nothing;
2019
end
2120
end
2221
end
@@ -31,7 +30,7 @@ end
3130
@test take!(ch) == :timeout
3231

3332
ch = GraphQLClient.async_reader_with_timeout(ws, 5)
34-
write(ws, "Data")
33+
HTTP.send(ws, "Data")
3534
@test String(take!(ch)) == "Data"
3635

3736
# stopfn
@@ -44,11 +43,11 @@ end
4443
@test take!(ch) == :stopfn
4544
stop[] = false
4645
ch = GraphQLClient.async_reader_with_stopfn(ws, stopfn, 0.5)
47-
write(ws, "Data")
46+
HTTP.send(ws, "Data")
4847
@test String(take!(ch)) == "Data"
4948

5049
# readfromwebsocket - no timeout or stopfn
51-
write(ws, "Data")
50+
HTTP.send(ws, "Data")
5251
@test String(GraphQLClient.readfromwebsocket(ws, nothing, 0)) == "Data"
5352

5453
# readfromwebsocket - timeout
@@ -70,10 +69,9 @@ end
7069

7170
function send_error_localhost(message, port)
7271
@async HTTP.listen(HTTP.Sockets.localhost, port) do http
73-
if HTTP.WebSockets.is_upgrade(http.message)
72+
if HTTP.WebSockets.isupgrade(http.message)
7473
HTTP.WebSockets.upgrade(http) do ws
75-
while !eof(ws)
76-
data = readavailable(ws)
74+
for data in ws
7775
isempty(data) && continue
7876
query = JSON3.read(data)
7977
error_payload = """
@@ -92,7 +90,7 @@ function send_error_localhost(message, port)
9290
}
9391
}
9492
"""
95-
write(ws, error_payload)
93+
HTTP.send(ws, error_payload)
9694
end
9795
end
9896
end
@@ -101,10 +99,9 @@ end
10199

102100
function send_data_localhost(sub_name, port)
103101
@async HTTP.listen(HTTP.Sockets.localhost, port) do http
104-
if HTTP.WebSockets.is_upgrade(http.message)
102+
if HTTP.WebSockets.isupgrade(http.message)
105103
HTTP.WebSockets.upgrade(http) do ws
106-
while !eof(ws)
107-
data = readavailable(ws)
104+
for data in ws
108105
isempty(data) && continue
109106
query = JSON3.read(data)
110107
data_payload = """
@@ -119,7 +116,7 @@ function send_data_localhost(sub_name, port)
119116
}
120117
}
121118
"""
122-
write(ws, data_payload)
119+
HTTP.send(ws, data_payload)
123120
end
124121
end
125122
end
@@ -208,4 +205,4 @@ end
208205
@test results[1] isa GraphQLClient.GQLResponse{Response}
209206
@test isnothing(results[1].errors)
210207
@test !isnothing(results[1].data) # No point testing content as we've coded it in the test function
211-
end
208+
end

0 commit comments

Comments
 (0)