Skip to content

Commit

Permalink
added-status-check-myaffiliates (#17)
Browse files Browse the repository at this point in the history
* added-status-check

* removed-tidy-files

* trigger tests 2024-10-09 13:41:08

* added-test-case

* trigger tests 2024-10-13 14:04:15

* trigger tests 2024-10-16 05:51:30

* status-update

* trigger tests 2024-10-17 06:24:01

* trigger tests 2024-10-28 17:19:40

* trigger tests 2024-10-28 17:48:17

* trigger tests 2024-10-28 19:46:48

* trigger tests 2024-10-29 05:45:36
  • Loading branch information
Venugopal-Deriv authored Nov 5, 2024
1 parent 7d078cb commit f1e91a2
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 83 deletions.
93 changes: 58 additions & 35 deletions lib/WebService/MyAffiliates.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sub errstr { return $errstr }

sub new { ## no critic (ArgUnpacking)
my $class = shift;
my %args = @_ % 2 ? %{$_[0]} : @_;
my %args = @_ % 2 ? %{ $_[0] } : @_;

for (qw/user pass host/) {
$args{$_} || croak "Param $_ is required.";
Expand All @@ -36,7 +36,7 @@ sub __http_tiny {

return $self->{http_tiny} if exists $self->{http_tiny};

my $http_tiny = HTTP::Tiny->new(timeout => $self->{timeout});
my $http_tiny = HTTP::Tiny->new( timeout => $self->{timeout} );
$self->{http_tiny} = $http_tiny;

return $http_tiny;
Expand All @@ -45,25 +45,36 @@ sub __http_tiny {
## https://myaffiliates.atlassian.net/wiki/display/PUB/Feed+1%3A+Users+Feed
sub get_users { ## no critic (ArgUnpacking)
my $self = shift;
my %args = @_ % 2 ? %{$_[0]} : @_;
my %args = @_ % 2 ? %{ $_[0] } : @_;
my $url = Mojo::URL->new('/feeds.php?FEED_ID=1');
$url->query(\%args) if %args;
return $self->request($url->to_string);
$url->query( \%args ) if %args;
return $self->request( $url->to_string );
}

sub get_users_status { ## no critic (ArgUnpacking)
my $self = shift;
my %args = @_ % 2 ? %{ $_[0] } : @_;
my $url = Mojo::URL->new('/feeds.php?FEED_ID=3');
$url->query( \%args ) if %args;
return $self->request( $url->to_string );
}

sub create_affiliate { ## no critic (ArgUnpacking)
my $self = shift;
my %args = @_ % 2 ? %{$_[0]} : @_;
my $parameters = +{map { ('PARAM_' . $_ => $args{$_}) } keys %args};
my %args = @_ % 2 ? %{ $_[0] } : @_;
my $parameters = +{ map { ( 'PARAM_' . $_ => $args{$_} ) } keys %args };

my $url = Mojo::URL->new('/feeds.php?FEED_ID=26');
$url->query($parameters);
my $res = $self->request($url->to_string);
my $res = $self->request( $url->to_string );

my $error_count = $res->{INIT}->{ERROR_COUNT};
if ($error_count) {
my $init = $res->{INIT};
my @errors = ref $init->{ERROR} eq 'ARRAY' ? $init->{ERROR}->@* : ($init->{ERROR});
my $init = $res->{INIT};
my @errors =
ref $init->{ERROR} eq 'ARRAY'
? $init->{ERROR}->@*
: ( $init->{ERROR} );
$errstr = map { $_->{MSG} . " " . $_->{DETAIL} } @errors;
return;
}
Expand All @@ -74,10 +85,17 @@ sub create_affiliate { ## no critic (ArgUnpacking)
}

sub get_user {
my ($self, $id) = @_;
my ( $self, $id ) = @_;

$id or croak "id is required.";
my $user = $self->get_users( USER_ID => $id ) or return;
return $user->{USER};
}

$id or croak "id is required.";
my $user = $self->get_users(USER_ID => $id) or return;
sub get_users_status_by_affiliate_id {
my ( $self, $ids ) = @_;
$ids or croak "id is required.";
my $user = $self->get_users_status( USER_IDS => $ids );
return $user->{USER};
}

Expand All @@ -86,68 +104,73 @@ sub decode_token {
my $self = shift;
my @tokens = @_ or croak 'Must pass at least one token.';

return $self->request('/feeds.php?FEED_ID=4&TOKENS=' . url_escape(join(',', @tokens)));
return $self->request(
'/feeds.php?FEED_ID=4&TOKENS=' . url_escape( join( ',', @tokens ) ) );
}

## https://myaffiliates.atlassian.net/wiki/display/PUB/Feed+5%3A+Encode+Token
sub encode_token { ## no critic (ArgUnpacking)
my $self = shift;
my %args = @_ % 2 ? %{$_[0]} : @_;
my %args = @_ % 2 ? %{ $_[0] } : @_;

$args{USER_ID} or croak "USER_ID is required.";
$args{SETUP_ID} or croak "SETUP_ID is required.";

my $url = Mojo::URL->new('/feeds.php?FEED_ID=5');
$url->query(\%args) if %args;
return $self->request($url->to_string);
$url->query( \%args ) if %args;
return $self->request( $url->to_string );
}

## https://myaffiliates.atlassian.net/wiki/display/PUB/Feed+6%3A+User+Transactions+Feed
sub get_user_transactions { ## no critic (ArgUnpacking)
my $self = shift;
my %args = @_ % 2 ? %{$_[0]} : @_;
my %args = @_ % 2 ? %{ $_[0] } : @_;

$args{FROM_DATE} or croak 'FROM_DATE is reqired.';

my $url = Mojo::URL->new('/feeds.php?FEED_ID=6');
$url->query(\%args) if %args;
return $self->request($url->to_string);
$url->query( \%args ) if %args;
return $self->request( $url->to_string );
}

sub get_customers { ## no critic (ArgUnpacking)
my $self = shift;
my %args = @_ % 2 ? %{$_[0]} : @_;
my %args = @_ % 2 ? %{ $_[0] } : @_;

my $url = Mojo::URL->new('/feeds.php?FEED_ID=10');
$url->query(\%args) if %args;
$url->query( \%args ) if %args;

my $res = $self->request($url->to_string);
my $res = $self->request( $url->to_string );

my $customers =
!exists $res->{PLAYER} ? []
: ref $res->{PLAYER} eq 'ARRAY' ? $res->{PLAYER}
: [$res->{PLAYER}];
!exists $res->{PLAYER} ? []
: ref $res->{PLAYER} eq 'ARRAY' ? $res->{PLAYER}
: [ $res->{PLAYER} ];

return $customers;
}

sub request {
my ($self, $url, $method, %params) = @_;
my ( $self, $url, $method, %params ) = @_;

$method ||= 'GET';

my $http_tiny = $self->__http_tiny;
my $header = {Authorization => 'Basic ' . b64_encode($self->{user} . ':' . $self->{pass}, '')};
my @extra = %params ? (form => \%params) : ();
my $response = $http_tiny->get($self->{host} . $url, {headers => $header});
my $header = { Authorization => 'Basic '
. b64_encode( $self->{user} . ':' . $self->{pass}, '' ) };
my @extra = %params ? ( form => \%params ) : ();
my $response =
$http_tiny->get( $self->{host} . $url, { headers => $header } );

unless ($response->{success}) {
unless ( $response->{success} ) {
$errstr = $response->{reason};
return;
}

if ($response->{headers}{"content-type"} and $response->{headers}{"content-type"} =~ 'text/xml') {
return XMLin($response->{content});
if ( $response->{headers}{"content-type"}
and $response->{headers}{"content-type"} =~ 'text/xml' )
{
return XMLin( $response->{content} );
}

$errstr = $response->{content};
Expand All @@ -156,7 +179,7 @@ sub request {

## un-documented helper
sub get_affiliate_id_from_token {
my ($self, $token) = @_;
my ( $self, $token ) = @_;

$token or croak 'Must pass a token to get_affiliate_id_from_token.';

Expand All @@ -179,7 +202,7 @@ Returns token_info hash
=cut

sub get_affiliate_details {
my ($self, $token) = @_;
my ( $self, $token ) = @_;

croak 'Must pass a token to get_affiliate_email_from_token.' unless $token;

Expand Down
5 changes: 3 additions & 2 deletions t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ my $aff = WebService::MyAffiliates->new(
ok($aff);

throws_ok { $aff->decode_token() } qr/Must pass at least one token/;
throws_ok { $aff->get_affiliate_id_from_token() } qr/Must pass a token to get_affiliate_id_from_token/,
'Throws exception if no token given.';
throws_ok { $aff->get_affiliate_id_from_token() }
qr/Must pass a token to get_affiliate_id_from_token/,
'Throws exception if no token given.';

done_testing;
62 changes: 38 additions & 24 deletions t/create_affiliate.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ $mock_aff->mock(
sub {
return +{
'INIT' => {
'ERROR_COUNT' => 0,
'ERROR_COUNT' => 0,
'WARNING_COUNT' => 0,
}};
}
};

});
}
);

ok($aff->create_affiliate({}), 'A hashref is a valid parameter');
ok( $aff->create_affiliate( {} ), 'A hashref is a valid parameter' );

my $hash_params = (
first_name => 'Dummy'
);
my $hash_params = ( first_name => 'Dummy' );

ok($aff->create_affiliate({}), 'A hash is a valid parameter');
ok( $aff->create_affiliate( {} ), 'A hash is a valid parameter' );

$mock_aff->mock(
'request',
Expand All @@ -43,9 +43,12 @@ $mock_aff->mock(
'ERROR' => {
'MSG' => 'An account with this email already exists.',
'DETAIL' => 'email'
}}};
}
}
};

});
}
);

my $args = {
'first_name' => 'Charles',
Expand All @@ -68,28 +71,33 @@ my $args = {

my $res = $aff->create_affiliate($args);

ok(!$res, 'Returns undef in case of a single error');
ok( !$res, 'Returns undef in case of a single error' );

$mock_aff->mock(
'request',
sub {
return +{
'INIT' => {
'ERROR_COUNT' => 2,
'ERROR' => [{
'MSG' => 'An account with this email already exists.',
'ERROR' => [
{
'MSG' => 'An account with this email already exists.',
'DETAIL' => 'email'
},
{
'DETAIL' => 'username',
'MSG' => 'Username not available'
}]}};
}
]
}
};

});
}
);

$res = $aff->create_affiliate($args);

ok(!$res, 'Returns undef in case of multiple errors');
ok( !$res, 'Returns undef in case of multiple errors' );

$mock_aff->mock(
'request',
Expand All @@ -100,13 +108,16 @@ $mock_aff->mock(
'ERROR' => {
'MSG' => 'An account with this email already exists.',
'DETAIL' => 'email'
}}};
}
}
};

});
}
);

$res = $aff->create_affiliate($args);

ok(!$res, 'Returns undef in case of single error');
ok( !$res, 'Returns undef in case of single error' );

$mock_aff->mock(
'request',
Expand All @@ -117,7 +128,8 @@ $mock_aff->mock(
'WARNING_COUNT' => 1,
'WARNING_DETAIL' => {
'DETAIL' => 'password',
'MSG' => 'Setting a password for a new affiliate is optional and will be deprecated in future'
'MSG' =>
'Setting a password for a new affiliate is optional and will be deprecated in future'
},
'USERNAME' => 'charles_babbage',
'PASSWORD' => 's3cr3t',
Expand All @@ -126,13 +138,15 @@ $mock_aff->mock(
'COUNTRY' => 'GB',
'LANGUAGE' => 0,
'EMAIL' => '[email protected]'
}};
});
}
};
}
);

$res = $aff->create_affiliate($args);

ok($res, 'Returns the ref correctly');
ok(!$res->{PASSWORD}, 'It does not return the password');
ok( $res, 'Returns the ref correctly' );
ok( !$res->{PASSWORD}, 'It does not return the password' );

done_testing();

Expand Down
Loading

0 comments on commit f1e91a2

Please sign in to comment.