event

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: 6 Imported by: 9

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event uint32

func (Event) HTTP

func (e Event) HTTP() (http.Event, error)
Example
package main

import (
	"fmt"
	"io/ioutil"

	httpEventSym "github.com/taubyte/go-sdk-symbols/http/event"
	"github.com/taubyte/go-sdk/common"
	"github.com/taubyte/go-sdk/event"
)

// Taubyte example function that gets an http call
func httpExample(e event.Event) uint32 {
	// Confirm that this is a http event
	httpEvent, err := e.HTTP()
	if err != nil {
		return 1
	}

	body := httpEvent.Body()
	data, err := ioutil.ReadAll(body)
	if err != nil {
		return 1
	}

	fmt.Println("Body:", string(data))

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

	// The host or fqdn used to reach this function
	host, err := httpEvent.Host()
	if err != nil {
		return 1
	}

	fmt.Println("Host:", host)

	// The Method that was called to reach this function
	method, err := httpEvent.Method()
	if err != nil {
		return 1
	}
	fmt.Println("Method:", method)

	// The url path not including the host used to reach this function
	path, err := httpEvent.Path()
	if err != nil {
		return 1
	}

	fmt.Println("Path:", path)

	// The user agent of the http request
	userAgent, err := httpEvent.UserAgent()
	if err != nil {
		return 1
	}

	fmt.Println("UserAgent:", userAgent)

	// The headers of the http request
	headers, err := httpEvent.Headers().List()
	if err != nil {
		return 1
	}

	fmt.Println("Headers:", headers)

	contentType, err := httpEvent.Headers().Get("Content-Type")
	if err != nil {
		return 1
	}

	fmt.Println("ContentType:", contentType)

	// A list of queries sent in the url
	queries, err := httpEvent.Query().List()
	if err != nil {
		return 1
	}

	fmt.Println("Queries:", queries)

	// The username query
	username, err := httpEvent.Query().Get("username")
	if err != nil {
		return 1
	}

	fmt.Println("Username:", username)

	// Write the return code
	err = httpEvent.Return(200)
	if err != nil {
		return 1
	}

	// Write the http response
	_, err = httpEvent.Write([]byte("Hello from the other side!"))
	if err != nil {
		return 1
	}

	return 0
}

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	m := httpEventSym.MockData{
		EventType: common.EventTypeHttp,
		Body:      []byte("Hello, world!"),
		Host:      "taubyte.com",
		Method:    "POST",
		Path:      "/console/v1",
		UserAgent: "Mozilla/5.0 (X11; Linux x86_64)",
		Headers: map[string]string{
			"Content-Type": "application/json",
		},
		Queries: map[string]string{
			"username": "taubyte",
		},
	}.Mock()

	if httpExample(0) != 0 {
		return
	}

	fmt.Println("ReturnCode:", m.ReturnCode)
	fmt.Println("Sent:", string(m.ReturnBody))
}
Output:

Body: Hello, world!
Host: taubyte.com
Method: POST
Path: /console/v1
UserAgent: Mozilla/5.0 (X11; Linux x86_64)
Headers: [Content-Type]
ContentType: application/json
Queries: [username]
Username: taubyte
ReturnCode: 200
Sent: Hello from the other side!

func (Event) P2P

func (e Event) P2P() (p2p.Event, error)

Gives a p2pEvent if the current function is called through p2p

Example
package main

import (
	"fmt"

	"github.com/ipfs/go-cid"
	symbols "github.com/taubyte/go-sdk-symbols/p2p/event"
	"github.com/taubyte/go-sdk/event"
)

// Taubyte example function that gets a p2p call
func p2pExample(e event.Event) uint32 {
	// Confirm that this is a p2p event
	p2pEvent, err := e.P2P()
	if err != nil {
		return 1
	}

	// The data that was sent over p2p to this function
	data, err := p2pEvent.Data()
	if err != nil {
		return 1
	}

	fmt.Println("Data:", string(data))

	// The protocol that was called to reach this function
	protocol, err := p2pEvent.Protocol()
	if err != nil {
		return 1
	}

	fmt.Println("Protocol:", protocol)

	// The Command that was called to reach this function
	command, err := p2pEvent.Command()
	if err != nil {
		return 1
	}
	fmt.Println("Command:", command)

	// The cid of the node that sent the p2p request
	from, err := p2pEvent.From()
	if err != nil {
		return 1
	}

	fmt.Println("From:", from.String())

	// The cid of the receiving node, or the node that this function is running on
	to, err := p2pEvent.To()
	if err != nil {
		return 1
	}

	fmt.Println("To:", to.String())

	// Write the response
	err = p2pEvent.Write([]byte("Hello from the other side!"))
	if err != nil {
		return 1
	}

	return 0
}

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	fromCid, _ := cid.Parse("QmZjBpQzRw8UovKaMoWJ3qvQrHZvErqQuXNyXNusm4XYK3")
	toCid, _ := cid.Parse("QmTLqapULGiA5ZFWk2ckEjKq2B6kGVuFUF1frSAeeGjuGt")
	m := symbols.MockData{
		From:     fromCid,
		To:       toCid,
		Command:  "ping",
		Protocol: "/test/v1",
		Data:     []byte("Hello, world!"),
	}.Mock()

	if p2pExample(0) != 0 {
		return
	}

	fmt.Println("Sent:", string(m.DataToSend))
}
Output:

Data: Hello, world!
Protocol: /test/v1
Command: ping
From: QmZjBpQzRw8UovKaMoWJ3qvQrHZvErqQuXNyXNusm4XYK3
To: QmTLqapULGiA5ZFWk2ckEjKq2B6kGVuFUF1frSAeeGjuGt
Sent: Hello from the other side!

func (Event) PubSub

func (e Event) PubSub() (pubsub.Event, error)
Example
package main

import (
	"fmt"

	symbols "github.com/taubyte/go-sdk-symbols/pubsub/event"
	"github.com/taubyte/go-sdk/event"
)

// Taubyte example function that gets a Pub-Sub call
func pubSubExample(e event.Event) uint32 {
	// Confirm that this is a p2p event
	pubSubEvent, err := e.PubSub()
	if err != nil {
		return 1
	}

	// The data sent to the pub-sub function
	data, err := pubSubEvent.Data()
	if err != nil {
		return 1
	}

	fmt.Println("Data:", string(data))

	// The Channel that this function was called through
	channel, err := pubSubEvent.Channel()
	if err != nil {
		return 1
	}
	fmt.Println("Channel:", channel.Name())

	return 0
}

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{
		Channel:   "someChannel",
		EventData: []byte("Hello, world!"),
	}.Mock()

	if pubSubExample(0) != 0 {
		return
	}
}
Output:

Data: Hello, world!
Channel: someChannel

func (Event) Type

func (e Event) Type() common.EventType

Jump to

Keyboard shortcuts

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