<commit_before>use v6.c;

use URI::Template;
use HTTP::Request::Common;


use HTTP::UserAgent ();

class Sofa::UserAgent is HTTP::UserAgent {
    has Str             $.host              = 'localhost';
    has Int             $.port              = 5984;
    has Bool            $.secure            = False;
    has Str             $.base-url;
    has URI::Template   $!base-template;
    has                 %.default-headers   = (Accept => "application/json", Content-Type => "application/json");

    subset FromJSON of Mu where { $_.can('from-json') };
    subset ToJSON   of Mu where { $_.can('to-json') };
    subset LikeJSONClass of Mu where all(FromJSON,ToJSON);

    role CouchResponse {
        method is-json() returns Bool {
            if self.content-type eq 'application/json' {
                True
            }
            else {
                False
            }
        }

        multi method from-json() {
            from-json(self.content);
        }
        multi method from-json(FromJSON $c) {
            $c.from-json(self.content);
        }

    }

    method base-url() returns Str {
        if not $!base-url.defined {
            $!base-url = 'http' ~ ($!secure ?? 's' !! '') ~ '://' ~ $!host ~ ':' ~ $!port.Str ~ '{/path*}{?params*}';
        }
        $!base-url;
    }

    method base-template() returns URI::Template handles <process> {
        if not $!base-template.defined {
            $!base-template = URI::Template.new(template => self.base-url);
        }
        $!base-template;
    }

    proto method get(|c) { * }

    multi method get(:$path!, :$params, *%headers) returns CouchResponse {
        self.request(GET(self.process(:$path, :$params), |%!default-headers, |%headers)) but CouchResponse;
    }

    proto method put(|c) { * }

    multi method put(:$path!, :$params, Str :$content, *%headers) returns CouchResponse {
        self.request(PUT(self.process(:$path, :$params), :$content, |%!default-headers, |%headers)) but CouchResponse;
    }

    multi method put(:$path!, :$params, :%content, *%headers) returns CouchResponse {
        samewith(:$path, :$params, content => to-json(%content), |%headers);
    }

    multi method put(:$path!, :$params, ToJSON :$content, *%headers) returns CouchResponse {
        samewith(:$path, :$params, content => $content.to-json, |%headers);
    }

    proto method post(|c) { * }

    multi method post(:$path!, :$params, Str :$content, :%form, *%headers) returns CouchResponse {
        if %form {
            # Need to force this here
            my %h = Content-Type => 'application/x-www-form-urlencoded', content-type => 'application/x-www-form-urlencoded';
            self.request(POST(self.process(:$path, :$params), %form, |%!default-headers, |%headers, |%h)) but CouchResponse;
        }
        else {
            self.request(POST(self.process(:$path, :$params), :$content, |%!default-headers, |%headers)) but CouchResponse;
        }
    }

    multi method post(:$path!, :$params, :%content, *%headers) returns CouchResponse {
        samewith(:$path, :$params, content => to-json(%content), |%headers);
    }

    multi method post(:$path!, :$params,  ToJSON :$content, *%headers) returns CouchResponse {
        samewith(:$path, :$params, content => $content.to-json, |%headers);
    }

    proto method delete(|c) { * }

    multi method delete(:$path!, :$params, *%headers) returns CouchResponse {
        self.request(DELETE(self.process(:$path, :$params), |%!default-headers, |%headers)) but CouchResponse;
    }
}

# vim: expandtab shiftwidth=4 ft=perl6
<commit_msg>Add Buf candidate for put to support attachments<commit_after>use v6.c;

use URI::Template;
use HTTP::Request::Common;


use HTTP::UserAgent ();

class Sofa::UserAgent is HTTP::UserAgent {
    has Str             $.host              = 'localhost';
    has Int             $.port              = 5984;
    has Bool            $.secure            = False;
    has Str             $.base-url;
    has URI::Template   $!base-template;
    has                 %.default-headers   = (Accept => "application/json", Content-Type => "application/json");

    subset FromJSON of Mu where { $_.can('from-json') };
    subset ToJSON   of Mu where { $_.can('to-json') };
    subset LikeJSONClass of Mu where all(FromJSON,ToJSON);

    role CouchResponse {
        method is-json() returns Bool {
            if self.content-type eq 'application/json' {
                True
            }
            else {
                False
            }
        }

        multi method from-json() {
            from-json(self.content);
        }
        multi method from-json(FromJSON $c) {
            $c.from-json(self.content);
        }

    }

    method base-url() returns Str {
        if not $!base-url.defined {
            $!base-url = 'http' ~ ($!secure ?? 's' !! '') ~ '://' ~ $!host ~ ':' ~ $!port.Str ~ '{/path*}{?params*}';
        }
        $!base-url;
    }

    method base-template() returns URI::Template handles <process> {
        if not $!base-template.defined {
            $!base-template = URI::Template.new(template => self.base-url);
        }
        $!base-template;
    }

    proto method get(|c) { * }

    multi method get(:$path!, :$params, *%headers) returns CouchResponse {
        self.request(GET(self.process(:$path, :$params), |%!default-headers, |%headers)) but CouchResponse;
    }

    proto method put(|c) { * }

    multi method put(:$path!, :$params, Blob :$content!, *%headers) returns CouchResponse {
        self.request(PUT(self.process(:$path, :$params), :$content, |%!default-headers, |%headers)) but CouchResponse;
    }
    multi method put(:$path!, :$params, Str :$content, *%headers) returns CouchResponse {
        self.request(PUT(self.process(:$path, :$params), :$content, |%!default-headers, |%headers)) but CouchResponse;
    }

    multi method put(:$path!, :$params, :%content, *%headers) returns CouchResponse {
        samewith(:$path, :$params, content => to-json(%content), |%headers);
    }

    multi method put(:$path!, :$params, ToJSON :$content, *%headers) returns CouchResponse {
        samewith(:$path, :$params, content => $content.to-json, |%headers);
    }

    proto method post(|c) { * }

    multi method post(:$path!, :$params, Str :$content, :%form, *%headers) returns CouchResponse {
        if %form {
            # Need to force this here
            my %h = Content-Type => 'application/x-www-form-urlencoded', content-type => 'application/x-www-form-urlencoded';
            self.request(POST(self.process(:$path, :$params), %form, |%!default-headers, |%headers, |%h)) but CouchResponse;
        }
        else {
            self.request(POST(self.process(:$path, :$params), :$content, |%!default-headers, |%headers)) but CouchResponse;
        }
    }

    multi method post(:$path!, :$params, :%content, *%headers) returns CouchResponse {
        samewith(:$path, :$params, content => to-json(%content), |%headers);
    }

    multi method post(:$path!, :$params,  ToJSON :$content, *%headers) returns CouchResponse {
        samewith(:$path, :$params, content => $content.to-json, |%headers);
    }

    proto method delete(|c) { * }

    multi method delete(:$path!, :$params, *%headers) returns CouchResponse {
        self.request(DELETE(self.process(:$path, :$params), |%!default-headers, |%headers)) but CouchResponse;
    }
}

# vim: expandtab shiftwidth=4 ft=perl6

{'fraction_non_alphanumeric': 0.16662180349932706, 'fraction_numerical': 0.0018842530282637954, 'mean_word_length': 2.470808033629145, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1962793', 'n_tokens_mistral': 2336, 'n_tokens_neox': 2261, 'n_words': 533}