www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Vibe.d web interface tutorial

reply aberba <karabutaworld gmail.com> writes:
New blog post for the learning audience

aberba.com/2018/using-vibe-d-web-interface
Mar 09
parent reply aberba <karabutaworld gmail.com> writes:
On Friday, 9 March 2018 at 16:32:28 UTC, aberba wrote:
 New blog post for the learning audience

 aberba.com/2018/using-vibe-d-web-interface
http://aberba.com/2018/using-vibe-d-web-interface
Mar 09
next sibling parent reply Martin Tschierschke <mt smartdolphin.de> writes:
On Friday, 9 March 2018 at 16:34:52 UTC, aberba wrote:
 On Friday, 9 March 2018 at 16:32:28 UTC, aberba wrote:
 New blog post for the learning audience

 aberba.com/2018/using-vibe-d-web-interface
http://aberba.com/2018/using-vibe-d-web-interface
Thank you! I linke it.
Mar 10
parent aberba <karabutaworld gmail.com> writes:
On Saturday, 10 March 2018 at 15:03:59 UTC, Martin Tschierschke 
wrote:
 On Friday, 9 March 2018 at 16:34:52 UTC, aberba wrote:
 On Friday, 9 March 2018 at 16:32:28 UTC, aberba wrote:
 New blog post for the learning audience

 aberba.com/2018/using-vibe-d-web-interface
http://aberba.com/2018/using-vibe-d-web-interface
Thank you! I linke it.
Glad you did.
Mar 12
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 3/9/18 11:34 AM, aberba wrote:
 On Friday, 9 March 2018 at 16:32:28 UTC, aberba wrote:
 New blog post for the learning audience

 aberba.com/2018/using-vibe-d-web-interface
http://aberba.com/2018/using-vibe-d-web-interface
Very nice! Although this is missing one of my favorite vibe.d web interface features -- automatic parsing of route parameters: class WebInterface { void getRoute(HTTPServerResponse res, int foo, Nullable!int bar) { import std.format; res.writeBody(format("foo = %s, bar = %s", foo, bar)); } } /route => Error, foo required /route?foo=1 => "foo = 1, bar = Nullable.null" /route?foo=2&bar=5 => "foo = 1, bar = 5" -Steve
Mar 13
next sibling parent reply Daniel Kozak <kozzi11 gmail.com> writes:
On contrary I really hate that :D.
Because of this:

HTTP method Recognized prefixes
GET get, query
PUT set, put
POST add, create, post
DELETE remove, erase, delete
PATCH update, patch

I am calling vibed from PHP and I need to tweak my curl for every request
baceuse of this :D

 public function sendRequest($method, $params = []) {

        $method = strtolower(preg_replace('/([^A-Z])([A-Z])/', "$1_$2",
$method));
        $isGetRequestType = substr($method, 0, 3) == 'get';
        $isPutRequestType = substr($method, 0, 3) == 'set';
        $httpHeader = $isGetRequestType ? [] : ['Content-Type:
application/json'];
        if ($this->sessionId) {
            $httpHeader[] = "Cookie: vibe.session_id=" . $this->sessionId .
"; Path=/; HttpOnly";
        }

        if ($isGetRequestType) {
            $url =
UrlBuilder::fromUrl($this->url)->appendPath(substr($method, 4));
            $url->addParams($params);
            $curl = curl_init($url);
            curl_setopt_array($curl, [
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_HTTPHEADER => $httpHeader,
                CURLOPT_USERAGENT => EdiClient::USER_AGENT,
                CURLOPT_URL => $url->toString(),
                CURLOPT_RETURNTRANSFER => true
            ]);
        } elseif ($isPutRequestType) {
            $url =
UrlBuilder::fromUrl($this->url)->appendPath(substr($method, 4));
            $curl = curl_init($url);
            $json = json_encode($params);

            curl_setopt_array($curl, [
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_HTTPHEADER => $httpHeader,
                CURLOPT_USERAGENT => EdiClient::USER_AGENT,
                CURLOPT_URL => $url->toString(),
                CURLOPT_CUSTOMREQUEST => "PUT",
                CURLOPT_POSTFIELDS => $json,
                CURLOPT_RETURNTRANSFER => true
            ]);
        } else {
            $url = UrlBuilder::fromUrl($this->url)->appendPath($method);
            $curl = curl_init($url);
            $json = json_encode($params);

            curl_setopt_array($curl, [
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_HTTPHEADER => $httpHeader,
                CURLOPT_USERAGENT => EdiClient::USER_AGENT,
                CURLOPT_URL => $url->toString(),
                CURLOPT_POST => true,
                CURLOPT_POSTFIELDS => $json,
                CURLOPT_RETURNTRANSFER => true
            ]);
        }

        $result = curl_exec($curl);
        curl_close($curl);
        $result = json_decode($result, true);

        return $result;
    }


On Tue, Mar 13, 2018 at 11:12 AM, Steven Schveighoffer via
Digitalmars-d-announce <digitalmars-d-announce puremagic.com> wrote:

 On 3/9/18 11:34 AM, aberba wrote:

 On Friday, 9 March 2018 at 16:32:28 UTC, aberba wrote:

 New blog post for the learning audience

 aberba.com/2018/using-vibe-d-web-interface
http://aberba.com/2018/using-vibe-d-web-interface
Very nice! Although this is missing one of my favorite vibe.d web interface features -- automatic parsing of route parameters: class WebInterface { void getRoute(HTTPServerResponse res, int foo, Nullable!int bar) { import std.format; res.writeBody(format("foo = %s, bar = %s", foo, bar)); } } /route => Error, foo required /route?foo=1 => "foo = 1, bar = Nullable.null" /route?foo=2&bar=5 => "foo = 1, bar = 5" -Steve
Mar 13
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 3/13/18 6:22 AM, Daniel Kozak wrote:
 On contrary I really hate that :D.
 Because of this:
 
 HTTP methodRecognized prefixes
 GETget, query
 PUTset, put
 POSTadd, create, post
 DELETEremove, erase, delete
 PATCHupdate, patch
 
 I am calling vibed from PHP and I need to tweak my curl for every 
 request baceuse of this :D
That's not what I was talking about, I was talking about how the parameters are automatically parsed passed to the web functions. It doesn't affect how the routes are called externally, just how the parameters are dealt with. Note, you can override the method for any/all routes: class WebInterface { method(HTTPMethod.GET): ... // all methods are now get } I think perhaps the root cause of your problem is using PHP ;) -Steve
Mar 13
parent reply Daniel Kozak <kozzi11 gmail.com> writes:
Yes PHP is always to blame :)

On Tue, Mar 13, 2018 at 1:55 PM, Steven Schveighoffer via
Digitalmars-d-announce <digitalmars-d-announce puremagic.com> wrote:

 On 3/13/18 6:22 AM, Daniel Kozak wrote:

 On contrary I really hate that :D.
 Because of this:

 HTTP methodRecognized prefixes
 GETget, query
 PUTset, put
 POSTadd, create, post
 DELETEremove, erase, delete
 PATCHupdate, patch

 I am calling vibed from PHP and I need to tweak my curl for every request
 baceuse of this :D
That's not what I was talking about, I was talking about how the parameters are automatically parsed passed to the web functions. It doesn't affect how the routes are called externally, just how the parameters are dealt with. Note, you can override the method for any/all routes: class WebInterface { method(HTTPMethod.GET): ... // all methods are now get } I think perhaps the root cause of your problem is using PHP ;) -Steve
Mar 13
next sibling parent aberba <karabutaworld gmail.com> writes:
On Tuesday, 13 March 2018 at 13:42:20 UTC, Daniel Kozak wrote:
 Yes PHP is always to blame :)
I can testify I've never written PHP code as clean as yours above. Ha ha. Even when I used PHP heavily. I suck a lil bit at naming things. I really loved PHP but ... vibe.d happened.
 On Tue, Mar 13, 2018 at 1:55 PM, Steven Schveighoffer via 
 Digitalmars-d-announce <digitalmars-d-announce puremagic.com> 
 wrote:

 On 3/13/18 6:22 AM, Daniel Kozak wrote:

[...]
That's not what I was talking about, I was talking about how the parameters are automatically parsed passed to the web functions. It doesn't affect how the routes are called externally, just how the parameters are dealt with. Note, you can override the method for any/all routes: class WebInterface { method(HTTPMethod.GET): ... // all methods are now get } I think perhaps the root cause of your problem is using PHP ;) -Steve
Mar 13
prev sibling parent "Nick Sabalausky (Abscissa)" <SeeWebsiteToContactMe semitwist.com> writes:
On 03/13/2018 09:42 AM, Daniel Kozak wrote:
 Yes PHP is always to blame :)
 
From my archives: https://semitwist.com/articles/article/view/10-fun-facts-about-php
Mar 13
prev sibling parent reply aberba <karabutaworld gmail.com> writes:
On Tuesday, 13 March 2018 at 10:12:24 UTC, Steven Schveighoffer 
wrote:
 On 3/9/18 11:34 AM, aberba wrote:
 On Friday, 9 March 2018 at 16:32:28 UTC, aberba wrote:
 New blog post for the learning audience

 aberba.com/2018/using-vibe-d-web-interface
http://aberba.com/2018/using-vibe-d-web-interface
Very nice! Although this is missing one of my favorite vibe.d web interface features -- automatic parsing of route parameters: class WebInterface { void getRoute(HTTPServerResponse res, int foo, Nullable!int bar) { import std.format; res.writeBody(format("foo = %s, bar = %s", foo, bar)); } } /route => Error, foo required /route?foo=1 => "foo = 1, bar = Nullable.null" /route?foo=2&bar=5 => "foo = 1, bar = 5" -Steve
I don't know why but I dont really use that feature often. I tend to access them with req.query.get("param") and req.form.get("param") directly. Don't know why... time to save some key strokes. ...but how does file handling work with this approach? input(type="file"...)
Mar 13
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 3/13/18 4:42 PM, aberba wrote:
 On Tuesday, 13 March 2018 at 10:12:24 UTC, Steven Schveighoffer wrote:
 On 3/9/18 11:34 AM, aberba wrote:
 http://aberba.com/2018/using-vibe-d-web-interface
Very nice! Although this is missing one of my favorite vibe.d web interface features -- automatic parsing of route parameters:
I don't know why but I dont really use that feature often. I tend to access them with req.query.get("param") and req.form.get("param") directly. Don't know why... time to save some key strokes. ...but how does file handling work with this approach? input(type="file"...)
I don't know, I haven't used it in that capacity. The reason why I like it is because it validates the text for you (in cases where you aren't looking for strings), and because it feels more like calling a function from a browser rather than a protocol parsing exercise ;) -Steve
Mar 15