diff --git a/Project.toml b/Project.toml index a084af3..8f2b4cc 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Discord" uuid = "e67e5439-1494-44b2-b9ab-d191a16377ec" authors = ["Chris de Graaf and contributors"] -version = "0.1.0" +version = "0.1.1" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" @@ -11,7 +11,7 @@ Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a" SuperEnum = "5c958174-e7d9-5990-9618-4567de4ba542" [compat] -HTTP = "0.8" +HTTP = "0.9" JSON3 = "1" Parameters = "0.12" SuperEnum = "0.1" diff --git a/src/Discord.jl b/src/Discord.jl index 8b4e7e2..afb8752 100644 --- a/src/Discord.jl +++ b/src/Discord.jl @@ -4,7 +4,7 @@ using Base.Iterators: Pairs using Dates: DateTime, ISODateTimeFormat, Millisecond, UTC, now, unix2datetime, year -using HTTP: HTTP, Response, StatusError, escapeuri, header, request +using HTTP: HTTP, Form, Response, StatusError, escapeuri, header, request using JSON3: JSON3, StructTypes using Parameters: @with_kw using SuperEnum: @se diff --git a/src/routes.jl b/src/routes.jl index d0d05c1..8916924 100644 --- a/src/routes.jl +++ b/src/routes.jl @@ -15,9 +15,24 @@ function api_call(c, method, path, Into=Nothing, params=Dict(); kwargs...) "X-RateLimit-Precision" => "millisecond", ] - body, query = if method in (:PATCH, :PUT, :POST) - push!(headers, "Content-Type" => "application/json") - JSON3.write(kwargs), params + body, query = if method in (:PATCH, :POST, :PUT) + if haskey(kwargs, :file) + kw_dict = Dict(kwargs) + file = pop!(kw_dict, :file) + # This is just a hack to allow for easier testing. No user should use this. + boundary = NamedTuple() + if haskey(kw_dict, :__boundary__) + boundary = (; boundary=pop!(kw_dict, :__boundary__)) + end + form = Form( + Dict(:file => file, :payload_json => JSON3.write(kw_dict)); + boundary..., + ) + form, params + else + push!(headers, "Content-Type" => "application/json") + JSON3.write(kwargs), params + end else "", kwargs end diff --git a/test/fixtures/files.bson b/test/fixtures/files.bson new file mode 100644 index 0000000..b99090a Binary files /dev/null and b/test/fixtures/files.bson differ diff --git a/test/routes.jl b/test/routes.jl index 471dbfd..9a40aee 100644 --- a/test/routes.jl +++ b/test/routes.jl @@ -159,3 +159,26 @@ const client = D.BotClient(get(ENV, "DISCORD_TOKEN", "")) end end end + +@testset "Sending files" begin + playback("files.bson") do + guild = D.create_guild(client; name="MyGuild") + try + channel = D.create_guild_channel(client, guild; name="test") + msg = mktempdir() do dir + file = joinpath(dir, "foo.json") + write(file, "{}") + open(file) do f + D.create_message(client, channel; file=f, __boundary__="abcdef") + end + end + attachments = msg.attachments + @test length(attachments) == 1 + attachment = attachments[1] + @test attachment.filename == "foo.json" + @test attachment.size == 2 + finally + D.delete_guild(client, guild) + end + end +end