devto

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2019 License: MIT Imports: 11 Imported by: 4

Documentation

Overview

Package devto is a wrapper around dev.to REST API

Where programmers share ideas and help each other grow. It is an online community for sharing and discovering great ideas, having debates, and making friends. Anyone can share articles, questions, discussions, etc. as long as they have the rights to the words they are sharing. Cross-posting from your own blog is welcome.

See: https://docs.dev.to/api

Index

Constants

View Source
const (
	BaseURL      string = "https://dev.to"
	APIVersion   string = "0.5.1"
	APIKeyHeader string = "api-key"
)

Configuration constants

Variables

View Source
var (
	ErrMissingConfig     = errors.New("missing configuration")
	ErrProtectedEndpoint = errors.New("to use this resource you need to provide an authentication method")
)

devto client errors

View Source
var (
	ErrMissingRequiredParameter = errors.New("a required parameter is missing")
)

Configuration errors

Functions

This section is empty.

Types

type Article

type Article struct {
	TypeOf                 string     `json:"type_of,omitempty"`
	ID                     uint32     `json:"id,omitempty"`
	Title                  string     `json:"title,omitempty"`
	Description            string     `json:"description,omitempty"`
	CoverImage             *WebURL    `json:"cover_image,omitempty"`
	SocialImage            *WebURL    `json:"social_image,omitempty"`
	ReadablePublishDate    string     `json:"readable_publish_date"`
	Published              bool       `json:"published,omitempty"`
	PublishedAt            *time.Time `json:"published_at,omitempty"`
	CreatedAt              *time.Time `json:"created_at,omitempty"`
	EditedAt               *time.Time `json:"edited_at,omitempty"`
	CrossPostedAt          *time.Time `json:"crossposted_at,omitempty"`
	LastCommentAt          *time.Time `json:"last_comment_at,omitempty"`
	TagList                string     `json:"tag_list,omitempty"`
	Tags                   Tags       `json:"tags,omitempty"`
	Slug                   string     `json:"slug,omitempty"`
	Path                   *WebURL    `json:"path,omitempty"`
	URL                    *WebURL    `json:"url,omitempty"`
	CanonicalURL           *WebURL    `json:"canonical_url,omitempty"`
	CommentsCount          uint       `json:"comments_count,omitempty"`
	PositiveReactionsCount uint       `json:"positive_reactions_count,omitempty"`
	User                   User       `json:"user,omitempty"`
	BodyHTML               string     `json:"body_html,omitempty"`
	BodyMarkdown           string     `json:"body_markdown,omitempty"`
}

Article contains all the information related to a single information resource from devto.

type ArticleListOptions

type ArticleListOptions struct {
	Tags     string `url:"tag,omitempty"`
	Username string `url:"username,omitempty"`
	State    string `url:"state,omitempty"`
	Top      string `url:"top,omitempty"`
	Page     int    `url:"page,omitempty"`
}

ArticleListOptions holds the query values to pass as query string parameter to the Articles List action.

type ArticleUpdate

type ArticleUpdate struct {
	Title          string   `json:"title"`
	BodyMarkdown   string   `json:"body_markdown"`
	Published      bool     `json:"published"`
	Series         *string  `json:"series"`
	MainImage      string   `json:"main_image,omitempty"`
	CanonicalURL   string   `json:"canonical_url,omitempty"`
	Description    string   `json:"description,omitempty"`
	Tags           []string `json:"tags,omitempty"`
	OrganizationID int32    `json:"organization_id,omitempty"`
}

ArticleUpdate represents an update to an article; it is used as the payload in POST and PUT requests for writing articles.

type ArticlesResource

type ArticlesResource struct {
	API *Client
}

ArticlesResource implements the APIResource interface for devto articles.

func (*ArticlesResource) Find

func (ar *ArticlesResource) Find(ctx context.Context, id uint32) (Article, error)

Find will retrieve an Article matching the ID passed.

func (*ArticlesResource) List

List will return the articles uploaded to devto, the result can be narrowed down, filtered or enhanced using query parameters as specified on the documentation. See: https://docs.dev.to/api/#tag/articles/paths/~1articles/get

func (*ArticlesResource) ListAllMyArticles

func (ar *ArticlesResource) ListAllMyArticles(ctx context.Context, opts *MyArticlesOptions) ([]ListedArticle, error)

ListAllMyArticles lists all articles written by the user authenticated with this client, erroring if the caller is not authenticated. Articles in the response will be listed in reverse chronological order by their creation times, with unpublished articles listed before published articles.

If opts is nil, then no query parameters will be sent; the page number will be 1 and the page size will be 30 articles.

func (*ArticlesResource) ListForTag

func (ar *ArticlesResource) ListForTag(ctx context.Context, tag string, page int) ([]ListedArticle, error)

ListForTag is a convenience method for retrieving articles for a particular tag, calling the base List method.

func (*ArticlesResource) ListForUser

func (ar *ArticlesResource) ListForUser(ctx context.Context, username string, page int) ([]ListedArticle, error)

ListForUser is a convenience method for retrieving articles written by a particular user, calling the base List method.

func (*ArticlesResource) ListMyPublishedArticles

func (ar *ArticlesResource) ListMyPublishedArticles(ctx context.Context, opts *MyArticlesOptions) ([]ListedArticle, error)

ListMyPublishedArticles lists all published articles written by the user authenticated with this client, erroring if the caller is not authenticated. Articles in the response will be listed in reverse chronological order by their publication times.

If opts is nil, then no query parameters will be sent; the page number will be 1 and the page size will be 30 articles.

func (*ArticlesResource) ListMyUnpublishedArticles

func (ar *ArticlesResource) ListMyUnpublishedArticles(ctx context.Context, opts *MyArticlesOptions) ([]ListedArticle, error)

ListMyUnpublishedArticles lists all unpublished articles written by the user authenticated with this client, erroring if the caller is not authenticated. Articles in the response will be listed in reverse chronological order by their creation times.

If opts is nil, then no query parameters will be sent; the page number will be 1 and the page size will be 30 articles.

func (*ArticlesResource) New

New will create a new article on dev.to

func (*ArticlesResource) Update

func (ar *ArticlesResource) Update(ctx context.Context, u ArticleUpdate, id uint32) (Article, error)

Update will mutate the resource by id, and all the changes performed to the Article will be applied, thus validation on the API side.

type Client

type Client struct {
	Context    context.Context
	BaseURL    *url.URL
	HTTPClient httpClient
	Config     *Config
	Articles   *ArticlesResource
}

Client is the main data structure for performing actions against dev.to API

func NewClient

func NewClient(ctx context.Context, conf *Config, bc httpClient, bu string) (dev *Client, err error)

NewClient takes a context, a configuration pointer and optionally a base http client (bc) to build an Client instance.

func (*Client) NewRequest

func (c *Client) NewRequest(method string, uri string, body io.Reader) (*http.Request, error)

NewRequest build the request relative to the client BaseURL

type Config

type Config struct {
	APIKey       string
	InsecureOnly bool
}

Config contains the elements required to initialize a devto client.

func NewConfig

func NewConfig(p bool, k string) (c *Config, err error)

NewConfig build a devto configuration instance with the required parameters.

It takes a boolean (p) as first parameter to indicate if you need access to endpoints which require authentication, and a API key as second parameter, if p is set to true and you don't provide an API key, it will return an error.

type ErrorResponse

type ErrorResponse struct {
	ErrorMessage string `json:"error"`
	Status       int    `json:"status"`
}

ErrorResponse is an error returned from a dev.to API endpoint.

func (*ErrorResponse) Error

func (e *ErrorResponse) Error() string

type FlareTag

type FlareTag struct {
	Name         string `json:"name"`
	BGColorHex   string `json:"bg_color_hex"`
	TextColorHex string `json:"text_color_hex"`
}

FlareTag represents an article's flare tag, if the article has one.

type ListedArticle

type ListedArticle struct {
	TypeOf                 string        `json:"type_of,omitempty"`
	ID                     uint32        `json:"id,omitempty"`
	Title                  string        `json:"title,omitempty"`
	Description            string        `json:"description,omitempty"`
	CoverImage             *WebURL       `json:"cover_image,omitempty"`
	PublishedAt            *time.Time    `json:"published_at,omitempty"`
	PublishedTimestamp     string        `json:"published_timestamp,omitempty"`
	TagList                Tags          `json:"tag_list,omitempty"`
	Slug                   string        `json:"slug,omitempty"`
	Path                   string        `json:"path,omitempty"`
	URL                    *WebURL       `json:"url,omitempty"`
	CanonicalURL           *WebURL       `json:"canonical_url,omitempty"`
	CommentsCount          uint          `json:"comments_count,omitempty"`
	PositiveReactionsCount uint          `json:"positive_reactions_count,omitempty"`
	User                   User          `json:"user,omitempty"`
	Organization           *Organization `json:"organization,omitempty"`
	FlareTag               *FlareTag     `json:"flare_tag,omitempty"`
	// Only present in "/articles/me/*" endpoints
	BodyMarkdown string `json:"body_markdown,omitempty"`
	Published    bool   `json:"published,omitempty"`
}

ListedArticle represents an article returned from one of the list articles endpoints (/articles, /articles/me/*).

func (*ListedArticle) UnmarshalJSON

func (a *ListedArticle) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the JSON Unmarshaler interface.

type MyArticlesOptions

type MyArticlesOptions struct {
	Page    int `url:"page,omitempty"`
	PerPage int `url:"per_page,omitempty"`
}

MyArticlesOptions defines pagination options used as query params in the dev.to "list my articles" endpoints.

type Organization

type Organization struct {
	Name           string  `json:"name,omitempty"`
	Username       string  `json:"username,omitempty"`
	Slug           string  `json:"slug,omitempty"`
	ProfileImage   *WebURL `json:"profile_image,omitempty"`
	ProfileImage90 *WebURL `json:"profile_image_90,omitempty"`
}

Organization describes a company or group that publishes content to devto.

type Tags

type Tags []string

Tags are a group of topics related to an article

type User

type User struct {
	Name            string  `json:"name,omitempty"`
	Username        string  `json:"username,omitempty"`
	TwitterUsername string  `json:"twitter_username,omitempty"`
	GithubUsername  string  `json:"github_username,omitempty"`
	WebsiteURL      *WebURL `json:"website_url,omitempty"`
	ProfileImage    *WebURL `json:"profile_image,omitempty"`
	ProfileImage90  *WebURL `json:"profile_image_90,omitempty"`
}

User contains information about a devto account

type WebURL

type WebURL struct {
	*url.URL
}

WebURL is a class embed to override default unmarshal behavior.

func (*WebURL) UnmarshalJSON

func (s *WebURL) UnmarshalJSON(b []byte) error

UnmarshalJSON overrides the default unmarshal behaviour for URL

Jump to

Keyboard shortcuts

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