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: 3

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) Body

func (e Event) Body() EventBody
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"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	httpEventSym.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 (Event) Flush added in v0.3.9

func (e Event) Flush()

func (Event) Headers

func (e Event) Headers() EventHeaders
Example
package main

import (
	"fmt"

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

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	httpEventSym.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 (Event) Host

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

import (
	"fmt"

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

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	httpEventSym.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 (Event) Method

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

import (
	"fmt"

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

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	httpEventSym.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 (Event) Path

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

import (
	"fmt"

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

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	httpEventSym.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 (Event) Query

func (e Event) Query() Queries
Example
package main

import (
	"fmt"

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

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	httpEventSym.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 (Event) Redirect

func (e Event) Redirect(url string) httpRedirect
Example
package main

import (
	"fmt"

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

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

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

	err = httpEvent.Redirect("https://google.com").Temporary()
	if err != nil {
		return
	}

	fmt.Println(m.RedirectedTo)
}
Output:

https://google.com

func (Event) Return

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

import (
	"fmt"

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

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	m := httpEventSym.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 (Event) UserAgent

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

import (
	"fmt"

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

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	httpEventSym.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 (Event) Write

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

import (
	"fmt"

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

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	m := httpEventSym.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 EventBody

type EventBody uint32

func (EventBody) Close

func (b EventBody) Close() error

func (EventBody) Read

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

type EventHeaders

type EventHeaders uint32

func (EventHeaders) Get

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

func (EventHeaders) List

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

func (EventHeaders) Set

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

type Queries

type Queries uint32

func (Queries) Get

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

func (Queries) List

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

Jump to

Keyboard shortcuts

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