goprsc

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2021 License: MIT Imports: 8 Imported by: 2

README

Goprsc

Goprsc is a GO client library for accessing the Postfix Rest Server V1 API.

Goprsc currently implements all Postfix Rest Server APIs (domains, accounts, aliases, automatic sender and recipient BCC).

Usage

import "github.com/lyubenblagoev/goprsc"

Create a new client and use the exposed services to access different parts of the Postfix Rest Server API.

Use the DefaultClient:

client := goprsc.DefaultClient

Create a new Client using NewClient(*http.Client):

var httpClient *http.Client

...

client := goprsc.NewClient(httpClient)

Create a new client using NewClientWithOptions(*http.Client, ...ClientOption):

client := goprsc.NewClientWithOptions(nil, HTTPSProtocolOption())

Client options allow changing the default protocol, host, port and user agent string using HTTPSProtocolOption(), HostOption(), PortOption() and UserAgentOption() functions. These functions return a ClientOption which changes the corresponding option in the client.

Examples

To create a new domain:

client := goprsc.DefaultClient

domainName := "example.com"

if err := client.Domains.Create(domainName); err != nil {
    fmt.Printf("Unable to create domain %s\n\n", domainName)
    return err
}

To get a list of all accounts in a domain:


client := goprsc.DefaultClient

domainName := "example.com"

accounts, err := client.Accounts.List(domainName)
if err != nil {
    fmt.Printf("Unable to list accounts for domain %s\n\n", domainName)
    return err
}

Similarly you can manage other entities.

For more usage examples, you may wish to take a look at emailctl (a CLI for the Postfix Rest Server).

Documentation

Overview

Package goprsc is the Postfix REST Server API client for GO.

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = NewClient(nil)

DefaultClient is the default Client that works with the default HTTP client and connects to port 8080 on localhost. Use this one if you don't need specific http.Client implementation, protocol, host/IP or port number.

Functions

This section is empty.

Types

type Account

type Account struct {
	ID       int      `json:"id"`
	Username string   `json:"username"`
	Domain   string   `json:"domain"`
	DomainID int      `json:"domainId"`
	Enabled  bool     `json:"enabled"`
	Created  DateTime `json:"created"`
	Updated  DateTime `json:"updated"`
}

Account is an instance of a account (an email address)

type AccountService

type AccountService service

AccountService handles communication with the account APIs in the Postfix REST Server.

func (*AccountService) Create

func (s *AccountService) Create(domain, username, password string) error

Create creates a new account with the given username in the given domain.

func (*AccountService) Delete

func (s *AccountService) Delete(domain, username string) error

Delete removes the account specified with the given domain and username

func (*AccountService) Get

func (s *AccountService) Get(domain, username string) (*Account, error)

Get returns the account with the given username on the given domain.

func (*AccountService) List

func (s *AccountService) List(domain string) ([]Account, error)

List makes a GET request for all registered accounts in the specified domain.

func (*AccountService) Update

func (s *AccountService) Update(domain, username string, updateRequest *AccountUpdateRequest) error

Update updates the specified account.

type AccountUpdateRequest

type AccountUpdateRequest struct {
	Username        string `json:"username,omitempty"`
	Password        string `json:"password,omitempty"`
	ConfirmPassword string `json:"confirmPassword,omitempty"`
	Enabled         bool   `json:"enabled"`
}

AccountUpdateRequest is a data structure that carries account update information

type Alias

type Alias struct {
	ID      int      `json:"id"`
	Name    string   `json:"name"`
	Email   string   `json:"email"`
	Enabled bool     `json:"enabled"`
	Created DateTime `json:"created"`
	Updated DateTime `json:"updated"`
}

Alias is an email address alias.

type AliasService

type AliasService service

AliasService handles communication with the alias APIs in the Postfix REST Server.

func (*AliasService) Create

func (s *AliasService) Create(domain, alias, email string) error

Create makes a POST request to create a new alias.

func (*AliasService) Delete

func (s *AliasService) Delete(domain, alias, email string) error

Delete removes an alias.

func (*AliasService) Get

func (s *AliasService) Get(domain, alias string) ([]Alias, error)

Get retrieves information for an alias.

func (*AliasService) GetForEmail

func (s *AliasService) GetForEmail(domain, alias, email string) (*Alias, error)

GetForEmail retrieves an alias for specific account and target email.

func (*AliasService) List

func (s *AliasService) List(domain string) ([]Alias, error)

List makes a GET request for all aliases for the given domain.

func (*AliasService) Update

func (s *AliasService) Update(domain, alias, email string, ur *AliasUpdateRequest) error

Update makes a PUT request and updates the specified alias.

type AliasUpdateRequest

type AliasUpdateRequest struct {
	Name    string `json:"name,omitempty"`
	Email   string `json:"email,omitempty"`
	Enabled bool   `json:"enabled"`
}

AliasUpdateRequest carries alias update information.

type AuthResponse

type AuthResponse struct {
	AuthToken    string `json:"token"`
	RefreshToken string `json:"refreshToken"`
}

AuthResponse represents a successfull authentication response

type AuthService

type AuthService service

AuthService handles communication with the authentication APIs in the Postfix Rest Server.

func (*AuthService) Login

func (s *AuthService) Login(login, password string) (*AuthResponse, error)

Login makes a post request to the API for logging in

func (*AuthService) Logout

func (s *AuthService) Logout(login, refreshToken string) error

Logout makes a post request to the API for logging out

type Bcc

type Bcc struct {
	ID        int      `json:"id"`
	AccountID int      `json:"accountId"`
	Email     string   `json:"email"`
	Enabled   bool     `json:"enabled"`
	Created   DateTime `json:"created"`
	Updated   DateTime `json:"updated"`
}

Bcc is a blind carbon copy for specific account

type BccService

type BccService interface {
	// Get makes a GET request and fetches the specified BCC.
	Get(domain, account string) (*Bcc, error)

	// Create makes a POST request to create a new BCC.
	Create(domain, account, email string) error

	// Update makes a PUT request to update the specified BCC.
	Update(domain, account string, ur *BccUpdateRequest) error

	// Delete removes a BCC.
	Delete(domain, account string) error
}

type BccUpdateRequest

type BccUpdateRequest struct {
	Email   string `json:"email,omitempty"`
	Enabled bool   `json:"enabled"`
}

BccUpdateRequest carrires BCC update information.

type Client

type Client struct {

	// The protocol used for API requests (defaults to http).
	Protocol string

	// The host to connect to (defaults to localhost).
	Host string

	// The port on which the client should connect to the server (defaults to 8080).
	Port string

	// UserAgent is the client user agent
	UserAgent string

	// Login represents the username of the logged in user
	Login string

	// The authentication token
	AuthToken string

	// The refresh token for retrieving new authentication token
	RefreshToken string

	// Auth is the service used for communication with the authentication API.
	Auth *AuthService

	// Domains is the service used for communication with the domains API.
	Domains *DomainService

	// Accounts is the service used for communication with the accounts API.
	Accounts *AccountService

	// Aliases is the service used for communication with the aliases API.
	Aliases *AliasService

	// OutputBccs is the service used for communication with the output BCC API.
	OutputBccs *OutgoingBccService

	// InputBccs is the service used for communication with the input BCC API.
	InputBccs *IncomingBccService
	// contains filtered or unexported fields
}

Client manages communication with the Postfix REST Server API.

func NewClient

func NewClient(httpClient *http.Client) *Client

NewClient returns a new Postfix REST Server API client.

func NewClientWithOptions

func NewClientWithOptions(httpClient *http.Client, options ...ClientOption) (*Client, error)

NewClientWithOptions returns a new client with the given ClientOptions applied.

func (*Client) Do

func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error)

Do sends a request and returns an API response. The respose is JSON decoded and stored in the value pointed to by v.

func (*Client) NewRequest

func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error)

NewRequest creates an API request. An URL relative to the API version path must be provided in urlStr.

type ClientOption

type ClientOption func(*Client) error

ClientOption is an option to the client that can be passed to NewClientWithOptions().

func AuthOption

func AuthOption(login, authToken, refreshToken string) ClientOption

AuthOption is a client option for setting the authentication tokens.

func HTTPSProtocolOption

func HTTPSProtocolOption() ClientOption

HTTPSProtocolOption is a client option for using the HTTPS protocol.

func HostOption

func HostOption(host string) ClientOption

HostOption is a client option for setting the hostname or IP address of the server.

func PortOption

func PortOption(port string) ClientOption

PortOption is a client option for setting the port number on which the server is listening.

func UserAgentOption

func UserAgentOption(userAgent string) ClientOption

UserAgentOption is a client option for setting the user agent.

type DateTime

type DateTime struct {
	time.Time
}

DateTime represents a time that can be unmarshaled from a JSON string formatted as "yyyy-mm-ddThh:mm:ss+|-hhmm" (e.g. '2017-01-02T15:47:59+0100'). All exported methods of time.Time can be called on DateTime.

func (DateTime) Equal

func (t DateTime) Equal(u DateTime) bool

Equal reports whether t and u are equal based on time.Equal().

func (DateTime) String

func (t DateTime) String() string

func (*DateTime) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface.

type Domain

type Domain struct {
	ID      int      `json:"id"`
	Name    string   `json:"name"`
	Enabled bool     `json:"enabled"`
	Created DateTime `json:"created"`
	Updated DateTime `json:"updated"`
}

Domain represents a domain (e.g. example.com)

type DomainService

type DomainService service

DomainService handles communiation with the domain APIs in the Postfix REST Server.

func (*DomainService) Create

func (s *DomainService) Create(domain string) error

Create makes a POST request to the API to create a new domain.

func (*DomainService) Delete

func (s *DomainService) Delete(name string) error

Delete makes a DELETE request to the API to delete the specified domain

func (*DomainService) Get

func (s *DomainService) Get(domain string) (*Domain, error)

Get makes a GET request for a specific domain specified with the domain parameter.

func (*DomainService) List

func (s *DomainService) List() ([]Domain, error)

List makes a GET request for all registered domains.

func (*DomainService) Update

func (s *DomainService) Update(name string, updateRequest *DomainUpdateRequest) error

Update makes a PUT request to update domain parameters

type DomainUpdateRequest

type DomainUpdateRequest struct {
	Name    string `json:"name,omitempty"`
	Enabled bool   `json:"enabled"`
}

DomainUpdateRequest represents a request for domain update

type ErrorResponse

type ErrorResponse struct {
	// The HTTP response
	Response *http.Response

	// Message is the error message received as response to the API request.
	Message string `json:"message"`

	// Path is the URL of the request.
	Path string `json:"path"`

	// Method is the HTTP method of the request.
	Method string `json:"method"`
}

ErrorResponse represents an error caused by an API request.

func (ErrorResponse) Error

func (e ErrorResponse) Error() string

type IncomingBccService

type IncomingBccService struct {
	// contains filtered or unexported fields
}

IncomingBccService handles communication with the incoming BCC APIs in the Postfix REST Server.

func NewIncomingBccService

func NewIncomingBccService(c *Client) *IncomingBccService

NewIncomingBccService creates a new IncomingBccService instance.

func (IncomingBccService) Create

func (s IncomingBccService) Create(domain, account, email string) error

Create makes a POST request to create a new BCC.

func (IncomingBccService) Delete

func (s IncomingBccService) Delete(domain, account string) error

Delete removes a BCC.

func (IncomingBccService) Get

func (s IncomingBccService) Get(domain, account string) (*Bcc, error)

Get makes a GET request and fetches the specified BCC.

func (IncomingBccService) Update

func (s IncomingBccService) Update(domain, account string, ur *BccUpdateRequest) error

Update makes a PUT request to update the specified BCC.

type LoginRequest

type LoginRequest struct {
	Login    string `json:"login"`
	Password string `json:"password"`
}

LoginRequest represents a request for logging in

type LogoutRequest

type LogoutRequest struct {
	Login        string `json:"login"`
	RefreshToken string `json:"refreshToken"`
}

LogoutRequest representes a request for logging out

type OutgoingBccService

type OutgoingBccService struct {
	// contains filtered or unexported fields
}

OutgoingBccService handles communication with the outgoing BCC APIs in the Postfix REST Server.

func NewOutgoingBccService

func NewOutgoingBccService(c *Client) *OutgoingBccService

NewOutgoingBccService creates a new OutgoingBccService instance.

func (OutgoingBccService) Create

func (s OutgoingBccService) Create(domain, account, email string) error

Create makes a POST request to create a new BCC.

func (OutgoingBccService) Delete

func (s OutgoingBccService) Delete(domain, account string) error

Delete removes a BCC.

func (OutgoingBccService) Get

func (s OutgoingBccService) Get(domain, account string) (*Bcc, error)

Get makes a GET request and fetches the specified BCC.

func (OutgoingBccService) Update

func (s OutgoingBccService) Update(domain, account string, ur *BccUpdateRequest) error

Update makes a PUT request to update the specified BCC.

type RefreshTokenRequest

type RefreshTokenRequest struct {
	Login        string `json:"login"`
	RefreshToken string `json:"refreshToken"`
}

RefreshTokenRequest represents a request for retrieving a new access token

Jump to

Keyboard shortcuts

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