Util.pm 1.1 KB

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