Util.pm 904 B

123456789101112131415161718192021222324252627282930313233343536
  1. package Playwright::Util;
  2. use strict;
  3. use warnings;
  4. use JSON::MaybeXS();
  5. use Carp qw{confess};
  6. #ABSTRACT: Common utility functions for the Playwright module
  7. no warnings 'experimental';
  8. use feature qw{signatures};
  9. =head2 request(STRING method, STRING url, INTEGER port, LWP::UserAgent ua, HASH args) = HASH
  10. De-duplicates request logic in the Playwright Modules.
  11. =cut
  12. sub request ($method, $url, $port, $ua, %args) {
  13. my $fullurl = "http://localhost:$port/$url";
  14. my $request = HTTP::Request->new( $method, $fullurl);
  15. $request->header( 'Content-type' => 'application/json' );
  16. $request->content( JSON::MaybeXS::encode_json(\%args) );
  17. my $response = $ua->request($request);
  18. my $content = $response->decoded_content();
  19. my $decoded = JSON::MaybeXS::decode_json($content);
  20. my $msg = $decoded->{message};
  21. confess($msg) if $decoded->{error};
  22. return $msg;
  23. }
  24. 1;