oc

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: MIT Imports: 19 Imported by: 0

README

oc-client-go

A project for simplifying when working with Open Content API.

Search example

import "github.com/navigacontentlab/oc-client-go"

client, err := oc.New(oc.Options{
	BaseURL: "https://host:8443/opencontent",
	Auth:    oc.BearerAuth("<token>"),
})

req := oc.SearchRequest{
	Properties: "uuid,updated",
	Query:      "contenttype:Image",
	Sort: []oc.SearchSort{{
		IndexField: "updated",
		Descending: true,
	}},
}

resp, err := client.Search(context.Background(), req)

Upload example

import (
    "github.com/navigacontentlab/oc-client-go"
    "golang.org/x/oauth2"
    "golang.org/x/oauth2/clientcredentials"
)

config := clientcredentials.Config{
	ClientID:     "<client-id>",
	ClientSecret: "<client-secret>",
	AuthStyle:    oauth2.AuthStyleInParams,
	TokenURL:     "https://access-token.stage.id.navigacloud.com/v1/token",
}

httpClient := config.Client(context.Background())
httpClient.Timeout = time.Second * 5

client, err := oc.New(oc.Options{
	BaseURL:    "https://<host>:7777/opencontent",
	HTTPClient: httpClient,
})

reader, err := os.Open("sample.jpeg")
metadataReader, err := os.Open("sample-image-metadata.xml")

req := oc.UploadRequest{
	Files: oc.FileSet{
		"file": oc.File{
			Name:     "sample.jpeg",
			Reader:   reader,
			Mimetype: "image/jpeg",
		},
		"metadata": oc.File{
			Name:     "sample-image.metadata.xml",
			Reader:   metadataReader,
			Mimetype: "application/vnd.iptc.g2.newsitem+xml.picture",
		},
	},
}

resp, err := client.Upload(context.Background(), req)

Metrics

This library provides metrics in the form of a prometheus collector.

The following metrics are supported:

  • statusCodes
  • responseTimes

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultOrganisationExtractor

func DefaultOrganisationExtractor(_ context.Context) string

Types

type AuthenticationMethod

type AuthenticationMethod func(req *http.Request)

AuthenticationMethod is a function that adds authentication information to a request.

func BasicAuth

func BasicAuth(username, password string) AuthenticationMethod

BasicAuth adds a basic auth authorisation header to the outgoing requests.

func BearerAuth

func BearerAuth(token string) AuthenticationMethod

BasicAuth adds a bearer token authorisation header to the outgoing requests.

type ChangelogEntry

type ChangelogEntry struct {
	Text    string `xml:",chardata"`
	ID      string `xml:"id"`
	Title   string `xml:"title"`
	Updated string `xml:"updated"`
}

type Client

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

Client is an Open Content client.

func New

func New(opt Options) (*Client, error)

New creates a new Open Content client.

func (*Client) Changelog

func (c *Client) Changelog(ctx context.Context, start int, limit int) (*Feed, error)

func (*Client) CheckExists

func (c *Client) CheckExists(ctx context.Context, uuid string) (*ExistsResponse, error)

CheckExists does a HEAD request against an object and returns information about the object.

func (*Client) ConsistencyCheck

func (c *Client) ConsistencyCheck(ctx context.Context, uuid string) (*ConsistencyStatus, error)

ConsistencyCheck performs a check against the database, storage, and index.

func (*Client) ContentTypes

func (c *Client) ContentTypes(ctx context.Context, req ContentTypesRequest) (*ContentTypesResponse, error)

ContentTypes gets the schema (all content types and properties) from Open Content.

func (*Client) Contentlog

func (c *Client) Contentlog(ctx context.Context, event int) ([]ContentlogEvent, error)

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, uuid string, options *DeleteOptions) error

Delete deletes an object.

func (*Client) DeleteMetadataFile

func (c *Client) DeleteMetadataFile(ctx context.Context, req DeleteMetadataRequest) error

func (*Client) Eventlog

func (c *Client) Eventlog(ctx context.Context, event int) ([]EventlogEvent, error)

func (*Client) Get

func (c *Client) Get(ctx context.Context, req GetRequest) (*GetResponse, error)

Get is used to retrieve properties for a given set of uuids. It requires a backend of OC 3.0+, because it uses a new endpoint `/get`. This endpoint uses what is called 'real-time get' in solr. This basically means retrieving document in a way that is cheaper than an ordinary search but you are still assured you get what is in the index.

func (*Client) GetFile

func (c *Client) GetFile(ctx context.Context, uuid string, filename string, version int64) (*FileResponse, error)

func (*Client) GetMetadataFile

func (c *Client) GetMetadataFile(ctx context.Context, uuid string, version int) (*FileResponse, error)

func (*Client) GetObject

func (c *Client) GetObject(ctx context.Context, uuid string, version int64) (*ObjectResponse, error)

func (*Client) GetVersion

func (c *Client) GetVersion(ctx context.Context) (string, error)

func (*Client) Head

func (c *Client) Head(ctx context.Context, uuid string) (int64, error)

func (*Client) Health

func (c *Client) Health(ctx context.Context, req HealthRequest) (Health, error)

Health performs a health check request against OC.

func (*Client) ListFiles

func (c *Client) ListFiles(ctx context.Context, uuid string, version int64) (*FileList, error)

func (*Client) Properties

func (c *Client) Properties(ctx context.Context, uuid string, properties PropertyList) (*PropertyResult, error)

func (*Client) PropertiesVersion

func (c *Client) PropertiesVersion(
	ctx context.Context,
	uuid string, version int64, properties PropertyList,
) (*PropertyResult, error)

func (*Client) Purge

func (c *Client) Purge(ctx context.Context, uuid string, options *PurgeOptions) error

Purge purges an object.

func (*Client) ReplaceMetadataFile

func (c *Client) ReplaceMetadataFile(ctx context.Context, req ReplaceMetadataRequest) error

func (*Client) Search

func (c *Client) Search(ctx context.Context, req SearchRequest) (*SearchResponse, error)

Search performs a search against Open Content.

Example
client, err := oc.New(oc.Options{
	BaseURL: os.Getenv("OC_BASEURL"),
	Auth: oc.BasicAuth(
		os.Getenv("OC_USERNAME"),
		os.Getenv("OC_PASSWORD"),
	),
})
if err != nil {
	log.Fatal(err)
}

ctx := context.Background()

searchReq := oc.SearchRequest{
	Start:       0,
	Limit:       15,
	Properties:  "uuid,Headline,created,updated",
	ContentType: "Article",
	Sort: []oc.SearchSort{
		{IndexField: "updated"},
	},
}

_, err = client.Search(ctx, searchReq)
if err != nil {
	fmt.Println(err)
}
Output:

func (*Client) Suggest

func (c *Client) Suggest(ctx context.Context, req SuggestRequest) (*SuggestResponse, error)

Suggest performs a suggest against Open Content.

func (*Client) Undelete

func (c *Client) Undelete(ctx context.Context, uuid string, options *UndeleteOptions) error

func (*Client) Upload

func (c *Client) Upload(ctx context.Context, req UploadRequest) (*UploadResponse, error)

Upload saves the fileset in the OC database.

Example

Will not actually be run since there is no output because we cannot run this multiple times to the same oc.

client, err := oc.New(oc.Options{
	BaseURL: os.Getenv("OC_BASEURL"),
	Auth: oc.BasicAuth(
		os.Getenv("OC_USERNAME"),
		os.Getenv("OC_PASSWORD"),
	),
})
if err != nil {
	log.Fatal(err)
}

reader, err := os.Open("sample.jpeg")
if err != nil {
	log.Fatal(err)
}

metadataReader, err := os.Open("sample-image-metadata.xml")
if err != nil {
	log.Fatal(err)
}

ctx := context.Background()

uploadReq := oc.UploadRequest{
	UUID:   uuid.New().String(),
	Source: "mycustomuploader",
	Files: oc.FileSet{
		"file": oc.File{

			Name:     "sample.jpeg",
			Reader:   reader,
			Mimetype: "image/jpeg",
		},
		"metadata": oc.File{

			Name:     "sample-image.metadata.xml",
			Reader:   metadataReader,
			Mimetype: "application/vnd.iptc.g2.newsitem+xml.picture",
		},
	},
}

_, err = client.Upload(ctx, uploadReq)

if err != nil {
	fmt.Println(err)
}
Output:

type ConsistencyCheckResponse

type ConsistencyCheckResponse struct {
	Database ConsistencyStatus `json:"database"`
	Index    ConsistencyStatus `json:"index"`
	Storage  ConsistencyStatus `json:"storage"`
}

type ConsistencyStatus

type ConsistencyStatus struct {
	Deleted  bool   `json:"deleted"`
	Checksum string `json:"checksum"`
	Version  int    `json:"version"`
}

type ContentTypesRequest

type ContentTypesRequest struct {
	Temporary bool
}

func (*ContentTypesRequest) QueryValues

func (cr *ContentTypesRequest) QueryValues() (url.Values, error)

type ContentTypesResponse

type ContentTypesResponse struct {
	ContentTypes []struct {
		Name       string `json:"name"`
		Properties []struct {
			Name           string `json:"name"`
			Type           string `json:"type"`
			MultiValued    bool   `json:"multiValued"`
			Searchable     bool   `json:"searchable"`
			ReadOnly       bool   `json:"readOnly"`
			Description    string `json:"description"`
			Suggest        bool   `json:"suggest"`
			IndexFieldType string `json:"indexFieldType"`
		} `json:"properties"`
	} `json:"contentTypes"`
}

type ContentlogEvent

type ContentlogEvent struct {
	ID        int       `json:"id"`
	UUID      string    `json:"uuid"`
	EventType string    `json:"eventType"`
	Created   time.Time `json:"created"`
	Content   struct {
		UUID        string      `json:"uuid"`
		Version     int         `json:"version"`
		Created     time.Time   `json:"created"`
		Source      interface{} `json:"source"`
		ContentType string      `json:"contentType"`
		Batch       bool        `json:"batch"`
	} `json:"content"`
}

type DeleteMetadataRequest

type DeleteMetadataRequest struct {
	// UUID of the document to delete a metadata file for.
	UUID string
	// Filename of the metadata file to delete.
	Filename string
	// IfMatch only deletes the metadata file if the main object
	// still has a matching ETag. Optional.
	IfMatch string
	// Unit is used when passing a X-Imid-Unit header to OC
	Unit string
}

type DeleteOptions

type DeleteOptions struct {
	// IfMatch causes the object to only be deleted if its ETag
	// matches the provided value.
	IfMatch string
	// Unit is passed to OC as a X-Imid-Unit HTTP header is passed
	Unit string
}

type EventlogEvent

type EventlogEvent struct {
	ID        int       `json:"id"`
	UUID      string    `json:"uuid"`
	EventType string    `json:"eventType"`
	Created   time.Time `json:"created"`
	Content   struct {
		UUID        string      `json:"uuid"`
		Version     int         `json:"version"`
		Created     time.Time   `json:"created"`
		Source      interface{} `json:"source"`
		ContentType string      `json:"contentType"`
		Batch       bool        `json:"batch"`
	} `json:"content"`
}

type ExistsResponse

type ExistsResponse struct {
	Exists        bool
	ETag          string
	ContentType   string
	ContentLength int
	Version       int64
}

type FacetField

type FacetField struct {
	Year        int         `json:"year"`
	Month       int         `json:"month"`
	Day         int         `json:"day"`
	FacetField  string      `json:"facetField"`
	Frequencies []Frequency `json:"frequencies"`
}

type FacetFields

type FacetFields struct {
	Fields []FacetField `json:"fields"`
}

type Feed

type Feed struct {
	XMLName      xml.Name         `xml:"feed"`
	Text         string           `xml:",chardata"`
	TotalChanges string           `xml:"totalChanges,attr"`
	Xmlns        string           `xml:"xmlns,attr"`
	Entries      []ChangelogEntry `xml:"entry"`
	Title        string           `xml:"title"`
}

type File

type File struct {
	Name     string
	Reader   io.Reader
	Mimetype string
}

type FileList

type FileList struct {
	Version   int64
	Primary   ObjectFile   `json:"primary"`
	Metadata  []ObjectFile `json:"metadata"`
	Preview   ObjectFile   `json:"preview"`
	Thumb     ObjectFile   `json:"thumb"`
	Created   *time.Time   `json:"created"`
	Updated   *time.Time   `json:"updated"`
	EventType string       `json:"eventType"`
}

type FileResponse

type FileResponse struct {
	ETag        string
	ContentType string
	Version     int64
	Body        io.ReadCloser
}

type FileSet

type FileSet map[string]File

type Frequency

type Frequency struct {
	Term      string `json:"term"`
	Frequency int    `json:"frequency"`
}

type GetRequest

type GetRequest struct {
	UUIDs      []string
	Properties string
	Filters    string
	Deleted    bool
}

GetRequest is the request payload for a call to client.Get().

type GetResponse

type GetResponse SearchResponse

type Health

type Health struct {
	Indexer             bool  `json:"indexer"`
	Solr                bool  `json:"index"`
	Database            bool  `json:"database"`
	Storage             bool  `json:"filesystem"`
	FreeSystemDiskSpace int64 `json:"freeSystemDiskSpace"`
	MaximumMemory       int   `json:"maximumMemory"`
	CurrentMemory       int   `json:"currentMemory"`
	ActiveConfiguration struct {
		Checksum     string    `json:"checksum"`
		LastModified time.Time `json:"lastModified"`
	} `json:"activeConfiguration"`
	TempConfiguration struct {
		Checksum     string    `json:"checksum"`
		LastModified time.Time `json:"lastModified"`
	} `json:"tempConfiguration"`
}

Health is a OpenContent health check response.

type HealthRequest

type HealthRequest struct {
	// SkipIndexer tells OC to omit the indexer health check.
	SkipIndexer bool
	// SkipSolr tells OC to omit the Solr health check.
	SkipSolr bool
	// SkipStorage tells OC to omit the storage health check.
	SkipStorage bool
}

HealthRequest is a request to the health check endpoint.

type Highlights

type Highlights map[string][]string

type Hit

type Hit struct {
	ID         string     `json:"id"`
	Version    int        `json:"version"`
	Properties Properties `json:"properties"`
}

func (Hit) MarshalJSON

func (h Hit) MarshalJSON() ([]byte, error)

func (*Hit) UnmarshalJSON

func (h *Hit) UnmarshalJSON(data []byte) error

type Hits

type Hits struct {
	TotalHits    int   `json:"totalHits"`
	Items        []Hit `json:"hits"`
	IncludedHits int   `json:"includedHits"`
}

type Metrics

type Metrics struct {
	StatusCodes  *prometheus.CounterVec
	Duration     *prometheus.HistogramVec
	OrgExtractor func(ctx context.Context) string
}

func NewMetrics

func NewMetrics(reg prometheus.Registerer, orgExtractor func(ctx context.Context) string) (*Metrics, error)

type ObjectFile

type ObjectFile struct {
	Name     string `json:"filename"`
	Mimetype string `json:"mimetype"`
}

type ObjectResponse

type ObjectResponse struct {
	ETag        string
	ContentType string
	Body        io.ReadCloser
	Version     int64
}

type Options

type Options struct {
	BaseURL    string
	HTTPClient *http.Client
	Auth       AuthenticationMethod
	Logger     log.Logger
	Metrics    *Metrics
}

Options controls the behaviour of the Open Content client.

type Properties

type Properties map[string][]interface{}

func (Properties) Get

func (p Properties) Get(property string) (string, bool)

func (Properties) GetValues

func (p Properties) GetValues(property string) ([]string, bool)

func (Properties) Relationships

func (p Properties) Relationships(property string) ([]Properties, bool)

type Property

type Property struct {
	Name        string          `json:"name"`
	Type        string          `json:"type"`
	MultiValued bool            `json:"multiValued"`
	ReadOnly    bool            `json:"readOnly"`
	Values      []PropertyValue `json:"values"`
}

type PropertyList

type PropertyList []*PropertyReference

func (*PropertyList) AddProperty

func (pl *PropertyList) AddProperty(name string, nested ...string) *PropertyReference

func (*PropertyList) Append

func (pl *PropertyList) Append(names ...string)

func (*PropertyList) Ensure

func (pl *PropertyList) Ensure(names ...string) *PropertyReference

func (*PropertyList) MarshalText

func (pl *PropertyList) MarshalText() ([]byte, error)
Example
var list oc.PropertyList

list.Append("UUID", "Headline")
list.AddProperty("Image", "Width", "Height")

list.AddProperty("Author", "Title", "UUID", "URL")
list.Ensure("Author", "Avatar").AddNested("Width", "Height")

txt, err := list.MarshalText()
if err != nil {
	log.Fatal(err)
}

fmt.Println(string(txt))
Output:

UUID,Headline,Image[Width,Height],Author[Title,UUID,URL,Avatar[Width,Height]]

func (*PropertyList) UnmarshalText

func (pl *PropertyList) UnmarshalText(text []byte) error
Example
var list oc.PropertyList

err := list.UnmarshalText([]byte("UUID,Headline,Image[Width,Height],Author[Title,UUID,URL,Avatar[Width,Height]]"))
if err != nil {
	log.Fatal(err)
}

txt, err := list.MarshalText()
if err != nil {
	log.Fatal(err)
}

fmt.Println(string(txt))
Output:

UUID,Headline,Image[Width,Height],Author[Title,UUID,URL,Avatar[Width,Height]]

type PropertyReference

type PropertyReference struct {
	Name   string
	Nested PropertyList
}

func NewPropertyReference

func NewPropertyReference(name string, nested ...string) *PropertyReference

func (*PropertyReference) AddNested

func (p *PropertyReference) AddNested(nested ...string)

type PropertyResult

type PropertyResult struct {
	ContentType string     `json:"contentType"`
	Editable    bool       `json:"editable"`
	Properties  []Property `json:"properties"`
}

type PropertyValue

type PropertyValue struct {
	NestedProperty *PropertyResult
	Value          string
}

func (*PropertyValue) UnmarshalJSON

func (pv *PropertyValue) UnmarshalJSON(data []byte) error

type PurgeOptions

type PurgeOptions struct {
	// IfMatch causes the object to only be purged if its ETag
	// matches the provided value.
	IfMatch string
}

type ReplaceMetadataRequest

type ReplaceMetadataRequest struct {
	UUID        string
	Filename    string
	ContentType string
	Body        io.Reader
	IfMatch     string
	Batch       bool
	Unit        string
}

type ResponseError

type ResponseError struct {
	Response *http.Response
	Body     *bytes.Buffer
	// contains filtered or unexported fields
}

ResponseError contains the response struct so that it can be inspected by the client.

func (*ResponseError) Error

func (re *ResponseError) Error() string

Error formats an error message.

type SearchFacets

type SearchFacets struct {
	Fields   []string
	Limit    int
	MinCount int
}

type SearchRequest

type SearchRequest struct {
	IfNoneMatch string
	Start       int
	Limit       int
	Property    string
	Properties  string
	Filters     string
	Query       string
	FilterQuery string
	Sort        []SearchSort
	ContentType string // OC type, i.e. Article
	Deleted     bool
	Facets      SearchFacets
	Highlight   []string
}

func (*SearchRequest) QueryValues

func (sr *SearchRequest) QueryValues() (url.Values, error)

type SearchResponse

type SearchResponse struct {
	Hits      Hits                  `json:"hits"`
	Facet     FacetFields           `json:"facet"`
	Stats     Stats                 `json:"stats"`
	Highlight map[string]Properties `json:"highlight"`
}

type SearchSort

type SearchSort struct {
	IndexField string
	Descending bool
}

type Stats

type Stats struct {
	Duration int         `json:"duration"`
	Hits     interface{} `json:"hits"`
}

type SuggestField

type SuggestField struct {
	Name  string `json:"name"`
	Terms []Term `json:"terms"`
}

type SuggestIndexField

type SuggestIndexField struct {
	Name           string
	IncompleteWord string
}

type SuggestRequest

type SuggestRequest struct {
	IndexFields          []SuggestIndexField
	Limit                int
	Query                string
	IncompleteWordInText string
	Type                 SuggestType
	Timezone             string
}

func (*SuggestRequest) QueryValues

func (sr *SuggestRequest) QueryValues() (url.Values, error)

type SuggestResponse

type SuggestResponse struct {
	Fields []SuggestField `json:"facetFields"`
}

type SuggestType

type SuggestType int
const (
	Facet SuggestType = iota
	Ngram
)

func (SuggestType) String

func (s SuggestType) String() string

type Term

type Term struct {
	Name      string `json:"name"`
	Frequency int    `json:"frequency"`
}

type UndeleteOptions

type UndeleteOptions struct {
	// Unit is passed to OC as a X-Imid-Unit HTTP header is passed
	Unit string
}

type UploadRequest

type UploadRequest struct {
	UUID    string
	Source  string
	Files   FileSet
	Unit    string
	Batch   bool
	IfMatch string
}

type UploadResponse

type UploadResponse struct {
	UUID    string
	ETag    string
	Version int64
}

Jump to

Keyboard shortcuts

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