ospry-go: github.com/ospry/ospry-go Index | Files | Directories

package ospry

import "github.com/ospry/ospry-go"

Package ospry provides bindings for ospry's image hosting api (see https://ospry.io).

If you're writing a web service, images will generally be uploaded directly to ospry from the browser via ospry.js (with your public key), but you'll keep track of them server-side. Server-side operations should be done with your secret key:

ospry.SetKey("sk-test-********")

If you've turned the claiming feature on in your account settings (recommended), then you'll need to claim the images after your client uploads them and sends the resulting ids to your server:

metadata, err := ospry.Claim(id)

Once you have claimed the images, you can retrieve their metadata, change their permissions and delete them as needed.

metadata, err := ospry.GetMetadata(id)
metadata, err := ospry.MakePrivate(id)
metadata, err := ospry.MakePublic(id)
err := ospry.Delete(id)

To give access to private images to someone that doesn't have your secret key (i.e your js client running in the browser), you can use FormatURL to sign the urls by providing an expiration time.

url, err := ospry.FormatURL(image.URL, &RenderOpts{
  TimeExpired: time.Now().Add(5*time.Minute),
})

Image data can be uploaded and downloaded server-side too if you want:

metadata, err := ospry.UploadPublic("foo.jpg", fooReader)
metadata, err := ospry.UploadPrivate("bar.jpg", barReader)
readCloser, err := ospry.Download(metadata.URL, &RenderOpts{MaxHeight: 400})

Remember to close any ReadClosers you get from Download once you're done reading.

Index

Package Files

ospry.go

Variables

var (
    Formats       = []string{"jpeg", "png", "gif"}
    DefaultClient = New("")
)

func Delete Uses

func Delete(id string) error

Delete calls Delete on the default client.

func Download Uses

func Download(url string, opts *RenderOpts) (io.ReadCloser, error)

Download calls Download on the default client.

func FormatURL Uses

func FormatURL(urlstr string, opts *RenderOpts) (string, error)

FormatURL calls FormatURL on the default client.

func SetKey Uses

func SetKey(key string)

SetKey changes the api key used by the default client.

type Client Uses

type Client struct {
    Key        string
    ServerURL  string
    HTTPClient *http.Client
}

A Client performs authenticated API calls.

func New Uses

func New(key string) *Client

New creates a client that authenticates with the given key. By default, the client's HTTPClient is http.DefaultClient.

func (*Client) Claim Uses

func (c *Client) Claim(id string) (*Metadata, error)

Claim claims ownership of an image that was uploaded client-side. You need to claim images to prevent them from disappearing (if you've turned claiming on in your account settings).

func (*Client) Delete Uses

func (c *Client) Delete(id string) error

Delete deletes an image. Attempts to retrieve images that have been deleted will result in 404s.

func (*Client) Download Uses

func (c *Client) Download(urlstr string, opts *RenderOpts) (io.ReadCloser, error)

Download retrieves the image data at the given url. You can render a modified image by providing a non-nil RenderOpts.

func (*Client) FormatURL Uses

func (c *Client) FormatURL(urlstr string, opts *RenderOpts) (string, error)

FormatURL modifies an image url to produce a url that can be used to download a modified image (e.g. resized). If TimeExpired is given, the url is signed with the client's key and can be used to download access a private image until TimeExpired has past. An error is returned if the given url is invalid.

func (*Client) GetMetadata Uses

func (c *Client) GetMetadata(id string) (*Metadata, error)

GetMetadata retrieves the metadata for the image with the given id.

func (*Client) MakePrivate Uses

func (c *Client) MakePrivate(id string) (*Metadata, error)

MakePrivate makes an image an private if it isn't already. Private images can be downloaded by anyone who has an unexpired, signed url to that image (see FormatURL).

func (*Client) MakePublic Uses

func (c *Client) MakePublic(id string) (*Metadata, error)

MakePublic makes an image public if it isn't already. Public images can be downloaded by anyone who has the url to that image.

func (*Client) UploadPrivate Uses

func (c *Client) UploadPrivate(filename string, data io.Reader) (*Metadata, error)

UploadPrivate uploads a private image with the given filename. The image will be automatically claimed if the client was initialized with your secret key.

func (*Client) UploadPublic Uses

func (c *Client) UploadPublic(filename string, data io.Reader) (*Metadata, error)

UploadPublic uploads a public image with the given filename. The image will be automatically claimed if the client was initialized with your secret key.

type Error Uses

type Error struct {
    HTTPStatusCode int    `json:"httpStatusCode"`
    Cause          string `json:"cause"`
    Message        string `json:"message"`
}

func (*Error) Error Uses

func (e *Error) Error() string

type Metadata Uses

type Metadata struct {
    ID          string    `json:"id"`
    URL         string    `json:"url"`
    HTTPSURL    string    `json:"httpsURL"`
    TimeCreated time.Time `json:"timeCreated"`
    IsClaimed   bool      `json:"isClaimed"`
    IsPrivate   bool      `json:"isPrivate"`
    Filename    string    `json:"filename"`
    Format      string    `json:"format"`
    Size        int64     `json:"size"`
    Height      int       `json:"height"`
    Width       int       `json:"width"`
}

func Claim Uses

func Claim(id string) (*Metadata, error)

Claim calls Claim on the default client.

func GetMetadata Uses

func GetMetadata(id string) (*Metadata, error)

GetMetadata calls GetMetadata on the default client.

func MakePrivate Uses

func MakePrivate(id string) (*Metadata, error)

MakePrivate calls MakePrivate on the default client.

func MakePublic Uses

func MakePublic(id string) (*Metadata, error)

MakePublic calls MakePublic on the default client.

func UploadPrivate Uses

func UploadPrivate(filename string, data io.Reader) (*Metadata, error)

UploadPrivate calls UploadPrivate on the default client.

func UploadPublic Uses

func UploadPublic(filename string, data io.Reader) (*Metadata, error)

UploadPublic calls UploadPublic on the default client.

type RenderOpts Uses

type RenderOpts struct {
    Format      string
    MaxHeight   int
    MaxWidth    int
    TimeExpired time.Time
}

Directories

PathSynopsis
example

Package ospry imports 11 packages (graph) and is imported by 1 packages. Updated 2016-07-19. Refresh now. Tools for package owners.