Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Add special case for sending attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher-dG committed Nov 16, 2020
1 parent e731669 commit d34d163
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Discord"
uuid = "e67e5439-1494-44b2-b9ab-d191a16377ec"
authors = ["Chris de Graaf <me@cdg.dev> and contributors"]
version = "0.1.0"
version = "0.1.1"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/Discord.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions src/routes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added test/fixtures/files.bson
Binary file not shown.
23 changes: 23 additions & 0 deletions test/routes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 comments on commit d34d163

@christopher-dG
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register

Release notes:
Sending files as attachments is now supported.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/24748

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.1 -m "<description of version>" d34d16374fd668c167ec8bf57581767a537f8b27
git push origin v0.1.1

Please # to comment.