Skip to content

clients.httpconnection #

HTTP connection

http client with caching in redis (default disabled)


//as given to get
pub struct Request {
    method        Method
    prefix        string
    id            string
    params        map[string]string
    data          string
    cache_disable bool = true
    header        Header
    dict_key      string
}

import freeflowuniverse.crystallib.clients.httpconnection

mut conn := httpconnection.new(name: 'coredns', url: 'http://localhost:3334')!	

r := conn.get(prefix: 'health')!
println(r)

if r.trim_space == 'OK' {
    return
}

fn get #

fn get(name string) !&HTTPConnection

fn new #

fn new(args HTTPConnectionArgs) !&HTTPConnection

fn new_request #

fn new_request(args_ Request) !&Request

get new request

method        Method (.get, .post, .put, ...)
prefix        string
id            string
params        map[string]string
data          string
cache_disable bool = true
header        Header
dict_key      string
```	
for header see https://modules.vlang.io/net.http.html#Method . for method see https://modules.vlang.io/net.http.html#Header

struct CacheConfig #

struct CacheConfig {
pub mut:
	key               string // as used to identity in redis
	allowable_methods []Method = [.get, .head]
	allowable_codes   []int    = default_cacheable_codes
	disable           bool     = true // default cache is not working
	expire_after      int      = 3600 // default expire_after is 1h
	match_headers     bool // cache the request header to be matched later
}

struct HTTPConnection #

@[heap]
struct HTTPConnection {
pub mut:
	redis          Redis @[str: skip]
	base_url       string // the base url
	default_header Header
	cache          CacheConfig @[str: skip]
	retry          int = 5
}

fn (HTTPConnection) basic_auth #

fn (mut conn HTTPConnection) basic_auth(username string, password string)

fn (HTTPConnection) cache_drop #

fn (mut h HTTPConnection) cache_drop() !

drop full cache for specific cache_key

fn (HTTPConnection) clone #

fn (mut h HTTPConnection) clone() !&HTTPConnection

fn (HTTPConnection) delete #

fn (mut h HTTPConnection) delete(req_ Request) !string

Delete Request with json data and return response as string

fn (HTTPConnection) get #

fn (mut h HTTPConnection) get(req_ Request) !string

Get Request with json data and return response as string

fn (HTTPConnection) get_json #

fn (mut h HTTPConnection) get_json(req Request) !string

fn (HTTPConnection) get_json_dict #

fn (mut h HTTPConnection) get_json_dict(req Request) !map[string]json2.Any

do a request with certain prefix on the already specified url parse as json

fn (HTTPConnection) get_json_list #

fn (mut h HTTPConnection) get_json_list(req Request) ![]string

fn (HTTPConnection) post_json_str #

fn (mut h HTTPConnection) post_json_str(req_ Request) !string

fn (HTTPConnection) post_multi_part #

fn (mut h HTTPConnection) post_multi_part(req Request, form http.PostMultipartFormConfig) !http.Response

performs a multi part form data request

fn (HTTPConnection) send #

fn (mut h HTTPConnection) send(req Request) !Result

Core fucntion to be used in all other function

struct HTTPConnectionArgs #

@[params]
struct HTTPConnectionArgs {
pub:
	name  string @[required]
	url   string @[required]
	cache bool
	retry int = 1
}

struct HTTPConnections #

@[heap]
struct HTTPConnections {
pub mut:
	connections map[string]&HTTPConnection
}

struct Request #

@[params]
struct Request {
pub mut:
	method        Method
	prefix        string
	id            string
	params        map[string]string
	data          string
	cache_disable bool = true
	header        Header
	dict_key      string
	debug         bool
}

struct Result #

struct Result {
pub mut:
	code int
	data string
}

fn (Result) is_ok #

fn (r Result) is_ok() bool