File Coverage

blib/lib/Couch/DB/Mojolicious.pm
Criterion Covered Total %
statement 27 72 37.5
branch 0 18 0.0
condition 0 22 0.0
subroutine 9 17 52.9
pod 1 2 50.0
total 37 131 28.2


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Couch-DB version 0.201.
2             # The POD got stripped from this file by OODoc version 3.06.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2024-2026 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11             #oorestyle: no "use strict"
12             #oorestyle: no "use warnings"
13              
14             # SPDX-FileCopyrightText: 2024 Mark Overmeer <mark@overmeer.net>
15             # SPDX-License-Identifier: Artistic-2.0
16              
17             package Couch::DB::Mojolicious;{
18             our $VERSION = '0.201';
19             }
20              
21 1     1   1545 use parent 'Couch::DB';
  1         3  
  1         11  
22 1     1   118 use feature 'state';
  1         2  
  1         162  
23              
24 1     1   8 use Log::Report 'couch-db';
  1         6  
  1         11  
25 1     1   419 use Couch::DB::Util qw/flat/;
  1         2  
  1         10  
26              
27 1     1   9 use Scalar::Util qw/blessed/;
  1         2  
  1         76  
28 1     1   8 use Mojo::URL ();
  1         2  
  1         37  
29 1     1   6 use Mojo::UserAgent ();
  1         3  
  1         44  
30 1     1   8 use Mojo::JSON qw/decode_json/;
  1         2  
  1         90  
31 1     1   8 use HTTP::Status qw/HTTP_OK/;
  1         3  
  1         1306  
32              
33             #--------------------
34              
35             sub init($)
36 0     0 0   { my ($self, $args) = @_;
37              
38             $args->{to_perl} = +{
39 0     0     abs_uri => sub { Mojo::URL->new($_[2]) },
40 0           };
41              
42 0           $self->SUPER::init($args);
43             }
44              
45             #--------------------
46              
47             #--------------------
48              
49             sub createClient(%)
50 0     0 1   { my ($self, %args) = @_;
51 0           $args{couch} = $self;
52              
53 0   0       my $server = $args{server} || panic "Requires 'server'";
54 0 0 0       $args{server} = Mojo::URL->new("$server")
55             unless blessed $server && $server->isa('Mojo::URL');
56              
57 0   0       my $ua = $args{user_agent} ||= state $ua_shared = Mojo::UserAgent->new;
58 0 0 0       blessed $ua && $ua->isa('Mojo::UserAgent') or panic "Illegal user_agent";
59              
60 0           $self->SUPER::createClient(%args);
61             }
62              
63             #method call
64              
65             sub _callClient($$%)
66 0     0     { my ($self, $result, $client, %args) = @_;
67              
68 0 0         my $method = delete $args{method} or panic;
69 0   0       my $delay = delete $args{delay} || 0;
70 0           my $path = delete $args{path};
71 0           my $query = delete $args{query};
72 0           my $send = delete $args{send};
73              
74 0           my $ua = $client->userAgent;
75 0           my %headers = ( %{$client->headers}, %{delete $args{headers}} );
  0            
  0            
76             #warn "HEADERS = ", join ';', %headers;
77              
78 0           my $url = $client->server->clone->path($path);
79 0 0         $url->query($query) if $query;
80              
81             my @body
82             = ! defined $send ? ()
83 0 0         : $headers{'Content-Type'} eq 'application/json' ? (json => $send)
    0          
84             : $send;
85              
86             # $tx is a Mojo::Transaction::HTTP
87 0           my $tx = $ua->build_tx($method => $url, \%headers, @body);
88              
89             my $plan = $ua->start_p($tx)->then(sub ($) {
90 0     0     my $tx = shift;
91 0           my $response = $tx->res;
92 0           $result->setFinalResult({
93             client => $client,
94             request => $tx->req,
95             response => $response,
96             code => $response->code,
97             message => $response->message,
98             });
99 0           });
100              
101 0 0         if($delay)
102 0           { $result->setResultDelayed({ client => $client });
103             }
104             else
105 0           { $plan->wait;
106             }
107              
108 0           1;
109             }
110              
111             sub _extractAnswer($)
112 0     0     { my ($self, $response) = @_;
113 0           my $content = $response->content;
114              
115 0 0         $response->content->is_multipart
116             or return $response->json;
117              
118 0           my $part = $response->content->parts->[0];
119 0           decode_json $part->asset->slurp;
120             }
121              
122             sub _attachment($$)
123 0     0     { my ($self, $response, $name) = @_;
124 0   0       my $parts = $response->content->parts || [];
125 0           foreach my $part (@$parts)
126 0           { my $disp = $part->headers->content_disposition;
127 0 0 0       return $part->asset->slurp
      0        
128             if $disp && $disp =~ /filename="([^"]+)"/ && $1 eq $name;
129             }
130 0           undef;
131             }
132              
133 0     0     sub _messageContent($) { $_[1]->body }
134              
135             1;