api

package module
v0.0.0-...-ab1cd13 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2022 License: MIT Imports: 9 Imported by: 0

README

Go Reference

Sample API library for speedata Publisher PDF generation service // Go

This Go library connects to the speedata api and gets PDF from the server. See https://doc.speedata.de/publisher/en/saasapi/ for a description of the API.

Example usage

package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"time"

	"github.com/BurntSushi/toml"
	api "github.com/speedata/publisher-api"
)

type config struct {
	ServerAddress string
	Username      string
}

func dothings() error {
	var err error
	cfg := config{}
	_, err = toml.DecodeFile("clientconfig.toml", &cfg)
	if err != nil {
		log.Fatal(err)
	}

	ep, err := api.NewEndpoint(cfg.Username, cfg.ServerAddress)
	if err != nil {
		return err
	}
	p := ep.NewPublishRequest()

	// If you need a different specific version, you can check the availability
	// here:
	// versions, err := ep.AvailableVersions()
	// if err != nil {
	//    return err
	// }
	// and set the required version
	// p.Version = versions[0]

	p.AttachFile(filepath.Join("sample", "layout.xml"))
	p.AttachFile(filepath.Join("sample", "data.xml"))
	fmt.Println("-> Now sending data to the server")
	resp, err := ep.Publish(p)
	if err != nil {
		return err
	}

	fmt.Println("-> Getting the status of our publishing run")
	ps, err := resp.Status()
	if err != nil {
		return err
	}
	if ps.Finished != nil {
		fmt.Println("PDF done", ps.Errors, "errors occured")
		for _, e := range ps.Errormessages {
			fmt.Println("*  message", e.Error)
		}
	} else {
		fmt.Println("PDF not finished yet")
	}

	fmt.Println("-> Waiting for the PDF to get written.")
	ps, err = resp.Wait()
	if err != nil {
		return err
	}

	fmt.Println("PDF done", ps.Errors, "errors occured. Finished at", ps.Finished.Format(time.Stamp))
	for _, e := range ps.Errormessages {
		fmt.Println("*  message", e.Error)
	}

	fmt.Println("-> Getting the PDF")
	f, err := os.Create("out.pdf")
	if err != nil {
		return err
	}
	defer f.Close()
	err = resp.GetPDF(f)
	if err != nil {
		return err
	}
	return nil
}

func main() {
	err := dothings()
	if err != nil {
		if apierror, ok := err.(api.Error); ok {
			fmt.Println("API error", apierror.ErrorType)
			fmt.Println("Instance", apierror.Instance)
			fmt.Println("Title", apierror.Title)
			fmt.Println("Detail", apierror.Detail)
			fmt.Println("Request ID", apierror.RequestID)
		}
		log.Fatal(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned on a 404
	ErrNotFound error = fmt.Errorf("Resource not found")
)

Functions

This section is empty.

Types

type Endpoint

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

Endpoint is the starting point for all publishing activity

func NewEndpoint

func NewEndpoint(authstring string, location string) (*Endpoint, error)

NewEndpoint starts a publishing session.

func (*Endpoint) AvailableVersions

func (ep *Endpoint) AvailableVersions() ([]string, error)

AvailableVersions returns a list of versions available on the server. The version 'latest' is not included in the list.

func (*Endpoint) NewPublishRequest

func (e *Endpoint) NewPublishRequest() *PublishRequest

NewPublishRequest is the base structure to start a publishing request to the endpoint.

func (*Endpoint) Publish

func (e *Endpoint) Publish(data *PublishRequest) (PublishResponse, error)

Publish sends data to the server.

type Error

type Error struct {
	ErrorType string `json:"type"`
	Title     string
	Detail    string
	Instance  string
	RequestID int
}

Error is an error struct which is used when the communication between the client and the endpoint is broken.

func (Error) Error

func (a Error) Error() string

Give a meaningful string of the error message.

type Errormessage

type Errormessage struct {
	Code  int    `json:"code"`
	Error string `json:"error"`
}

Errormessage contains a message from the publisher together with its error code. The error message is a message from the publishing run (like image not found).

type ProcessStatus

type ProcessStatus struct {
	Finished      *time.Time
	Errors        int
	Errormessages []Errormessage
}

ProcessStatus contains information about the current status of the PDF generation. If the Finished field is nil, Errors and Errormessages are not set.

type PublishFile

type PublishFile struct {
	Filename string `json:"filename"`
	Contents []byte `json:"contents"`
}

PublishFile is a file for the publishing request.

type PublishRequest

type PublishRequest struct {
	Version string
	Files   []PublishFile `json:"files"`
	// contains filtered or unexported fields
}

PublishRequest is an instance to send data to a server and get a PDF. One Endpoint can have multiple PublishingRequests.

func (*PublishRequest) AttachFile

func (p *PublishRequest) AttachFile(pathToFile string) error

AttachFile adds a file to the PublishRequest for the server. Usually you have to provide the layout and the data file. All assets (fonts, images) can be referenced by http(s) hyperlinks.

type PublishResponse

type PublishResponse struct {
	ID string
	// contains filtered or unexported fields
}

PublishResponse holds the id to the publishing process.

func (*PublishResponse) GetPDF

func (p *PublishResponse) GetPDF(w io.Writer) error

GetPDF gets the PDF from the server. In case of an error, the bytes written to w might not contain a valid PDF.

func (*PublishResponse) Status

func (p *PublishResponse) Status() (*ProcessStatus, error)

Status returns the status of the publishing run. If the process is still running, the Finished field is set to nil.

func (*PublishResponse) Wait

func (p *PublishResponse) Wait() (*ProcessStatus, error)

Wait for the publishing process to finish. Return an error if something is wrong with the request. If there is an error during the publishing run but the request itself is without errors, the error is nil, but the returned publishing status has the numbers of errors set.

Jump to

Keyboard shortcuts

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