Utils: Deprecated client-scoped JSON driver

This commit is contained in:
diamondburned (Forefront) 2020-05-07 20:43:46 -07:00
parent cd1dfa10a0
commit 3a43e413d8
12 changed files with 38 additions and 45 deletions

View File

@ -42,7 +42,7 @@ func (c *Client) CreateChannel(
return ch, c.RequestJSON( return ch, c.RequestJSON(
&ch, "POST", &ch, "POST",
EndpointGuilds+guildID.String()+"/channels", EndpointGuilds+guildID.String()+"/channels",
httputil.WithJSONBody(c, data), httputil.WithJSONBody(data),
) )
} }
@ -57,7 +57,7 @@ func (c *Client) MoveChannel(guildID discord.Snowflake, datum []MoveChannelData)
return c.FastRequest( return c.FastRequest(
"PATCH", "PATCH",
EndpointGuilds+guildID.String()+"/channels", EndpointGuilds+guildID.String()+"/channels",
httputil.WithJSONBody(c, datum), httputil.WithJSONBody(datum),
) )
} }
@ -93,7 +93,7 @@ func (c *Client) ModifyChannel(channelID discord.Snowflake, data ModifyChannelDa
return c.FastRequest( return c.FastRequest(
"PATCH", "PATCH",
EndpointChannels+channelID.String(), EndpointChannels+channelID.String(),
httputil.WithJSONBody(c, data), httputil.WithJSONBody(data),
) )
} }
@ -107,7 +107,7 @@ func (c *Client) EditChannelPermission(
url := EndpointChannels + channelID.String() + "/permissions/" + overwrite.ID.String() url := EndpointChannels + channelID.String() + "/permissions/" + overwrite.ID.String()
overwrite.ID = 0 overwrite.ID = 0
return c.FastRequest("PUT", url, httputil.WithJSONBody(c, overwrite)) return c.FastRequest("PUT", url, httputil.WithJSONBody(overwrite))
} }
func (c *Client) DeleteChannelPermission(channelID, overwriteID discord.Snowflake) error { func (c *Client) DeleteChannelPermission(channelID, overwriteID discord.Snowflake) error {
@ -153,7 +153,7 @@ func (c *Client) AddRecipient(
return c.FastRequest( return c.FastRequest(
"PUT", "PUT",
EndpointChannels+channelID.String()+"/recipients/"+userID.String(), EndpointChannels+channelID.String()+"/recipients/"+userID.String(),
httputil.WithJSONBody(c, params), httputil.WithJSONBody(params),
) )
} }
@ -176,6 +176,6 @@ func (c *Client) Ack(channelID, messageID discord.Snowflake, ack *Ack) error {
ack, "POST", ack, "POST",
EndpointChannels+channelID.String()+ EndpointChannels+channelID.String()+
"/messages/"+messageID.String()+"/ack", "/messages/"+messageID.String()+"/ack",
httputil.WithJSONBody(c, ack), httputil.WithJSONBody(ack),
) )
} }

View File

@ -59,7 +59,7 @@ func (c *Client) CreateEmoji(
return emj, c.RequestJSON( return emj, c.RequestJSON(
&emj, "POST", &emj, "POST",
EndpointGuilds+guildID.String()+"/emojis", EndpointGuilds+guildID.String()+"/emojis",
httputil.WithJSONBody(c, param), httputil.WithJSONBody(param),
) )
} }
@ -80,7 +80,7 @@ func (c *Client) ModifyEmoji(
return c.FastRequest( return c.FastRequest(
"PATCH", "PATCH",
EndpointGuilds+guildID.String()+"/emojis/"+emojiID.String(), EndpointGuilds+guildID.String()+"/emojis/"+emojiID.String(),
httputil.WithJSONBody(c, param), httputil.WithJSONBody(param),
) )
} }

View File

@ -32,7 +32,7 @@ type CreateGuildData struct {
func (c *Client) CreateGuild(data CreateGuildData) (*discord.Guild, error) { func (c *Client) CreateGuild(data CreateGuildData) (*discord.Guild, error) {
var g *discord.Guild var g *discord.Guild
return g, c.RequestJSON(&g, "POST", Endpoint+"guilds", httputil.WithJSONBody(c, data)) return g, c.RequestJSON(&g, "POST", Endpoint+"guilds", httputil.WithJSONBody(data))
} }
func (c *Client) Guild(id discord.Snowflake) (*discord.Guild, error) { func (c *Client) Guild(id discord.Snowflake) (*discord.Guild, error) {
@ -148,7 +148,7 @@ func (c *Client) ModifyGuild(id discord.Snowflake, data ModifyGuildData) (*disco
return g, c.RequestJSON( return g, c.RequestJSON(
&g, "PATCH", &g, "PATCH",
EndpointGuilds+id.String(), EndpointGuilds+id.String(),
httputil.WithJSONBody(c, data), httputil.WithJSONBody(data),
) )
} }
@ -214,7 +214,7 @@ func (c *Client) AttachIntegration(
return c.FastRequest( return c.FastRequest(
"POST", "POST",
EndpointGuilds+guildID.String()+"/integrations", EndpointGuilds+guildID.String()+"/integrations",
httputil.WithJSONBody(c, param), httputil.WithJSONBody(param),
) )
} }

View File

@ -24,8 +24,7 @@ func (c *Client) Login(email, password string) (*LoginResponse, error) {
param.Password = password param.Password = password
var r *LoginResponse var r *LoginResponse
return r, c.RequestJSON(&r, "POST", EndpointLogin, return r, c.RequestJSON(&r, "POST", EndpointLogin, httputil.WithJSONBody(param))
httputil.WithJSONBody(c, param))
} }
func (c *Client) TOTP(code, ticket string) (*LoginResponse, error) { func (c *Client) TOTP(code, ticket string) (*LoginResponse, error) {
@ -37,6 +36,5 @@ func (c *Client) TOTP(code, ticket string) (*LoginResponse, error) {
param.Ticket = ticket param.Ticket = ticket
var r *LoginResponse var r *LoginResponse
return r, c.RequestJSON(&r, "POST", EndpointTOTP, return r, c.RequestJSON(&r, "POST", EndpointTOTP, httputil.WithJSONBody(param))
httputil.WithJSONBody(c, param))
} }

View File

@ -113,7 +113,7 @@ func (c *Client) AddMember(
return mem, c.RequestJSON( return mem, c.RequestJSON(
&mem, "PUT", &mem, "PUT",
EndpointGuilds+guildID.String()+"/members/"+userID.String(), EndpointGuilds+guildID.String()+"/members/"+userID.String(),
httputil.WithJSONBody(c, param), httputil.WithJSONBody(param),
) )
} }
@ -123,7 +123,7 @@ func (c *Client) ModifyMember(
return c.FastRequest( return c.FastRequest(
"PATCH", "PATCH",
EndpointGuilds+guildID.String()+"/members/"+userID.String(), EndpointGuilds+guildID.String()+"/members/"+userID.String(),
httputil.WithJSONBody(c, data), httputil.WithJSONBody(data),
) )
} }

View File

@ -130,7 +130,7 @@ func (c *Client) EditMessage(
return msg, c.RequestJSON( return msg, c.RequestJSON(
&msg, "PATCH", &msg, "PATCH",
EndpointChannels+channelID.String()+"/messages/"+messageID.String(), EndpointChannels+channelID.String()+"/messages/"+messageID.String(),
httputil.WithJSONBody(c, param), httputil.WithJSONBody(param),
) )
} }
@ -151,5 +151,5 @@ func (c *Client) DeleteMessages(channelID discord.Snowflake, messageIDs []discor
param.Messages = messageIDs param.Messages = messageIDs
return c.FastRequest("POST", EndpointChannels+channelID.String()+ return c.FastRequest("POST", EndpointChannels+channelID.String()+
"/messages/bulk-delete", httputil.WithJSONBody(c, param)) "/messages/bulk-delete", httputil.WithJSONBody(param))
} }

View File

@ -39,7 +39,7 @@ func (c *Client) CreateRole(
return role, c.RequestJSON( return role, c.RequestJSON(
&role, "POST", &role, "POST",
EndpointGuilds+guildID.String()+"/roles", EndpointGuilds+guildID.String()+"/roles",
httputil.WithJSONBody(c, data), httputil.WithJSONBody(data),
) )
} }
@ -58,7 +58,7 @@ func (c *Client) MoveRole(
return roles, c.RequestJSON( return roles, c.RequestJSON(
&roles, "PATCH", &roles, "PATCH",
EndpointGuilds+guildID.String()+"/roles", EndpointGuilds+guildID.String()+"/roles",
httputil.WithJSONBody(c, param), httputil.WithJSONBody(param),
) )
} }
@ -70,7 +70,7 @@ func (c *Client) ModifyRole(
return role, c.RequestJSON( return role, c.RequestJSON(
&role, "PATCH", &role, "PATCH",
EndpointGuilds+guildID.String()+"/roles/"+roleID.String(), EndpointGuilds+guildID.String()+"/roles/"+roleID.String(),
httputil.WithJSONBody(c, data), httputil.WithJSONBody(data),
) )
} }

View File

@ -107,8 +107,8 @@ type SendMessageData struct {
AllowedMentions *AllowedMentions `json:"allowed_mentions,omitempty"` AllowedMentions *AllowedMentions `json:"allowed_mentions,omitempty"`
} }
func (data *SendMessageData) WriteMultipart(c json.Driver, body *multipart.Writer) error { func (data *SendMessageData) WriteMultipart(body *multipart.Writer) error {
return writeMultipart(c, body, data, data.Files) return writeMultipart(body, data, data.Files)
} }
func (c *Client) SendMessageComplex( func (c *Client) SendMessageComplex(
@ -135,11 +135,11 @@ func (c *Client) SendMessageComplex(
if len(data.Files) == 0 { if len(data.Files) == 0 {
// No files, so no need for streaming. // No files, so no need for streaming.
return msg, c.RequestJSON(&msg, "POST", URL, httputil.WithJSONBody(c, data)) return msg, c.RequestJSON(&msg, "POST", URL, httputil.WithJSONBody(data))
} }
writer := func(mw *multipart.Writer) error { writer := func(mw *multipart.Writer) error {
return data.WriteMultipart(c, mw) return data.WriteMultipart(mw)
} }
resp, err := c.MeanwhileMultipart(writer, "POST", URL) resp, err := c.MeanwhileMultipart(writer, "POST", URL)
@ -150,7 +150,7 @@ func (c *Client) SendMessageComplex(
var body = resp.GetBody() var body = resp.GetBody()
defer body.Close() defer body.Close()
return msg, c.DecodeStream(body, &msg) return msg, json.DecodeStream(body, &msg)
} }
type ExecuteWebhookData struct { type ExecuteWebhookData struct {
@ -170,8 +170,8 @@ type ExecuteWebhookData struct {
AvatarURL discord.URL `json:"avatar_url,omitempty"` AvatarURL discord.URL `json:"avatar_url,omitempty"`
} }
func (data *ExecuteWebhookData) WriteMultipart(c json.Driver, body *multipart.Writer) error { func (data *ExecuteWebhookData) WriteMultipart(body *multipart.Writer) error {
return writeMultipart(c, body, data, data.Files) return writeMultipart(body, data, data.Files)
} }
// ExecuteWebhook sends a message to the webhook. If wait is bool, Discord will // ExecuteWebhook sends a message to the webhook. If wait is bool, Discord will
@ -210,11 +210,11 @@ func (c *Client) ExecuteWebhook(
if len(data.Files) == 0 { if len(data.Files) == 0 {
// No files, so no need for streaming. // No files, so no need for streaming.
return msg, c.RequestJSON(&msg, "POST", URL, return msg, c.RequestJSON(&msg, "POST", URL,
httputil.WithJSONBody(c, data)) httputil.WithJSONBody(data))
} }
writer := func(mw *multipart.Writer) error { writer := func(mw *multipart.Writer) error {
return data.WriteMultipart(c, mw) return data.WriteMultipart(mw)
} }
resp, err := c.MeanwhileMultipart(writer, "POST", URL) resp, err := c.MeanwhileMultipart(writer, "POST", URL)
@ -230,13 +230,10 @@ func (c *Client) ExecuteWebhook(
return nil, nil return nil, nil
} }
return msg, c.DecodeStream(body, &msg) return msg, json.DecodeStream(body, &msg)
} }
func writeMultipart( func writeMultipart(body *multipart.Writer, item interface{}, files []SendMessageFile) error {
c json.Driver, body *multipart.Writer,
item interface{}, files []SendMessageFile) error {
defer body.Close() defer body.Close()
// Encode the JSON body first // Encode the JSON body first
@ -245,7 +242,7 @@ func writeMultipart(
return errors.Wrap(err, "Failed to create bodypart for JSON") return errors.Wrap(err, "Failed to create bodypart for JSON")
} }
if err := c.EncodeStream(w, item); err != nil { if err := json.EncodeStream(w, item); err != nil {
return errors.Wrap(err, "Failed to encode JSON") return errors.Wrap(err, "Failed to encode JSON")
} }

View File

@ -47,7 +47,7 @@ func (c *Client) CreatePrivateChannel(
var dm *discord.Channel var dm *discord.Channel
return dm, c.RequestJSON(&dm, "POST", EndpointMe+"/channels", return dm, c.RequestJSON(&dm, "POST", EndpointMe+"/channels",
httputil.WithJSONBody(c, param)) httputil.WithJSONBody(param))
} }
// ChangeOwnNickname only replies with the nickname back, so we're not even // ChangeOwnNickname only replies with the nickname back, so we're not even
@ -64,7 +64,7 @@ func (c *Client) ChangeOwnNickname(
return c.FastRequest( return c.FastRequest(
"PATCH", "PATCH",
EndpointGuilds+guildID.String()+"/members/@me/nick", EndpointGuilds+guildID.String()+"/members/@me/nick",
httputil.WithJSONBody(c, param), httputil.WithJSONBody(param),
) )
} }

View File

@ -25,7 +25,7 @@ func (c *Client) CreateWebhook(
return w, c.RequestJSON( return w, c.RequestJSON(
&w, "POST", &w, "POST",
EndpointChannels+channelID.String()+"/webhooks", EndpointChannels+channelID.String()+"/webhooks",
httputil.WithJSONBody(c, param), httputil.WithJSONBody(param),
) )
} }

View File

@ -23,7 +23,6 @@ var Retries uint = 5
type Client struct { type Client struct {
httpdriver.Client httpdriver.Client
json.Driver
SchemaEncoder SchemaEncoder
// OnRequest, if not nil, will be copied and prefixed on each Request. // OnRequest, if not nil, will be copied and prefixed on each Request.
@ -42,7 +41,6 @@ type Client struct {
func NewClient() *Client { func NewClient() *Client {
return &Client{ return &Client{
Client: httpdriver.NewClient(), Client: httpdriver.NewClient(),
Driver: json.Default,
SchemaEncoder: &DefaultSchema{}, SchemaEncoder: &DefaultSchema{},
Retries: Retries, Retries: Retries,
context: context.Background(), context: context.Background(),
@ -147,7 +145,7 @@ func (c *Client) RequestJSON(to interface{}, method, url string, opts ...Request
return nil return nil
} }
if err := c.DecodeStream(body, to); err != nil { if err := json.DecodeStream(body, to); err != nil {
return JSONError{err} return JSONError{err}
} }
@ -211,7 +209,7 @@ func (c *Client) Request(method, url string, opts ...RequestOption) (httpdriver.
} }
// Optionally unmarshal the error. // Optionally unmarshal the error.
c.Unmarshal(httpErr.Body, &httpErr) json.Unmarshal(httpErr.Body, &httpErr)
return nil, httpErr return nil, httpErr
} }

View File

@ -68,7 +68,7 @@ func WithBody(body io.ReadCloser) RequestOption {
} }
// WithJSONBody inserts a JSON body into the request. This ignores JSON errors. // WithJSONBody inserts a JSON body into the request. This ignores JSON errors.
func WithJSONBody(json json.Driver, v interface{}) RequestOption { func WithJSONBody(v interface{}) RequestOption {
if v == nil { if v == nil {
return func(httpdriver.Request) error { return func(httpdriver.Request) error {
return nil return nil