Skip to content

Commit

Permalink
update code for v 0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
alexferl committed Jul 7, 2023
1 parent 87e98a5 commit ebe47cc
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 68 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ vest
*.dylib
*.dll
vls.log

.idea
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand Down
58 changes: 31 additions & 27 deletions client.v
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub mut:

// json will attempt to decode the body to json
// with the provided struct.
pub fn (r Response) json<T>() ?T {
pub fn (r Response) json[T]() !T {
return json.decode(T, r.body)
}

Expand All @@ -46,10 +46,10 @@ pub fn new(opt ...ClientOption) &Client {
}

// new_request creates a new Request.
pub fn (c &Client) new_request(ctx context.Context, method http.Method, url string, body io.Reader) ?&Request {
pub fn (c &Client) new_request(ctx context.Context, method http.Method, url string, body io.Reader) !&Request {
mut u := url
if c.opts.base_url != '' {
u = '$c.opts.base_url$url'
u = '${c.opts.base_url}${url}'
}

mut headers := map[string]string{}
Expand All @@ -76,12 +76,12 @@ pub fn (c &Client) new_request(ctx context.Context, method http.Method, url stri
cookies[k] = v
}

data := io.read_all(reader: body)?
data := io.read_all(reader: body)!

return &Request{
version: c.opts.version
method: method
header: http.new_custom_header_from_map(headers)?
header: http.new_custom_header_from_map(headers)!
cookies: cookies
data: data.bytestr()
url: u
Expand All @@ -97,15 +97,17 @@ pub fn (c &Client) new_request(ctx context.Context, method http.Method, url stri
}

// do sends a Request and returns a Response.
pub fn (c &Client) do(mut req Request) ?&Response {
pub fn (c &Client) do(mut req Request) !&Response {
unsafe {
if c.opts.before_request != nil {
c.opts.before_request(mut req)?
if c.opts.before_request.len > 0 {
for f in c.opts.before_request {
f(mut req)!
}
}
}
resp_ch := chan http.Response{}
err_ch := chan IError{}
go fn (resp_ch chan http.Response, err_ch chan IError, mut req Request) ? {
go fn (resp_ch chan http.Response, err_ch chan IError, mut req Request) ! {
resp := req.do() or {
err_ch <- err
return
Expand All @@ -122,8 +124,10 @@ pub fn (c &Client) do(mut req Request) ?&Response {
resp := <-resp_ch {
mut r := &Response{resp, req}
unsafe {
if c.opts.after_request != nil {
c.opts.after_request(mut r)?
if c.opts.after_request.len > 0 {
for f in c.opts.after_request {
f(mut r)!
}
}
}
return r
Expand All @@ -133,52 +137,52 @@ pub fn (c &Client) do(mut req Request) ?&Response {
}
}

return none
return error('error running request')
}

// get sends a GET request and returns a Response.
pub fn (c &Client) get(ctx context.Context, url string) ?&Response {
mut req := c.new_request(ctx, http.Method.get, url, bytes.new_buffer([]u8{}))?
pub fn (c &Client) get(ctx context.Context, url string) !&Response {
mut req := c.new_request(ctx, http.Method.get, url, bytes.new_buffer([]u8{}))!
return c.do(mut req)
}

// post sends a POST request and returns a Response.
pub fn (c &Client) post(ctx context.Context, url string, body io.Reader) ?&Response {
mut req := c.new_request(ctx, http.Method.post, url, body)?
pub fn (c &Client) post(ctx context.Context, url string, body io.Reader) !&Response {
mut req := c.new_request(ctx, http.Method.post, url, body)!
return c.do(mut req)
}

// put sends a PUT request and returns a Response.
pub fn (c &Client) put(ctx context.Context, url string, body io.Reader) ?&Response {
mut req := c.new_request(ctx, http.Method.put, url, body)?
pub fn (c &Client) put(ctx context.Context, url string, body io.Reader) !&Response {
mut req := c.new_request(ctx, http.Method.put, url, body)!
return c.do(mut req)
}

// head sends a HEAD request and returns a Response.
pub fn (c &Client) head(ctx context.Context, url string) ?&Response {
mut req := c.new_request(ctx, http.Method.head, url, bytes.new_buffer([]u8{}))?
pub fn (c &Client) head(ctx context.Context, url string) !&Response {
mut req := c.new_request(ctx, http.Method.head, url, bytes.new_buffer([]u8{}))!
return c.do(mut req)
}

// delete sends a DELETE request and returns a Response.
pub fn (c &Client) delete(ctx context.Context, url string) ?&Response {
mut req := c.new_request(ctx, http.Method.delete, url, bytes.new_buffer([]u8{}))?
pub fn (c &Client) delete(ctx context.Context, url string) !&Response {
mut req := c.new_request(ctx, http.Method.delete, url, bytes.new_buffer([]u8{}))!
return c.do(mut req)
}

// options sends a OPTIONS request and returns a Response.
pub fn (c &Client) options(ctx context.Context, url string) ?&Response {
mut req := c.new_request(ctx, http.Method.options, url, bytes.new_buffer([]u8{}))?
pub fn (c &Client) options(ctx context.Context, url string) !&Response {
mut req := c.new_request(ctx, http.Method.options, url, bytes.new_buffer([]u8{}))!
return c.do(mut req)
}

// patch sends a PATCH request and returns a Response.
pub fn (c &Client) patch(ctx context.Context, url string, body io.Reader) ?&Response {
mut req := c.new_request(ctx, http.Method.patch, url, body)?
pub fn (c &Client) patch(ctx context.Context, url string, body io.Reader) !&Response {
mut req := c.new_request(ctx, http.Method.patch, url, body)!
return c.do(mut req)
}

fn get_user_agent() string {
vm := vmod.decode(@VMOD_FILE) or { panic(err) }
return '$vm.name/$vm.version'
return '${vm.name}/${vm.version}'
}
20 changes: 10 additions & 10 deletions client_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import time
import bytes
import testing

fn before(mut req Request) ? {
fn before(mut req Request) ! {
req.version = http.Version.v2_0
}

fn after(mut resp Response) ? {
fn after(mut resp Response) ! {
resp.status_code = http.Status.created.int()
}

Expand Down Expand Up @@ -47,10 +47,10 @@ fn test_new_request() {
}

assert method == req.method
assert '$url$endpoint' == req.url
assert content_type == req.header.get(http.CommonHeader.content_type)?
assert '${url}${endpoint}' == req.url
assert content_type == req.header.get(http.CommonHeader.content_type)!
assert version == req.version
assert headers['X-Custom'] == req.header.get_custom('X-Custom')?
assert headers['X-Custom'] == req.header.get_custom('X-Custom')!
assert cookies == req.cookies
assert read_timeout == req.read_timeout
assert write_timeout == req.write_timeout
Expand Down Expand Up @@ -113,7 +113,7 @@ fn test_get() {
s.start() or { panic(err) }

c := new()
resp := c.get(context.background(), s.url)?
resp := c.get(context.background(), s.url)!

assert method == resp.request.method
assert body == resp.body
Expand All @@ -129,7 +129,7 @@ fn test_post() {
s.start() or { panic(err) }

c := new()
resp := c.post(context.background(), s.url, bytes.new_buffer(body.bytes()))?
resp := c.post(context.background(), s.url, bytes.new_buffer(body.bytes()))!

assert method == resp.request.method
assert body == resp.body
Expand All @@ -152,9 +152,9 @@ fn test_post_json() {
s.start() or { panic(err) }

c := new()
resp := c.post(context.background(), s.url, bytes.new_buffer(body.bytes()))?
resp := c.post(context.background(), s.url, bytes.new_buffer(body.bytes()))!

assert hello == resp.json<Hello>()?
assert hello == resp.json[Hello]()!
}

fn test_put() {
Expand Down Expand Up @@ -239,7 +239,7 @@ fn test_after_request() {
s.start() or { panic(err) }

c := new(with_after_request(after))
resp := c.get(context.background(), s.url)?
resp := c.get(context.background(), s.url)!

assert http.Status.created.int() == resp.status_code
}
4 changes: 2 additions & 2 deletions examples/after_request.v
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import context
import alexferl.vest

fn after(mut resp vest.Response) ? {
println('received $resp.status_code: $resp.status_msg')
fn after(mut resp vest.Response) ! {
println('received ${resp.status_code}: ${resp.status_msg}')
}

fn main() {
Expand Down
4 changes: 2 additions & 2 deletions examples/before_request.v
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import context
import alexferl.vest

fn before(mut req vest.Request) ? {
println('sending $req.method to $req.url')
fn before(mut req vest.Request) ! {
println('sending ${req.method} to ${req.url}')
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/json_get.v
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() {
return
}

mut r := resp.json<Response>() or {
mut r := resp.json[Response]() or {
eprintln('Failed to parse json')
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/json_post.v
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() {
return
}

mut r := resp.json<Response>() or {
mut r := resp.json[Response]() or {
eprintln('Failed to parse json')
return
}
Expand Down
16 changes: 8 additions & 8 deletions option.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ module vest
import net.http
import time

type BeforeFn = fn (mut Request) ?
type BeforeFn = fn (mut Request) !

type AfterFn = fn (mut Response) ?
type AfterFn = fn (mut Response) !

struct Options {
mut:
base_url string
accept string = 'application/json, */*;q=0.5'
content_type string = 'application/json'
auth string
before_request BeforeFn = unsafe { nil }
after_request AfterFn = unsafe { nil }
before_request []BeforeFn = []
after_request []AfterFn = []

version http.Version = http.Version.v1_1
headers map[string]string
Expand Down Expand Up @@ -83,17 +83,17 @@ pub fn with_auth(s string) ClientOption {

// with_before_request sets a function that will be run
// after the request is built, but before it's sent.
pub fn with_before_request(f fn (mut req Request) ?) ClientOption {
pub fn with_before_request(f fn (mut req Request) !) ClientOption {
return new_fn_option(fn [f] (mut o Options) {
o.before_request = f
o.before_request << f
})
}

// with_after_request sets a function that will be run
// after the request is run, before the response is returned.
pub fn with_after_request(f fn (mut req Response) ?) ClientOption {
pub fn with_after_request(f fn (mut req Response) !) ClientOption {
return new_fn_option(fn [f] (mut o Options) {
o.after_request = f
o.after_request << f
})
}

Expand Down
30 changes: 26 additions & 4 deletions option_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,44 @@ fn test_with_content_type() {
assert s == opts.content_type
}

fn before_test(mut req Request) ? {}
fn before_test(mut req Request) ! {}

fn test_with_before_request() {
mut opts := Options{}
with_before_request(before_test).apply(mut opts)

assert before_test == opts.before_request
assert before_test == opts.before_request[0]
}

fn after_test(mut resp Response) ? {}
fn before_test2(mut req Request) ! {}

fn test_with_before_request_multiple() {
mut opts := Options{}
with_before_request(before_test).apply(mut opts)
with_before_request(before_test2).apply(mut opts)

assert before_test == opts.before_request[0]
assert before_test2 == opts.before_request[1]
}

fn after_test(mut resp Response) ! {}

fn test_with_after_request() {
mut opts := Options{}
with_after_request(after_test).apply(mut opts)

assert after_test == opts.after_request
assert after_test == opts.after_request[0]
}

fn after_test2(mut resp Response) ! {}

fn test_with_after_request_multiple() {
mut opts := Options{}
with_after_request(after_test).apply(mut opts)
with_after_request(after_test2).apply(mut opts)

assert after_test == opts.after_request[0]
assert after_test2 == opts.after_request[1]
}

fn test_with_auth() {
Expand Down
Loading

0 comments on commit ebe47cc

Please sign in to comment.