event

package
Version: v0.1.39 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2022 License: GPL-3.0 Imports: 9 Imported by: 2

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() (HttpEvent, error)
Example
package main

import (
	"fmt"
	"io/ioutil"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/event"
	"bitbucket.org/taubyte/go-sdk/common"
	"bitbucket.org/taubyte/go-sdk/event"
)

// Taubyte example function that gets an http call
func httpExample(e event.Event) uint32 {

	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
	}

	host, err := httpEvent.Host()
	if err != nil {
		return 1
	}

	fmt.Println("Host:", host)

	method, err := httpEvent.Method()
	if err != nil {
		return 1
	}
	fmt.Println("Method:", method)

	path, err := httpEvent.Path()
	if err != nil {
		return 1
	}

	fmt.Println("Path:", path)

	userAgent, err := httpEvent.UserAgent()
	if err != nil {
		return 1
	}

	fmt.Println("UserAgent:", userAgent)

	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)

	queries, err := httpEvent.Query().List()
	if err != nil {
		return 1
	}

	fmt.Println("Queries:", queries)

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

	fmt.Println("Username:", username)

	err = httpEvent.Return(200)
	if err != nil {
		return 1
	}

	_, 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 := symbols.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"

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

// 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.PubSubEvent, error)
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/pubsub"
	"bitbucket.org/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

type HttpEvent

type HttpEvent uint32

func (HttpEvent) Body

func (e HttpEvent) Body() HttpEventBody
Example
package main

import (
	"fmt"
	"io/ioutil"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/event"
	"bitbucket.org/taubyte/go-sdk/common"
	"bitbucket.org/taubyte/go-sdk/event"
)

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

	var e event.Event
	httpEvent, err := e.HTTP()
	if err != nil {
		return
	}

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

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

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

Hello, world!

func (HttpEvent) Headers

func (e HttpEvent) Headers() HttpEventHeaders
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/event"
	"bitbucket.org/taubyte/go-sdk/common"
	"bitbucket.org/taubyte/go-sdk/event"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{
		EventType: common.EventTypeHttp,
		Headers: map[string]string{
			"Content-Type": "application/json",
		},
	}.Mock()

	var e event.Event
	httpEvent, err := e.HTTP()
	if err != nil {
		return
	}

	headers := httpEvent.Headers()

	contentType, err := headers.Get("Content-Type")
	if err != nil {
		return
	}
	fmt.Println("Received content type:", contentType)

	headersList, err := headers.List()
	if err != nil {
		return
	}
	fmt.Println("Headers:", headersList)

	err = headers.Set("Content-Type", "text/csv")
	if err != nil {
		return
	}

	contentType, err = headers.Get("Content-Type")
	if err != nil {
		return
	}
	fmt.Println("New content type:", contentType)

}
Output:

Received content type: application/json
Headers: [Content-Type]
New content type: text/csv

func (HttpEvent) Host

func (e HttpEvent) Host() (string, error)
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/event"
	"bitbucket.org/taubyte/go-sdk/common"
	"bitbucket.org/taubyte/go-sdk/event"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{
		EventType: common.EventTypeHttp,
		Host:      "hal.computers.com",
	}.Mock()

	var e event.Event

	httpEvent, err := e.HTTP()
	if err != nil {
		return
	}

	host, err := httpEvent.Host()
	if err != nil {
		return
	}

	fmt.Println("Host:", host)
}
Output:

Host: hal.computers.com

func (HttpEvent) Method

func (e HttpEvent) Method() (string, error)
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/event"
	"bitbucket.org/taubyte/go-sdk/common"
	"bitbucket.org/taubyte/go-sdk/event"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{
		EventType: common.EventTypeHttp,
		Method:    "POST",
	}.Mock()

	var e event.Event

	httpEvent, err := e.HTTP()
	if err != nil {
		return
	}

	method, err := httpEvent.Method()
	if err != nil {
		return
	}

	fmt.Println("Method:", method)
}
Output:

Method: POST

func (HttpEvent) Path

func (e HttpEvent) Path() (string, error)
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/event"
	"bitbucket.org/taubyte/go-sdk/common"
	"bitbucket.org/taubyte/go-sdk/event"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{
		EventType: common.EventTypeHttp,
		Path:      "/test/v1",
	}.Mock()

	var e event.Event

	httpEvent, err := e.HTTP()
	if err != nil {
		return
	}

	path, err := httpEvent.Path()
	if err != nil {
		return
	}

	fmt.Println("Path:", path)
}
Output:

Path: /test/v1

func (HttpEvent) Query

func (e HttpEvent) Query() HttpQueries
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/event"
	"bitbucket.org/taubyte/go-sdk/common"
	"bitbucket.org/taubyte/go-sdk/event"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{
		EventType: common.EventTypeHttp,
		Queries: map[string]string{
			"username": "taubyte",
		},
	}.Mock()

	var e event.Event
	httpEvent, err := e.HTTP()
	if err != nil {
		return
	}

	query := httpEvent.Query()

	username, err := query.Get("username")
	if err != nil {
		return
	}
	if username != "taubyte" {
		return
	}

	queryList, err := query.List()
	if err != nil {
		return
	}
	fmt.Println("Queries:", queryList)

}
Output:

Queries: [username]

func (HttpEvent) Return

func (e HttpEvent) Return(code int) error
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/event"
	"bitbucket.org/taubyte/go-sdk/common"
	"bitbucket.org/taubyte/go-sdk/event"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	m := symbols.MockData{
		EventType: common.EventTypeHttp,
	}.Mock()

	var e event.Event

	httpEvent, err := e.HTTP()
	if err != nil {
		return
	}

	err = httpEvent.Return(404)
	if err != nil {
		return
	}

	fmt.Println("Code:", m.ReturnCode)
}
Output:

Code: 404

func (HttpEvent) UserAgent

func (e HttpEvent) UserAgent() (string, error)
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/event"
	"bitbucket.org/taubyte/go-sdk/common"
	"bitbucket.org/taubyte/go-sdk/event"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{
		EventType: common.EventTypeHttp,
		UserAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/517.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/5447.36",
	}.Mock()

	var e event.Event

	httpEvent, err := e.HTTP()
	if err != nil {
		return
	}

	path, err := httpEvent.UserAgent()
	if err != nil {
		return
	}

	fmt.Println("UserAgent:", path)
}
Output:

UserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/517.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/5447.36

func (HttpEvent) Write

func (e HttpEvent) Write(data []byte) (int, error)
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/event"
	"bitbucket.org/taubyte/go-sdk/common"
	"bitbucket.org/taubyte/go-sdk/event"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	m := symbols.MockData{
		EventType: common.EventTypeHttp,
	}.Mock()

	var e event.Event

	httpEvent, err := e.HTTP()
	if err != nil {
		return
	}

	toWrite := []byte("Hello, world!")

	n, err := httpEvent.Write(toWrite)
	if err != nil {
		return
	}
	if len(toWrite) != n {
		return
	}

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

ReturnBody: Hello, world!

type HttpEventBody

type HttpEventBody uint32

func (HttpEventBody) Close

func (b HttpEventBody) Close() error

func (HttpEventBody) Read

func (b HttpEventBody) Read(p []byte) (int, error)

type HttpEventHeaders

type HttpEventHeaders uint32

func (HttpEventHeaders) Get

func (e HttpEventHeaders) Get(key string) (string, error)

func (HttpEventHeaders) List

func (e HttpEventHeaders) List() ([]string, error)

func (HttpEventHeaders) Set

func (e HttpEventHeaders) Set(key, value string) error

type HttpQueries

type HttpQueries uint32

func (HttpQueries) Get

func (e HttpQueries) Get(key string) (string, error)

func (HttpQueries) List

func (e HttpQueries) List() ([]string, error)

Jump to

Keyboard shortcuts

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