<commit_before>package WebService::Ooyala;

use 5.006;
use strict;
use warnings FATAL => 'all';
use URI::Escape qw(uri_escape);
use LWP::UserAgent;
use Carp qw(croak);
use Digest::SHA qw(sha256_base64);
use JSON;

=head1 NAME

WebService::Ooyala - Perl interface to Ooyala's API, currently only read
operations (GET requests) are supported

Support for create, update, and delete (PUT, POST, DELETE) operations will be added in future releases.

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';

=head1 SYNOPSIS

    my $ooyala = WebService::Ooyala->new({ api_key => $api_key, secret_key => $secret_key });

    # Grab all video assets (or at least the first page)
    my $data = $ooyala->get("assets");

    foreach my $video(@{$data->{items}}) {
        print "$video->{embed_code} $video->{name}\n";
    }

    # Get a particular video based on embed_code

    my $video = $data->get("assets/$embed_code");


=head1 EXPORT

A list of functions that can be exported.  You can delete this section
if you don't export anything, such as for a purely object-oriented module.

=head1 SUBROUTINES/METHODS

=head2 function1

=cut

sub new {
	my($class, $params) = @_;
	$params ||= {};

	my $self = {};
	$self->{api_key}    = $params->{api_key};
	$self->{secret_key} = $params->{secret_key};
	$self->{base_url}   = $params->{base_url} || "api.ooyala.com";
	$self->{cache_base_url} =
		$params->{cache_base_url} || "cdn-api.ooyala.com";
	$self->{expiration}  = $params->{expiration}  || 15;
	$self->{api_version} = $params->{api_version} || "v2";
	$self->{agent} =
		LWP::UserAgent->new(
		agent => "perl/$], WebService::Ooyala/" . $VERSION);

	bless $self, $class;

}

sub expires {
	my($self) = @_;
	my $now_plus_window = time() + $self->{expiration};
	return $now_plus_window + 300 - ($now_plus_window % 300);
}

sub send_request {
	my($self, $http_method, $relative_path, $body, $params) = @_;

	my $path = "/" . $self->{api_version} . "/" . $relative_path;

# TODO Convert the body to JSON format
#         json_body = ''
#                 if (body is not None):
#                             json_body = json.dumps(body) if type(body) is not str else body
#
	my $json_body = {};

	my $url =
		$self->build_path_with_authentication_params($http_method, $path,
		$params, "");

	# CHECK
	if (!$url) {
		return undef;
	}

	my $base_url;
	if ($http_method ne 'GET') {
		$base_url = $self->{base_url};
	} else {
		$base_url = $self->{cache_base_url};
	}

	print "$base_url$url\n";

	my $resp;
	if ($http_method eq 'GET') {
		$resp = $self->{agent}->get("https://" . $base_url . $url);

		if ($resp->is_success) {
			return decode_json($resp->decoded_content);
		}
	}
}

sub generate_signature {
	my($self, $http_method, $path, $params, $body) = @_;
	$body ||= '';

	my $signature = $self->{secret_key} . uc($http_method) . $path;

	foreach my $key (sort keys %$params) {
		$signature .= $key . "=" . $params->{$key};
	}

# TODO This is neccesary on python 2.7. if missing, signature+=body with raise an exception when body are bytes (image data)
#         signature = signature.encode('ascii')
#                 signature += body
#                         signature = base64.b64encode(hashlib.sha256(signature).digest())[0:43]
#                                 signature = urllib.quote_plus(signature)
	$signature = sha256_base64($signature);
	return $signature;
}

sub get {
	my($self, $path, $params) = @_;
	return $self->send_request('GET', $path, '', $params);
}

sub build_path {
	my($self, $path, $params) = @_;
	my $url = $path . '?';
	foreach (keys %$params) {
		$url .= "&$_=" . uri_escape($params->{$_});
	}
	return $url;
}

sub build_path_with_authentication_params {
	my($self, $http_method, $path, $params, $body) = @_;

# TODO
#if (http_method not in HTTP_METHODS) or (self.api_key is None) or (self.secret_key is None):
#           return None

	$params ||= {};
	my $authentication_params = {%$params};
	$authentication_params->{api_key} = $self->{api_key};
	$authentication_params->{expires} = $self->expires();
	$authentication_params->{signature} =
		$self->generate_signature($http_method, $path,
		$authentication_params, $body);
	return $self->build_path($path, $authentication_params);

}

sub get_api_key {
	my $self = shift;
	return $self->{api_key};
}

sub set_api_key {<commit_msg>Remove some TODO notes in source<commit_after>package WebService::Ooyala;

use 5.006;
use strict;
use warnings FATAL => 'all';
use URI::Escape qw(uri_escape);
use LWP::UserAgent;
use Carp qw(croak);
use Digest::SHA qw(sha256_base64);
use JSON;

=head1 NAME

WebService::Ooyala - Perl interface to Ooyala's API, currently only read
operations (GET requests) are supported

Support for create, update, and delete (PUT, POST, DELETE) operations will be added in future releases.

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';

=head1 SYNOPSIS

    my $ooyala = WebService::Ooyala->new({ api_key => $api_key, secret_key => $secret_key });

    # Grab all video assets (or at least the first page)
    my $data = $ooyala->get("assets");

    foreach my $video(@{$data->{items}}) {
        print "$video->{embed_code} $video->{name}\n";
    }

    # Get a particular video based on embed_code

    my $video = $data->get("assets/$embed_code");


=head1 EXPORT

A list of functions that can be exported.  You can delete this section
if you don't export anything, such as for a purely object-oriented module.

=head1 SUBROUTINES/METHODS

=head2 function1

=cut

sub new {
	my($class, $params) = @_;
	$params ||= {};

	my $self = {};
	$self->{api_key}    = $params->{api_key};
	$self->{secret_key} = $params->{secret_key};
	$self->{base_url}   = $params->{base_url} || "api.ooyala.com";
	$self->{cache_base_url} =
		$params->{cache_base_url} || "cdn-api.ooyala.com";
	$self->{expiration}  = $params->{expiration}  || 15;
	$self->{api_version} = $params->{api_version} || "v2";
	$self->{agent} =
		LWP::UserAgent->new(
		agent => "perl/$], WebService::Ooyala/" . $VERSION);

	bless $self, $class;

}

sub expires {
	my($self) = @_;
	my $now_plus_window = time() + $self->{expiration};
	return $now_plus_window + 300 - ($now_plus_window % 300);
}

sub send_request {
	my($self, $http_method, $relative_path, $body, $params) = @_;

	my $path = "/" . $self->{api_version} . "/" . $relative_path;

	my $json_body = {};

	my $url =
		$self->build_path_with_authentication_params($http_method, $path,
		$params, "");

	# CHECK
	if (!$url) {
		return undef;
	}

	my $base_url;
	if ($http_method ne 'GET') {
		$base_url = $self->{base_url};
	} else {
		$base_url = $self->{cache_base_url};
	}

	print "$base_url$url\n";

	my $resp;
	if ($http_method eq 'GET') {
		$resp = $self->{agent}->get("https://" . $base_url . $url);

		if ($resp->is_success) {
			return decode_json($resp->decoded_content);
		}
	}
}

sub generate_signature {
	my($self, $http_method, $path, $params, $body) = @_;
	$body ||= '';

	my $signature = $self->{secret_key} . uc($http_method) . $path;

	foreach my $key (sort keys %$params) {
		$signature .= $key . "=" . $params->{$key};
	}

	$signature = sha256_base64($signature);
	return $signature;
}

sub get {
	my($self, $path, $params) = @_;
	return $self->send_request('GET', $path, '', $params);
}

sub build_path {
	my($self, $path, $params) = @_;
	my $url = $path . '?';
	foreach (keys %$params) {
		$url .= "&$_=" . uri_escape($params->{$_});
	}
	return $url;
}

sub build_path_with_authentication_params {
	my($self, $http_method, $path, $params, $body) = @_;

	$params ||= {};
	my $authentication_params = {%$params};
	$authentication_params->{api_key} = $self->{api_key};
	$authentication_params->{expires} = $self->expires();
	$authentication_params->{signature} =
		$self->generate_signature($http_method, $path,
		$authentication_params, $body);
	return $self->build_path($path, $authentication_params);

}

sub get_api_key {
	my $self = shift;
	return $self->{api_key};
}

sub set_api_key {
{'fraction_non_alphanumeric': 0.1800075633429976, 'fraction_numerical': 0.010588680196646917, 'mean_word_length': 3.940224159402242, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 93, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2098443', 'n_tokens_mistral': 3083, 'n_tokens_neox': 2941, 'n_words': 689}