Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented EditUserText api request. #38

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 71 additions & 6 deletions oauth_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func (o *OAuthSession) NewCaptcha() (string, error) {
return c.Json.Data.Iden, nil
}

// getBody allows user to ask for custom OAuth requests
func (o *OAuthSession) getBody(link string, d interface{}) error {
req, err := http.NewRequest("GET", link, nil)
if err != nil {
Expand Down Expand Up @@ -186,6 +187,31 @@ func (o *OAuthSession) getBody(link string, d interface{}) error {
return nil
}

// GetRawRequest allows user to ask for custom OAuth requests
func (o *OAuthSession) GetRawRequest(link string) (response []byte, err error) {
req, err := http.NewRequest("GET", link, nil)
if err != nil {
return response, err
}

if o.Client == nil {
return response, errors.New("OAuth Session lacks HTTP client! Use func (o OAuthSession) LoginAuth() to make one.")
}

// Throttle request
if o.throttle != nil {
o.throttle.Wait()
}

resp, err := o.Client.Do(req)
if err != nil {
return response, err
}
defer resp.Body.Close()

return ioutil.ReadAll(resp.Body)
}

func (o *OAuthSession) Me() (*Redditor, error) {
r := &Redditor{}
err := o.getBody("https://oauth.reddit.com/api/v1/me", r)
Expand Down Expand Up @@ -411,6 +437,45 @@ func (o *OAuthSession) postBody(link string, form url.Values, d interface{}) err
return nil
}

// Delete accepts a thing_id and deletes a link or comment.
// Returns an error
func (o *OAuthSession) Delete(thingID string) error {
// Build form for POST request.
v := url.Values{
"id": {thingID},
}

return o.postBody("https://oauth.reddit.com/api/del", v, nil)

}

// EditUserText accepts an Edit type and edits the body text of a comment or self-post using OAuth.
// Returns a Submission type
func (o *OAuthSession) EditUserText(e *Edit) (*Submission, error) {
// Build form for POST request.
v := url.Values{
"api_type": {"json"},
"return_rtjson": {"true"},
"text": {e.Text},
"thing_id": {e.ThingID},
}

type submission struct {
Json struct {
Errors [][]string
Data Submission
}
}
submit := &submission{}

err := o.postBody("https://oauth.reddit.com/api/editusertext", v, submit)
if err != nil {
return nil, err
}
// TODO check s.Errors and do something useful?
return &submit.Json.Data, nil
}

// Submit accepts a NewSubmission type and submits a new link using OAuth.
// Returns a Submission type.
func (o *OAuthSession) Submit(ns *NewSubmission) (*Submission, error) {
Expand Down Expand Up @@ -451,13 +516,13 @@ func (o *OAuthSession) Submit(ns *NewSubmission) (*Submission, error) {
}

// Delete deletes a link or comment using the given full name ID.
func (o *OAuthSession) Delete(d Deleter) error {
// Build form for POST request.
v := url.Values{}
v.Add("id", d.deleteID())
// func (o *OAuthSession) Delete(d Deleter) error {
// Build form for POST request.
// v := url.Values{}
// v.Add("id", d.deleteID())

return o.postBody("https://oauth.reddit.com/api/del", v, nil)
}
// return o.postBody("https://oauth.reddit.com/api/del", v, nil)
// }

// SubredditSubmissions returns the submissions on the given subreddit using OAuth.
func (o *OAuthSession) SubredditSubmissions(subreddit string, sort PopularitySort, params ListingOptions) ([]*Submission, error) {
Expand Down
11 changes: 11 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ type NewSubmission struct {
Captcha *Captcha
}

// Edit contains the data needed to edit a user text
type Edit struct {
Text string
ThingID string
}

// NewEdit returns an Edit with parameters appropriate for an edit
func NewEdit(text string, thingID string) *Edit {
return &Edit{text, thingID}
}

// NewLinkSubmission returns a NewSubmission with parameters appropriate for a link submission
func NewLinkSubmission(sr, title, link string, replies bool, c *Captcha) *NewSubmission {
return &NewSubmission{sr, title, link, false, replies, true, true, c}
Expand Down