client

package
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 7 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client uint32
Example
package main

import (
	"fmt"
	"io"

	symbols "github.com/taubyte/go-sdk-symbols/ipfs/client"
	"github.com/taubyte/go-sdk/ipfs/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{}.Mock()

	ipfsClient, err := client.New()
	if err != nil {
		return
	}

	content, err := ipfsClient.Create()
	if err != nil {
		return
	}

	_, err = content.Write([]byte("Hello, world!"))
	if err != nil {
		return
	}

	cid, err := content.Push()
	if err != nil {
		return
	}

	newContent, err := ipfsClient.Open(cid)
	if err != nil {
		return
	}

	data, err := io.ReadAll(newContent)
	if err != nil {
		return
	}

	err = newContent.Close()
	if err != nil {
		return
	}

	newCid, err := newContent.Cid()
	if err != nil || cid != newCid {
		fmt.Println(err)
		return
	}

	fmt.Println(string(data))
}
Output:

Hello, world!

func New

func New() (Client, error)

func (Client) Create

func (c Client) Create() (ReadWriteContent, error)

Creates creates and returns the new content.

func (Client) Open

func (c Client) Open(_cid cid.Cid) (ReadOnlyContent, error)

Open creates a new content using the cid given as the file. Returns a new content.

type Content

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

func (*Content) Cid

func (c *Content) Cid() (cid.Cid, error)

Cid returns the cid of the file and an error.

Example
_cid, _ := cid.Parse("bafkreibrl5n5w5wqpdcdxcwaazheualemevr7ttxzbutiw74stdvrfhn2m")
symbols.MockData{
	Files: map[cid.Cid]*os.File{
		_cid: nil,
	},
}.Mock()

client, err := client.New()
if err != nil {
	return
}

content, err := client.Open(_cid)
if err != nil {
	return
}

cid, err := content.Cid()
if err != nil {
	return
}

fmt.Println("Cid:", cid.String())
Output:

Cid: bafkreibrl5n5w5wqpdcdxcwaazheualemevr7ttxzbutiw74stdvrfhn2m

func (*Content) Close

func (c *Content) Close() error

Close closes the file associated with the content. Returns an error.

func (*Content) Push

func (c *Content) Push() (cid.Cid, error)

Push adds the file into the network then closes the file Returns cid and an error

func (*Content) Read

func (c *Content) Read(p []byte) (int, error)

func (*Content) Seek

func (c *Content) Seek(offset int64, whence int) (int64, error)

Seek moves to a position inside the file. Offset is how much to move the current position Whence has three options: 0 = SeekStart, 1 = SeekCurrent, or 2 = SeekEnd Combines both offset and whence to find a new offset inside the file Returns the new offset and an error

func (*Content) Write

func (c *Content) Write(p []byte) (int, error)

Write writes the passed in data into the file. Returns how much was written and an error.

type ReadOnlyContent

type ReadOnlyContent interface {
	io.ReadSeekCloser
	Cid() (cid.Cid, error)
}

type ReadWriteContent

type ReadWriteContent interface {
	io.ReadWriteSeeker
	io.Closer
	Push() (cid.Cid, error)
}

Jump to

Keyboard shortcuts

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