h5ailist

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: MIT Imports: 14 Imported by: 0

README

About

Package h5ailist is a simple client for retrieving files/directories from a remote h5ai directory list frontend.

Using

package h5ailist_test

import (
	"context"
	"fmt"

	"github.com/kenshaw/h5ailist"
)

func Example() {
	var items []h5ailist.Item
	if err := h5ailist.Walk(context.Background(), "https://larsjung.de/h5ai/demo/file preview", func(n string, item *h5ailist.Item, err error) error {
		switch {
		case err != nil:
			return err
		}
		items = append(items, *item)
		return nil
	}); err != nil {
		panic(err)
	}
	var directories, files int64
	for i, item := range items {
		fmt.Printf("%d: %q\n", i, item.Href)
		if item.IsDir() {
			directories++
		} else {
			files++
		}
	}
	fmt.Printf("items: %d directories: %d files: %d\n", len(items), directories, files)
	// Output:
	// 0: "/h5ai/demo/file%20preview/"
	// 1: "/h5ai/demo/file preview/class_cli.py"
	// 2: "/h5ai/demo/file preview/image-1.jpg"
	// 3: "/h5ai/demo/file preview/image-2.jpg"
	// 4: "/h5ai/demo/file preview/image-3.jpg"
	// 5: "/h5ai/demo/file preview/modulejs-1.14.0.js"
	// 6: "/h5ai/demo/file preview/options.css"
	// 7: "/h5ai/demo/file preview/text.md"
	// items: 8 directories: 1 files: 7
}

Documentation

Overview

Package h5ailist retrieves a directory listing from a remote h5ai list.

Example
package main

import (
	"context"
	"fmt"

	"github.com/kenshaw/h5ailist"
)

func main() {
	var items []h5ailist.Item
	if err := h5ailist.Walk(context.Background(), "https://larsjung.de/h5ai/demo/file preview", func(n string, item *h5ailist.Item, err error) error {
		switch {
		case err != nil:
			return err
		}
		items = append(items, *item)
		return nil
	}); err != nil {
		panic(err)
	}
	var directories, files int64
	for i, item := range items {
		fmt.Printf("%d: %q\n", i, item.Href)
		if item.IsDir() {
			directories++
		} else {
			files++
		}
	}
	fmt.Printf("items: %d directories: %d files: %d\n", len(items), directories, files)
}
Output:

0: "/h5ai/demo/file%20preview/"
1: "/h5ai/demo/file preview/class_cli.py"
2: "/h5ai/demo/file preview/image-1.jpg"
3: "/h5ai/demo/file preview/image-2.jpg"
4: "/h5ai/demo/file preview/image-3.jpg"
5: "/h5ai/demo/file preview/modulejs-1.14.0.js"
6: "/h5ai/demo/file preview/options.css"
7: "/h5ai/demo/file preview/text.md"
items: 8 directories: 1 files: 7

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(ctx context.Context, urlstr string, opts ...Option) ([]byte, error)

Get retrieves the item.

func Walk

func Walk(ctx context.Context, urlstr string, f WalkFunc, opts ...Option) error

Walk walks all items.

Types

type Client

type Client struct {
	URL       string
	UserAgent string
	Transport http.RoundTripper
	Jar       http.CookieJar
	// contains filtered or unexported fields
}

Client is a h5ai client.

func New

func New(opts ...Option) *Client

New creates a new h5ai client.

func (*Client) BuildRequest

func (cl *Client) BuildRequest(method, urlstr string, body io.Reader) (*http.Request, error)

BuildRequest builds a http request.

func (*Client) Do

func (cl *Client) Do(ctx context.Context, method, urlstr string, request, v interface{}) error

Do executes a request against the context and client.

func (*Client) Get

func (cl *Client) Get(ctx context.Context, urlstr string) ([]byte, error)

Get retrieves the url.

func (*Client) Href

func (cl *Client) Href(paths ...string) (string, string, error)

Href returns the href path for the URL combined with paths.

func (*Client) Items

func (cl *Client) Items(ctx context.Context, paths ...string) ([]Item, error)

Items returns the items at the path.

func (*Client) JoinGet added in v0.2.1

func (cl *Client) JoinGet(ctx context.Context, paths ...string) ([]byte, error)

JoinGet retrieves a file.

func (*Client) List

func (cl *Client) List(ctx context.Context, paths ...string) ([]Item, error)

List returns the list at the path.

func (*Client) Walk

func (cl *Client) Walk(ctx context.Context, root string, fn WalkFunc) error

Walk walks the root.

type Error

type Error string

Error is a error.

const (
	SkipDir Error = "skip dir"
	SkipAll Error = "skip all"
)

func (Error) Error

func (err Error) Error() string

Error satisfies the error interface.

type Item

type Item struct {
	Fetched bool   `json:"fetched,omitempty"`
	Href    string `json:"href,omitempty"`
	Managed bool   `json:"managed,omitempty"`
	Size    *int64 `json:"size,omitempty"`
	Time    Time   `json:"time,omitempty"`
	URL     string `json:"-"`
}

Item is a item.

func Items

func Items(ctx context.Context, urlstr string, opts ...Option) ([]Item, error)

Items retrieves the items for the url.

func List

func List(ctx context.Context, urlstr string, opts ...Option) ([]Item, error)

List retrieves the listing for the url.

func (Item) FileSize

func (item Item) FileSize() int64

FileSize returns the file size.

func (Item) IsDir

func (item Item) IsDir() bool

IsDir returns true when the item is a directory.

type Option

type Option func(*Client)

Option is a client option.

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

WithHTTPClient is a client option to set the underlying http.Client used.

func WithJar

func WithJar(jar http.CookieJar) Option

WithJar is a client option to set the cookie jar.

func WithLogf

func WithLogf(logf interface{}, opts ...httplog.Option) Option

WithLogf is a client option to set a log handler for http requests and responses.

func WithTransport

func WithTransport(transport http.RoundTripper) Option

WithTransport is a client option to set the transport.

func WithURL

func WithURL(urlstr string) Option

WithURL is a client option to set the url.

func WithUserAgent

func WithUserAgent(userAgent string) Option

WithUserAgent is a client option to set the transport.

type Time

type Time struct {
	time.Time
}

Time is a wrapped time.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(buf []byte) error

UnmarshalJSON satisfies the json.Unmarshaler interface.

type WalkFunc

type WalkFunc func(path string, item *Item, err error) error

WalkFunc is the walk func signature.

Jump to

Keyboard shortcuts

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