api

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2019 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HTTP  = "http"
	HTTPS = "https"
)

schemes

View Source
const (
	OauthEndpointMe                 = "/api/v1/me"
	OauthEndpointStylesheet         = "/r/%s/stylesheet"
	OauthEndpointSetStylesheet      = "/r/%s/api/subreddit_stylesheet"
	OauthEndpointStylesheetTemplate = "/r/%s/about/stylesheet.json"
	OauthEndpointSubmitPost         = "/api/submit"
	OauthEndpointRequestSticky      = "/api/set_subreddit_sticky"
	OauthEndpointRequestContestMode = "/api/set_contest_mode"
	OauthEndpointRequestRemovePost  = "/api/remove"
	OauthEndpointComposeMessage     = "/api/compose"
)

Oauth Endpoints

View Source
const (
	GrantTypePassword = "password"
)

grant types

View Source
const (
	RedditEndpointLogin = "/api/v1/access_token"
)

Reddit Endpoints

Variables

This section is empty.

Functions

func GetOauthURL

func GetOauthURL(endpoint string, formats ...interface{}) *url.URL

GetOauthURL returns the URL for an Oauth endpoint

func GetRedditURL

func GetRedditURL(endpoint string, formats ...interface{}) *url.URL

GetRedditURL returns the URL for a reddit endpoint

Types

type BaseJSONResponse

type BaseJSONResponse struct {
	JSON    map[string][][]string `json:"json"`
	Message string                `json:"message"` // indicates a different failure
}

BaseJSONResponse is for the JSON API and provides the necessary Error function for it

func (*BaseJSONResponse) Error

func (r *BaseJSONResponse) Error() error

type BaseResponse

type BaseResponse struct {
	Err     int64  `json:"error"`
	Message string `json:"message"`
}

BaseResponse stores "error" and "message" and provides a function for validating a response based on these

func (*BaseResponse) Error

func (r *BaseResponse) Error() error

type CommentResponse

type CommentResponse struct {
	Subreddit      string          `json:"subreddit"`
	Saved          bool            `json:"saved"`
	GildCount      int             `json:"gilded"`
	Downvotes      int64           `json:"downs"`
	Name           string          `json:"name"`
	SubredditType  string          `json:"subreddit_type"`
	Upvotes        int64           `json:"ups"`
	AuthorName     string          `json:"author_fullname"`
	Score          int64           `json:"score"`
	Edited         FloatTime       `json:"edited"`
	Archived       bool            `json:"archived"`
	Removed        bool            `json:"removed"`
	Spoiler        bool            `json:"spoiler"`
	Locked         bool            `json:"locked"`
	SubredditName  string          `json:"subreddit_id"`
	Author         string          `json:"author"`
	ContestMode    bool            `json:"contest_mode"`
	Approved       bool            `json:"approved"`
	Stickied       bool            `json:"stickied"`
	CreatedUTC     FloatTime       `json:"created_utc"`
	Body           string          `json:"body"`
	ParentID       string          `json:"parent_id"`
	RepliesListing json.RawMessage `json:"replies"`
	Replies        []*CommentResponse
}

func (*CommentResponse) DecodeReplies

func (parentComment *CommentResponse) DecodeReplies() error

type CommentsByScore

type CommentsByScore []CommentResponse

CommentsByScore implements a sort.Interface for sorting comments by score, from lowest to highest

func (CommentsByScore) Len

func (a CommentsByScore) Len() int

func (CommentsByScore) Less

func (a CommentsByScore) Less(i, j int) bool

func (CommentsByScore) Swap

func (a CommentsByScore) Swap(i, j int)

type CommentsByScoreDescending

type CommentsByScoreDescending CommentsByScore

CommentsByScoreDescending implements a sort.Interface for sorting comments by score, from highest to lowest

func (CommentsByScoreDescending) Len

func (CommentsByScoreDescending) Less

func (a CommentsByScoreDescending) Less(i, j int) bool

func (CommentsByScoreDescending) Swap

func (a CommentsByScoreDescending) Swap(i, j int)

type ComposeMessageResponse

type ComposeMessageResponse struct {
	BaseJSONResponse
}

ComposeMessageResponse is the response from composing a message

type FloatTime

type FloatTime time.Time

FloatTime implements a custom unmarshaller for decoding the float time into a time.Time-compatible object

func (*FloatTime) UnmarshalJSON

func (ft *FloatTime) UnmarshalJSON(data []byte) error

type MeResponse

type MeResponse struct {
	BaseResponse

	Username         string    `json:"reddit_bot"`
	CommentKarma     int       `json:"comment_karma"`
	LinkKarma        int       `json:"link_karma"`
	Created          FloatTime `json:"created"`
	CreatedUTC       FloatTime `json:"created_utc"`
	HasMail          bool      `json:"has_mail"`
	HasModMail       bool      `json:"has_mod_mail"`
	HasVerifiedEmail bool      `json:"has_verified_email"`
	ID               string    `json:"id"`
	HasGold          bool      `json:"is_gold"`
	IsMod            bool      `json:"is_mod"`
	Over18           bool      `json:"over_18"`
}

MeResponse is the response from a Me query

type PostResponse

type PostResponse struct {
	Subreddit     string    `json:"subreddit"`
	Saved         bool      `json:"saved"`
	GildCount     int       `json:"gilded"`
	Hidden        bool      `json:"hidden"`
	Downvotes     int64     `json:"downs"`
	Name          string    `json:"name"`
	ID            string    `json:"id"`
	Quarantined   bool      `json:"quarantine"`
	SubredditType string    `json:"subreddit_type"`
	Upvotes       int64     `json:"ups"`
	AuthorName    string    `json:"author_fullname"`
	CommentCount  int64     `json:"num_comments"`
	Score         int64     `json:"score"`
	Edited        FloatTime `json:"edited"`
	IsSelf        bool      `json:"is_self"`
	Archived      bool      `json:"archived"`
	NSFW          bool      `json:"over_18"`
	Removed       bool      `json:"removed"`
	Spoiler       bool      `json:"spoiler"`
	Locked        bool      `json:"locked"`
	SubredditName string    `json:"subreddit_id"`
	Author        string    `json:"author"`
	ContestMode   bool      `json:"contest_mode"`
	Approved      bool      `json:"approved"`
	Stickied      bool      `json:"stickied"`
	URL           string    `json:"url"`
	CreatedUTC    FloatTime `json:"created_utc"`
	Body          string    `json:"selftext"`
	Replies       []CommentResponse
}

type RedditAPI

type RedditAPI struct {
	ClientID string

	UserAgent string
	Account   *RedditAccount
	Client    http.Client
	DebugMode bool
	// contains filtered or unexported fields
}

RedditAPI interfaces all the necessary functions to interact with Reddit via the official API

func NewRedditAPI

func NewRedditAPI(clientID, clientSecret, userAgent, username string, debugMode bool) *RedditAPI

NewRedditAPI creates a new API with a given ClientID and ClientSecret and with an unauthenticated account

func (*RedditAPI) ComposeMessage

func (api *RedditAPI) ComposeMessage(to, subject, text string) error

ComposeMessage sends a message to another user

func (*RedditAPI) Get

func (api *RedditAPI) Get(u *url.URL, query url.Values) (*http.Response, error)

Get performs a GET request to the specified URL with the specified query parameters Don't forget to close the response body

func (*RedditAPI) NewRequest

func (api *RedditAPI) NewRequest(method string, u *url.URL, body io.Reader) (*http.Request, error)

func (*RedditAPI) PostForm

func (api *RedditAPI) PostForm(u *url.URL, data url.Values) (*http.Response, error)

PostForm posts form data to the specified URL with the required authentication Don't forget to close the response body

func (*RedditAPI) RequestContestMode

func (api *RedditAPI) RequestContestMode(name string, state bool) error

RequestContestMode allows setting a post to contest mode

func (*RedditAPI) RequestMe

func (api *RedditAPI) RequestMe() (*MeResponse, error)

RequestMe queries the "me" API endpoint

func (*RedditAPI) RequestPostJSON

func (api *RedditAPI) RequestPostJSON(u *url.URL) (*PostResponse, error)

RequestPostJSON gets the JSON for a particular post

func (*RedditAPI) RequestRemovePost

func (api *RedditAPI) RequestRemovePost(name string, spam bool) error

RequestRemovePost removes a post as a moderator. Spam specifies whether or not to remove it as spam

func (*RedditAPI) RequestSetStylesheet

func (api *RedditAPI) RequestSetStylesheet(subreddit, stylesheet, reason string) (*SetStylesheetResponse, error)

RequestSetStylesheet sets the stylesheet for a subreddit

func (*RedditAPI) RequestSticky

func (api *RedditAPI) RequestSticky(subreddit string, name string, state bool, num int) error

RequestSticky allows setting a post to sticky set num to -1 for bottom

func (*RedditAPI) RequestStylesheet

func (api *RedditAPI) RequestStylesheet(subreddit string) (string, error)

RequestStylesheet gets the stylesheet of a particular subreddit

func (*RedditAPI) RequestStylesheetTemplate

func (api *RedditAPI) RequestStylesheetTemplate(subreddit string) (*StylesheetTemplateData, error)

RequestStylesheetTemplate gets the stylesheet template (with e.g. %% %% for images instead of actual urls) for a particular subrededit

func (*RedditAPI) RequestSubmitTextPost

func (api *RedditAPI) RequestSubmitTextPost(subreddit, title, text string, ad, nsfw, spoiler, sendReplies bool) (*SubmitPostData, error)

type RedditAccount

type RedditAccount struct {
	API      *RedditAPI
	Username string

	Token *Token
}

RedditAccount holds the data pertaining to a reddit account

func (*RedditAccount) PasswordLogin

func (a *RedditAccount) PasswordLogin(password string) error

PasswordLogin uses a password to authenticate, storing the access token in the RedditAccount. Returns an error.

type SetStylesheetResponse

type SetStylesheetResponse struct {
	BaseJSONResponse
}

type StylesheetTemplateData

type StylesheetTemplateData struct {
	Images     []StylesheetTemplateImage `json:"images"`
	Stylesheet string                    `json:"stylesheet"`
}

StylesheetTemplateData is the data returned in the stylesheet template request

type StylesheetTemplateImage

type StylesheetTemplateImage struct {
	URL  string `json:"url"`
	Link string `json:"link"`
	Name string `json:"name"`
}

StylesheetTemplateImage stores the data about an image that has been uploaded to a subreddit for use in the stylesheet

type SubmitPostData

type SubmitPostData struct {
	URL    string `json:"url"`
	Drafts int    `json:"drafts_count"`
	ID     string `json:"id"`
	Name   string `json:"name"`
}

type Token

type Token struct {
	Token     string         `json:"access_token"`
	TokenType string         `json:"token_type"`
	Scope     string         `json:"scope"`
	ExpiresIn TokenExpiresIn `json:"expires_in"` // seconds
	Expiry    time.Time
	Error     string `json:"error"`
}

Token stores the authentication token and expiry time so that the validity of the token can be automatically verified before requests.

type TokenExpiresIn

type TokenExpiresIn time.Duration

TokenExpiresIn is a Duration with custom unmarshaller for unmarshalling the duration as seconds

func (*TokenExpiresIn) UnmarshalJSON

func (t *TokenExpiresIn) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes the JSON into this

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL