client

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: 7 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HttpClient

type HttpClient uint32
Example
package main

import (
	"fmt"
	"io/ioutil"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/http/client"
	"bitbucket.org/taubyte/go-sdk/http/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{ClientId: 5, RequestId: 3, ResponseBody: []byte(`{"hello": "world"}`)}.Mock()

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

	request, err := httpClient.Request("google.com")
	if err != nil {
		return
	}

	if request.Headers().Set("Content-Type", "application/json") != nil {
		return
	}

	response, err := request.Do()
	if err != nil {
		return
	}

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

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

{"hello": "world"}

func New

func New() (HttpClient, error)
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/http/client"
	"bitbucket.org/taubyte/go-sdk/http/client"
)

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

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

	fmt.Printf("%d\n", httpClient)
}
Output:

5

func (HttpClient) Request

func (c HttpClient) Request(url string, options ...HttpRequestOption) (HttpRequest, error)
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/http/client"
	"bitbucket.org/taubyte/go-sdk/http/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{ClientId: 5, RequestId: 3}.Mock()

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

	request, err := httpClient.Request(
		"www.google.com",
		client.Headers(map[string][]string{"Content-Type": {"application/json"}}),
		client.Method("GET"),
		client.Body([]byte("Hello, world!")),
	)
	if err != nil {
		fmt.Println("ERR", err)
		return
	}

	fmt.Printf("%d\n", request)
}
Output:

{3 5}

type HttpMethod

type HttpMethod uint32
const (
	UNKNOWN HttpMethod = iota
	GET
	POST
	PUT
	DELETE
	HEAD
	OPTIONS
	PATCH
	TRACE
	CONNECT
)

type HttpRequest

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

func (*HttpRequest) Body

func (r *HttpRequest) Body() *HttpRequestBody
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/http/client"
	"bitbucket.org/taubyte/go-sdk/http/client"
)

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

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

	request, err := httpClient.Request("google.com")
	if err != nil {
		return
	}

	err = request.Body().Set([]byte("Hello, world!"))
	if err != nil {
		return
	}

	fmt.Println(string(m.RequestBody))
}
Output:

Hello, world!

func (*HttpRequest) Do

func (r *HttpRequest) Do() (*HttpResponse, error)
Example
package main

import (
	"fmt"
	"io/ioutil"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/http/client"
	"bitbucket.org/taubyte/go-sdk/http/client"
)

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

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

	request, err := httpClient.Request("google.com")
	if err != nil {
		return
	}

	response, err := request.Do()
	if err != nil {
		return
	}

	body := response.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 (*HttpRequest) Headers

func (r *HttpRequest) Headers() *HttpRequestHeaders

func (*HttpRequest) Method

func (r *HttpRequest) Method() *HttpRequestMethod
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/http/client"
	"bitbucket.org/taubyte/go-sdk/http/client"
)

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

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

	request, err := httpClient.Request("google.com")
	if err != nil {
		return
	}

	err = request.Method().Set("POST")
	if err != nil {
		return
	}

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

	fmt.Println(method)
}
Output:

POST

type HttpRequestBody

type HttpRequestBody HttpRequest

func (*HttpRequestBody) Set

func (r *HttpRequestBody) Set(data []byte) error

type HttpRequestHeaders

type HttpRequestHeaders HttpRequest

func (*HttpRequestHeaders) Add

func (r *HttpRequestHeaders) Add(key, value string) error
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/http/client"
	"bitbucket.org/taubyte/go-sdk/http/client"
)

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

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

	request, err := httpClient.Request("google.com")
	if err != nil {
		return
	}

	err = request.Headers().Add("fruits", "apple")
	if err != nil {
		return
	}

	fmt.Println("Success")
}
Output:

Success

func (*HttpRequestHeaders) Get

func (r *HttpRequestHeaders) Get(key string) ([]string, error)
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/http/client"
	"bitbucket.org/taubyte/go-sdk/http/client"
	"bitbucket.org/taubyte/go-sdk/utils/slices"
)

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

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

	request, err := httpClient.Request("google.com", client.Headers(map[string][]string{
		"fruits": {"banana", "orange"},
	}))
	if err != nil {
		return
	}

	headers, err := request.Headers().Get("fruits")
	if err != nil {
		return
	}

	if slices.Contains(headers, "banana") == false || slices.Contains(headers, "orange") == false {
		return
	}

	fmt.Println("Success")
}
Output:

Success

func (*HttpRequestHeaders) GetAll

func (r *HttpRequestHeaders) GetAll() (map[string][]string, error)
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/http/client"
	"bitbucket.org/taubyte/go-sdk/http/client"
	"bitbucket.org/taubyte/go-sdk/utils/slices"
)

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

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

	request, err := httpClient.Request("google.com", client.Headers(map[string][]string{
		"fruits":       {"banana", "orange"},
		"Content-Type": {"application/json"},
	}))
	if err != nil {
		return
	}

	headers, err := request.Headers().GetAll()
	if err != nil {
		return
	}

	fruits := headers["fruits"]
	if slices.Contains(fruits, "banana") == false || slices.Contains(fruits, "orange") == false {
		return
	}

	if slices.Contains(headers["Content-Type"], "application/json") == false {
		return
	}

	fmt.Println("Success")
}
Output:

Success

func (*HttpRequestHeaders) List

func (r *HttpRequestHeaders) List() ([]string, error)
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/http/client"
	"bitbucket.org/taubyte/go-sdk/http/client"
	"bitbucket.org/taubyte/go-sdk/utils/slices"
)

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

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

	request, err := httpClient.Request("google.com", client.Headers(map[string][]string{
		"fruits":       {"banana"},
		"Content-Type": {"application/json"},
	}))
	if err != nil {
		return
	}

	headerKeys, err := request.Headers().List()
	if err != nil {
		return
	}

	if slices.Contains(headerKeys, "fruits") == false || slices.Contains(headerKeys, "Content-Type") == false {
		return
	}

	fmt.Println("Success")
}
Output:

Success

func (*HttpRequestHeaders) Set

func (r *HttpRequestHeaders) Set(key string, values ...string) error
Example
package main

import (
	"fmt"
	"strings"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/http/client"
	"bitbucket.org/taubyte/go-sdk/http/client"
	"bitbucket.org/taubyte/go-sdk/utils/slices"
)

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

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

	request, err := httpClient.Request("google.com")
	if err != nil {
		return
	}

	testHeaders := map[string][]string{
		"content-type": {"json", "yaml"},
		"fruits":       {"apple", "orange"},
	}
	for key, values := range testHeaders {
		err = request.Headers().Set(key, values...)
		if err != nil {
			return
		}
	}

	for key, values := range testHeaders {
		for _, v := range values {
			if slices.Contains(m.Headers[key], v) == false {
				fmt.Printf("Expected %s to contain %s", strings.Join(testHeaders[key], ", \n"), v)
				return
			}
		}
	}

	err = request.Headers().Set("fruits", "banana")
	if err != nil {
		return
	}

	fruits := m.Headers["fruits"]
	if len(fruits) != 1 || fruits[0] != "banana" {
		return
	}

	fmt.Println("Success")
}
Output:

Success

type HttpRequestMethod

type HttpRequestMethod HttpRequest

func (*HttpRequestMethod) Get

func (r *HttpRequestMethod) Get() (string, error)

func (*HttpRequestMethod) Set

func (r *HttpRequestMethod) Set(method string) error

type HttpRequestOption

type HttpRequestOption func(HttpRequest) error

func Body

func Body(data []byte) HttpRequestOption

func Headers

func Headers(headers map[string][]string) HttpRequestOption

func Method

func Method(method string) HttpRequestOption

type HttpResponse

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

func (*HttpResponse) Body

func (resp *HttpResponse) Body() *HttpResponseBody

type HttpResponseBody

type HttpResponseBody HttpResponse

func (*HttpResponseBody) Close

func (resp *HttpResponseBody) Close() error

func (*HttpResponseBody) Read

func (resp *HttpResponseBody) Read(p []byte) (int, error)

Jump to

Keyboard shortcuts

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