Documentation ¶
Index ¶
Constants ¶
const DefaultPerPage = 10
DefaultPerPage is the default number of items to return per page in a paginated result set.
Variables ¶
var (
ErrPostNotFound = errors.New("post not found")
)
Functions ¶
func CheckResponse ¶
CheckResponse checks the API response for errors, and returns them if present. A response is considered an error if it has a status code outside the 200 range. API error responses are expected to have either no response body, or a JSON response body that maps to ErrorResponse. Any other response body will be silently ignored.
func IsHTTPErrorCode ¶
Types ¶
type Client ¶
type Client struct { Posts PostsService // BaseURL for HTTP requests to thesrc's API. BaseURL *url.URL //UserAgent used for HTTP requests to thesrc's API. UserAgent string // contains filtered or unexported fields }
A Client communicates with thesrc's HTTP API.
func NewClient ¶
NewClient creates a new HTTP API client for thesrc. If httpClient == nil, then http.DefaultClient is used.
func (*Client) Do ¶
Do sends an API request and returns the API response. The API response is JSON-decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred.
func (*Client) NewRequest ¶
NewRequest creates an API request. A relative URL can be provided in urlStr, in which case it is resolved relative to the BaseURL of the Client. Relative URLs should always be specified without a preceding slash. If specified, the value pointed to by body is JSON encoded and included as the request body.
type ErrorResponse ¶
An ErrorResponse reports errors caused by an API request.
func (*ErrorResponse) Error ¶
func (r *ErrorResponse) Error() string
func (*ErrorResponse) HTTPStatusCode ¶
func (r *ErrorResponse) HTTPStatusCode() int
type ListOptions ¶
type ListOptions struct { PerPage int `url:",omitempty" json:",omitempty"` Page int `url:",omitempty" json:",omitempty"` }
ListOptions specifies general pagination options for fetching a list of results.
func (ListOptions) Offset ¶
func (o ListOptions) Offset() int
func (ListOptions) PageOrDefault ¶
func (o ListOptions) PageOrDefault() int
func (ListOptions) PerPageOrDefault ¶
func (o ListOptions) PerPageOrDefault() int
type MockPostsService ¶
type MockPostsService struct { Get_ func(id int) (*Post, error) List_ func(opt *PostListOptions) ([]*Post, error) Submit_ func(post *Post) (bool, error) }
func (*MockPostsService) List ¶
func (s *MockPostsService) List(opt *PostListOptions) ([]*Post, error)
type Post ¶
type Post struct { // ID a unique identifier for this post. ID int `json:",omitempty"` // Title of the post. Title string // LinkURL is the URL to a link that this post is about. LinkURL string // Body of the post. Body string // SubmittedAt is when the post was submitted. SubmittedAt time.Time // AuthorUserID is the user ID of this post's author. AuthorUserID int // Score in points. Score int // Classification is the output of the classifier on this post. Classification string }
A Post is a link and short body submitted to and displayed on thesrc.
type PostListOptions ¶
type PostListOptions struct { // CodeOnly filters the result set to only those posts whose links contain code. CodeOnly bool ListOptions }
type PostsService ¶
type PostsService interface { // Get a post. Get(id int) (*Post, error) // List posts. List(opt *PostListOptions) ([]*Post, error) // Submit a post. If this post's link URL has never been submitted, post.ID // will be a new ID, and created will be true. If it has been submitted // before, post.ID will be the ID of the previous post, and created will be // false. Submit(post *Post) (created bool, err error) }
PostsService interacts with the post-related endpoints in thesrc's API.