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(
&ch, "POST",
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(
"PATCH",
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(
"PATCH",
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()
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 {
@ -153,7 +153,7 @@ func (c *Client) AddRecipient(
return c.FastRequest(
"PUT",
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",
EndpointChannels+channelID.String()+
"/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(
&emj, "POST",
EndpointGuilds+guildID.String()+"/emojis",
httputil.WithJSONBody(c, param),
httputil.WithJSONBody(param),
)
}
@ -80,7 +80,7 @@ func (c *Client) ModifyEmoji(
return c.FastRequest(
"PATCH",
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) {
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) {
@ -148,7 +148,7 @@ func (c *Client) ModifyGuild(id discord.Snowflake, data ModifyGuildData) (*disco
return g, c.RequestJSON(
&g, "PATCH",
EndpointGuilds+id.String(),
httputil.WithJSONBody(c, data),
httputil.WithJSONBody(data),
)
}
@ -214,7 +214,7 @@ func (c *Client) AttachIntegration(
return c.FastRequest(
"POST",
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
var r *LoginResponse
return r, c.RequestJSON(&r, "POST", EndpointLogin,
httputil.WithJSONBody(c, param))
return r, c.RequestJSON(&r, "POST", EndpointLogin, httputil.WithJSONBody(param))
}
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
var r *LoginResponse
return r, c.RequestJSON(&r, "POST", EndpointTOTP,
httputil.WithJSONBody(c, param))
return r, c.RequestJSON(&r, "POST", EndpointTOTP, httputil.WithJSONBody(param))
}

View File

@ -113,7 +113,7 @@ func (c *Client) AddMember(
return mem, c.RequestJSON(
&mem, "PUT",
EndpointGuilds+guildID.String()+"/members/"+userID.String(),
httputil.WithJSONBody(c, param),
httputil.WithJSONBody(param),
)
}
@ -123,7 +123,7 @@ func (c *Client) ModifyMember(
return c.FastRequest(
"PATCH",
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(
&msg, "PATCH",
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
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(
&role, "POST",
EndpointGuilds+guildID.String()+"/roles",
httputil.WithJSONBody(c, data),
httputil.WithJSONBody(data),
)
}
@ -58,7 +58,7 @@ func (c *Client) MoveRole(
return roles, c.RequestJSON(
&roles, "PATCH",
EndpointGuilds+guildID.String()+"/roles",
httputil.WithJSONBody(c, param),
httputil.WithJSONBody(param),
)
}
@ -70,7 +70,7 @@ func (c *Client) ModifyRole(
return role, c.RequestJSON(
&role, "PATCH",
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"`
}
func (data *SendMessageData) WriteMultipart(c json.Driver, body *multipart.Writer) error {
return writeMultipart(c, body, data, data.Files)
func (data *SendMessageData) WriteMultipart(body *multipart.Writer) error {
return writeMultipart(body, data, data.Files)
}
func (c *Client) SendMessageComplex(
@ -135,11 +135,11 @@ func (c *Client) SendMessageComplex(
if len(data.Files) == 0 {
// 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 {
return data.WriteMultipart(c, mw)
return data.WriteMultipart(mw)
}
resp, err := c.MeanwhileMultipart(writer, "POST", URL)
@ -150,7 +150,7 @@ func (c *Client) SendMessageComplex(
var body = resp.GetBody()
defer body.Close()
return msg, c.DecodeStream(body, &msg)
return msg, json.DecodeStream(body, &msg)
}
type ExecuteWebhookData struct {
@ -170,8 +170,8 @@ type ExecuteWebhookData struct {
AvatarURL discord.URL `json:"avatar_url,omitempty"`
}
func (data *ExecuteWebhookData) WriteMultipart(c json.Driver, body *multipart.Writer) error {
return writeMultipart(c, body, data, data.Files)
func (data *ExecuteWebhookData) WriteMultipart(body *multipart.Writer) error {
return writeMultipart(body, data, data.Files)
}
// 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 {
// No files, so no need for streaming.
return msg, c.RequestJSON(&msg, "POST", URL,
httputil.WithJSONBody(c, data))
httputil.WithJSONBody(data))
}
writer := func(mw *multipart.Writer) error {
return data.WriteMultipart(c, mw)
return data.WriteMultipart(mw)
}
resp, err := c.MeanwhileMultipart(writer, "POST", URL)
@ -230,13 +230,10 @@ func (c *Client) ExecuteWebhook(
return nil, nil
}
return msg, c.DecodeStream(body, &msg)
return msg, json.DecodeStream(body, &msg)
}
func writeMultipart(
c json.Driver, body *multipart.Writer,
item interface{}, files []SendMessageFile) error {
func writeMultipart(body *multipart.Writer, item interface{}, files []SendMessageFile) error {
defer body.Close()
// Encode the JSON body first
@ -245,7 +242,7 @@ func writeMultipart(
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")
}

View File

@ -47,7 +47,7 @@ func (c *Client) CreatePrivateChannel(
var dm *discord.Channel
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
@ -64,7 +64,7 @@ func (c *Client) ChangeOwnNickname(
return c.FastRequest(
"PATCH",
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(
&w, "POST",
EndpointChannels+channelID.String()+"/webhooks",
httputil.WithJSONBody(c, param),
httputil.WithJSONBody(param),
)
}

View File

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