writeas

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 8 Imported by: 6

README

go-writeas

godoc

Official Write.as Go client library.

Installation

go get github.com/writeas/go-writeas/v2

Documentation

See all functionality and usages in the API documentation.

Example usage
import "github.com/writeas/go-writeas/v2"

func main() {
	// Create the client
	c := writeas.NewClient()

	// Publish a post
	p, err := c.CreatePost(&writeas.PostParams{
		Title:   "Title!",
		Content: "This is a post.",
		Font:    "sans",
	})
	if err != nil {
		// Perhaps show err.Error()
	}

	// Save token for later, since it won't ever be returned again
	token := p.Token

	// Update a published post
	p, err = c.UpdatePost(p.ID, token, &writeas.PostParams{
		Content: "Now it's been updated!",
	})
	if err != nil {
		// handle
	}

	// Get a published post
	p, err = c.GetPost(p.ID)
	if err != nil {
		// handle
	}

	// Delete a post
	err = c.DeletePost(p.ID, token)
}

Contributing

The library covers our usage, but might not be comprehensive of the API. So we always welcome contributions and improvements from the community. Before sending pull requests, make sure you've done the following:

  • Run goimports on all updated .go files.
  • Document all exported structs and funcs.

License

MIT

Documentation

Overview

Package writeas provides the binding for the Write.as API

Index

Examples

Constants

View Source
const (
	TypePost            PostType = "post"
	TypePrompt                   = "prompt"
	TypePromptArchive            = "prompt-arch"
	TypeSubmission               = "submission"
	TypeSubmissionDraft          = "submission-draft"
)
View Source
const (

	// Current go-writeas version
	Version = "2"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthUser

type AuthUser struct {
	AccessToken string `json:"access_token,omitempty"`
	Password    string `json:"password,omitempty"`
	User        *User  `json:"user"`
}

AuthUser represents a just-authenticated user. It contains information that'll only be returned once (now) per user session.

type Author added in v2.1.0

type Author struct {
	User *User
	Name string `json:"name"`
	Slug string `json:"slug"`
}

Author represents a Write.as author.

type AuthorParams added in v2.1.0

type AuthorParams struct {
	// Name is the public display name of the Author.
	Name string `json:"name"`

	// Slug is the optional slug for the Author.
	Slug string `json:"slug"`

	// OrgAlias is the alias of the organization the Author belongs to.
	OrgAlias string `json:"-"`
}

AuthorParams are used to create or update a Write.as author.

type BatchPostResult

type BatchPostResult struct {
	ID           string `json:"id,omitempty"`
	Code         int    `json:"code,omitempty"`
	ErrorMessage string `json:"error_msg,omitempty"`
}

BatchPostResult contains the post-specific result as part of a larger batch operation.

type BodyResponse added in v2.0.3

type BodyResponse struct {
	Body string `json:"body"`
}

type Category added in v2.1.0

type Category struct {
	Hashtag string `json:"hashtag"`
	Slug    string `json:"slug"`
	Title   string `json:"title"`
}

Category represents a post tag with additional metadata, like a title and slug.

type ClaimPostResult

type ClaimPostResult struct {
	ID           string `json:"id,omitempty"`
	Code         int    `json:"code,omitempty"`
	ErrorMessage string `json:"error_msg,omitempty"`
	Post         *Post  `json:"post,omitempty"`
}

ClaimPostResult contains the post-specific result for a request to associate a post to an account.

type Client

type Client struct {

	// UserAgent overrides the default User-Agent header
	UserAgent string
	// contains filtered or unexported fields
}

Client is used to interact with the Write.as API. It can be used to make authenticated or unauthenticated calls.

func NewClient

func NewClient() *Client

NewClient creates a new API client. By default, all requests are made unauthenticated. To optionally make authenticated requests, call `SetToken`.

c := writeas.NewClient()
c.SetToken("00000000-0000-0000-0000-000000000000")

func NewClientWith

func NewClientWith(c Config) *Client

NewClientWith builds a new API client with the provided configuration.

func NewDevClient

func NewDevClient() *Client

NewDevClient creates a new API client for development and testing. It'll communicate with our development servers, and SHOULD NOT be used in production.

func NewTorClient

func NewTorClient(port int) *Client

NewTorClient creates a new API client for communicating with the Write.as Tor hidden service, using the given port to connect to the local SOCKS proxy.

func (*Client) BaseURL added in v2.1.0

func (c *Client) BaseURL() string

BaseURL returns the base API URL the Client will make calls against.

func (*Client) ClaimPosts

func (c *Client) ClaimPosts(sp *[]OwnedPostParams) (*[]ClaimPostResult, error)

ClaimPosts associates anonymous posts with a user / account. https://developers.write.as/docs/api/#claim-posts.

func (*Client) CreateCollection

func (c *Client) CreateCollection(sp *CollectionParams) (*Collection, error)

CreateCollection creates a new collection, returning a user-friendly error if one comes up. Requires a Write.as subscription. See https://developers.write.as/docs/api/#create-a-collection

func (*Client) CreateContributor added in v2.1.0

func (c *Client) CreateContributor(sp *AuthorParams) (*Author, error)

CreateContributor creates a new contributor on the given organization.

func (*Client) CreatePost

func (c *Client) CreatePost(sp *PostParams) (*Post, error)

CreatePost publishes a new post, returning a user-friendly error if one comes up. See https://developers.write.as/docs/api/#publish-a-post.

Example
dwac := NewDevClient()

// Publish a post
p, err := dwac.CreatePost(&PostParams{
	Title:   "Title!",
	Content: "This is a post.",
	Font:    "sans",
})
if err != nil {
	fmt.Printf("Unable to create: %v", err)
	return
}

fmt.Printf("%s", p.Content)
Output:

This is a post.

func (*Client) DeleteCollection

func (c *Client) DeleteCollection(alias string) error

DeleteCollection permanently deletes a collection and makes any posts on it anonymous.

See https://developers.write.as/docs/api/#delete-a-collection.

func (*Client) DeletePost

func (c *Client) DeletePost(id, token string) error

DeletePost permanently deletes a published post. See https://developers.write.as/docs/api/#delete-a-post.

func (*Client) GetCollection

func (c *Client) GetCollection(alias string) (*Collection, error)

GetCollection retrieves a collection, returning the Collection and any error (in user-friendly form) that occurs. See https://developers.write.as/docs/api/#retrieve-a-collection

func (*Client) GetCollectionPost

func (c *Client) GetCollectionPost(alias, slug string) (*Post, error)

GetCollectionPost retrieves a post from a collection and any error (in user-friendly form) that occurs). See https://developers.write.as/docs/api/#retrieve-a-collection-post

func (*Client) GetCollectionPosts

func (c *Client) GetCollectionPosts(alias string) (*[]Post, error)

GetCollectionPosts retrieves a collection's posts, returning the Posts and any error (in user-friendly form) that occurs. See https://developers.write.as/docs/api/#retrieve-collection-posts

func (*Client) GetMe added in v2.1.0

func (c *Client) GetMe(verbose bool) (*User, error)

GetMe retrieves the authenticated User's information. See: https://developers.write.as/docs/api/#retrieve-authenticated-user

func (*Client) GetPost

func (c *Client) GetPost(id string) (*Post, error)

GetPost retrieves a published post, returning the Post and any error (in user-friendly form) that occurs. See https://developers.write.as/docs/api/#retrieve-a-post.

func (*Client) GetUserCollections

func (c *Client) GetUserCollections() (*[]Collection, error)

GetUserCollections retrieves the authenticated user's collections. See https://developers.write.as/docs/api/#retrieve-user-39-s-collections

func (*Client) GetUserPosts

func (c *Client) GetUserPosts() (*[]Post, error)

GetUserPosts retrieves the authenticated user's posts. See https://developers.write.as/docs/api/#retrieve-user-39-s-posts

func (*Client) LogIn

func (c *Client) LogIn(username, pass string) (*AuthUser, error)

LogIn authenticates a user with Write.as. See https://developers.write.as/docs/api/#authenticate-a-user

func (*Client) LogOut

func (c *Client) LogOut() error

LogOut logs the current user out, making the Client's current access token invalid.

func (*Client) Markdown added in v2.0.3

func (c *Client) Markdown(body, collectionURL string) (string, error)

Markdown takes raw Markdown and renders it into usable HTML. See https://developers.write.as/docs/api/#render-markdown.

func (*Client) PinPost

func (c *Client) PinPost(alias string, pp *PinnedPostParams) error

PinPost pins a post in the given collection. See https://developers.write.as/docs/api/#pin-a-post-to-a-collection

func (*Client) SetApplicationKey added in v2.1.0

func (c *Client) SetApplicationKey(key string)

SetApplicationKey sets an application-level API key for all Client requests.

func (*Client) SetClient added in v2.1.0

func (c *Client) SetClient(cl *http.Client)

SetClient sets a custom http.Client to use instead of the default.

func (*Client) SetToken

func (c *Client) SetToken(token string)

SetToken sets the user token for all future Client requests. Setting this to an empty string will change back to unauthenticated requests.

func (*Client) Token

func (c *Client) Token() string

Token returns the user token currently set to the Client.

func (*Client) UnpinPost

func (c *Client) UnpinPost(alias string, pp *PinnedPostParams) error

UnpinPost unpins a post from the given collection. See https://developers.write.as/docs/api/#unpin-a-post-from-a-collection

func (*Client) UpdatePost

func (c *Client) UpdatePost(id, token string, sp *PostParams) (*Post, error)

UpdatePost updates a published post with the given PostParams. See https://developers.write.as/docs/api/#update-a-post.

type Collection

type Collection struct {
	Alias       string `json:"alias"`
	Title       string `json:"title"`
	Description string `json:"description"`
	StyleSheet  string `json:"style_sheet"`
	Private     bool   `json:"private"`
	Views       int64  `json:"views"`
	Domain      string `json:"domain,omitempty"`
	Email       string `json:"email,omitempty"`
	URL         string `json:"url,omitempty"`

	TotalPosts int `json:"total_posts"`

	Posts *[]Post `json:"posts,omitempty"`
}

Collection represents a collection of posts. Blogs are a type of collection on Write.as.

type CollectionParams

type CollectionParams struct {
	Alias       string `json:"alias"`
	Title       string `json:"title"`
	Description string `json:"description,omitempty"`
}

CollectionParams holds values for creating a collection.

type Config

type Config struct {
	// URL of the Write.as API service. Defaults to https://write.as/api.
	URL string

	// If specified, the API client will communicate with the Write.as Tor
	// hidden service using the provided port to connect to the local SOCKS
	// proxy.
	TorPort int

	// If specified, requests will be authenticated using this user token.
	// This may be provided after making a few anonymous requests with
	// SetToken.
	Token string
}

Config configures a Write.as client.

type OrgMember added in v2.1.0

type OrgMember struct {
	Author
	Email string `json:"email"`
	Role  Role   `json:"role"`
}

OrgMember represents a member of an Organization

type OrgMemberParams added in v2.1.0

type OrgMemberParams struct {
	AuthorParams
	Username string `json:"username"`
	Email    string `json:"email"`
	Role     Role   `json:"role"`
}

OrgMemberParams are parameters for creating or updating an OrgMember.

type OwnedPostParams

type OwnedPostParams struct {
	ID    string `json:"id"`
	Token string `json:"token,omitempty"`
}

OwnedPostParams are, together, fields only the original post author knows.

type PinnedPostParams

type PinnedPostParams struct {
	ID       string `json:"id"`
	Position int    `json:"position"`
}

PinnedPostParams holds values for pinning a post

type Post

type Post struct {
	ID        string    `json:"id"`
	Slug      string    `json:"slug"`
	Token     string    `json:"token"`
	Font      string    `json:"appearance"`
	Language  *string   `json:"language"`
	RTL       *bool     `json:"rtl"`
	Listed    bool      `json:"listed"`
	Type      PostType  `json:"type"`
	Created   time.Time `json:"created"`
	Updated   time.Time `json:"updated"`
	Title     string    `json:"title"`
	Content   string    `json:"body"`
	Views     int64     `json:"views"`
	Tags      []string  `json:"tags"`
	Images    []string  `json:"images"`
	OwnerName string    `json:"owner,omitempty"`

	Collection *Collection `json:"collection,omitempty"`
}

Post represents a published Write.as post, whether anonymous, owned by a user, or part of a collection.

type PostParams

type PostParams struct {
	// Parameters only for updating
	ID    string `json:"-"`
	Token string `json:"token,omitempty"`

	// Parameters for creating or updating
	Slug     string     `json:"slug"`
	Created  *time.Time `json:"created,omitempty"`
	Updated  *time.Time `json:"updated,omitempty"`
	Title    string     `json:"title,omitempty"`
	Content  string     `json:"body,omitempty"`
	Font     string     `json:"font,omitempty"`
	IsRTL    *bool      `json:"rtl,omitempty"`
	Language *string    `json:"lang,omitempty"`

	AuthorSlug *string    `json:"author,omitempty"`
	Categories []Category `json:"categories,omitempty"`

	// Parameters only for creating
	Crosspost []map[string]string `json:"crosspost,omitempty"`

	// Parameters for collection posts
	Collection string `json:"-"`
}

PostParams holds values for creating or updating a post.

type PostType added in v2.0.3

type PostType string

type Role added in v2.1.0

type Role string

Role is an OrgMember's role.

const (
	RoleAdmin  Role = "admin"
	RoleEditor Role = "editor"
	RoleAuthor Role = "author"
)

type User

type User struct {
	Username string    `json:"username"`
	Email    string    `json:"email"`
	Created  time.Time `json:"created"`

	// Optional properties
	Subscription *UserSubscription `json:"subscription"`
}

User represents a registered Write.as user.

type UserSubscription

type UserSubscription struct {
	Name       string    `json:"name"`
	Begin      time.Time `json:"begin"`
	End        time.Time `json:"end"`
	AutoRenew  bool      `json:"auto_renew"`
	Active     bool      `json:"is_active"`
	Delinquent bool      `json:"is_delinquent"`
}

UserSubscription contains information about a user's Write.as subscription.

Jump to

Keyboard shortcuts

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