| 1 | package Yahoo::Marketing::APT::Service; | ||
| 2 | # Copyright (c) 2010 Yahoo! Inc. All rights reserved. | ||
| 3 | # The copyrights to the contents of this file are licensed under the Perl Artistic License (ver. 15 Aug 1997) | ||
| 4 | |||
| 5 | 44 | ▶ | use strict; use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 44 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 44 | ▶ | use YAML qw/DumpFile LoadFile Dump/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 44 | ▶ | use base qw/Yahoo::Marketing::APT Yahoo::Marketing::Service/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | |||
| 12 | # need to override @simple_type_exceptions! | ||
| 13 | sub simple_type_exceptions { | ||
| 14 | return (qw/ | ||
| 15 | AccountDescriptorType | ||
| 16 | AccountStatus | ||
| 17 | AccountType | ||
| 18 | AdBehavior | ||
| 19 | AdFormat | ||
| 20 | AdjustmentInboundFeedStatus | ||
| 21 | AdjustmentInboundFeedType | ||
| 22 | AdLinkingType | ||
| 23 | AdSecurity | ||
| 24 | AdStatus | ||
| 25 | AdType | ||
| 26 | AgreementStatus | ||
| 27 | ApprovalCategory | ||
| 28 | ApprovalObjectType | ||
| 29 | ApprovalStatus | ||
| 30 | ApprovalTaskContext | ||
| 31 | ApprovalTaskStatus | ||
| 32 | ApprovalTrigger | ||
| 33 | ApprovalType | ||
| 34 | ApprovalWorkflowExecutionType | ||
| 35 | ApprovalWorkflowNotificationType | ||
| 36 | ApprovalWorkflowStatus | ||
| 37 | ApproverType | ||
| 38 | AudienceSegmentStatus | ||
| 39 | BuyType | ||
| 40 | ColumnFormatType | ||
| 41 | ComplaintStatus | ||
| 42 | Context | ||
| 43 | Country | ||
| 44 | CreativeStatus | ||
| 45 | CreativeType | ||
| 46 | Currency | ||
| 47 | CustomGeoAreaStatus | ||
| 48 | CustomTargetingAttributeOwnership | ||
| 49 | DataGrouping | ||
| 50 | DayOfTheWeek | ||
| 51 | DealApprovalStatus | ||
| 52 | DeliveryLevel | ||
| 53 | DeliveryMethod | ||
| 54 | DeliveryModel | ||
| 55 | DeliveryType | ||
| 56 | DiscountFormat | ||
| 57 | DiscountType | ||
| 58 | DistanceUnits | ||
| 59 | EditorialStatus | ||
| 60 | EndDateChangeType | ||
| 61 | FileType | ||
| 62 | FolderItemType | ||
| 63 | FolderType | ||
| 64 | ImpressionChangeType | ||
| 65 | Language | ||
| 66 | LibraryAdStatus | ||
| 67 | LinkedCompanyType | ||
| 68 | LinkType | ||
| 69 | Locale | ||
| 70 | Month | ||
| 71 | OperationResult | ||
| 72 | OptimizationMetric | ||
| 73 | OrderContactType | ||
| 74 | OrderCreditStatus | ||
| 75 | OrderFeeStatus | ||
| 76 | OrderFeeType | ||
| 77 | OrderStatus | ||
| 78 | Origin | ||
| 79 | PixelCodeType | ||
| 80 | PixelFrequencyType | ||
| 81 | PlacementEditQtyChangeType | ||
| 82 | PlacementEditQtyType | ||
| 83 | PlacementStatus | ||
| 84 | PlacementTargetType | ||
| 85 | PriceChangeType | ||
| 86 | PricingType | ||
| 87 | ProcessingStatus | ||
| 88 | PublisherSelectorType | ||
| 89 | RateCardStatus | ||
| 90 | ReconciliationAction | ||
| 91 | ReportCurrency | ||
| 92 | ReportDateRange | ||
| 93 | ReportExecutionStatus | ||
| 94 | ReportFrequency | ||
| 95 | RevenueModel | ||
| 96 | SearchAccountType | ||
| 97 | SellingRuleType | ||
| 98 | ServiceContext | ||
| 99 | SiteAccessMethod | ||
| 100 | SiteStatus | ||
| 101 | SourceOwnerType | ||
| 102 | TagStatus | ||
| 103 | TagType | ||
| 104 | TargetingAttributeType | ||
| 105 | TemplateStatus | ||
| 106 | TimePeriodType | ||
| 107 | TimeZone | ||
| 108 | UserStatus | ||
| 109 | VideoCreativeProcessingStatus | ||
| 110 | WindowTarget | ||
| 111 | YahooPremiumBehavioralSegmentTargetingProgram | ||
| 112 | /); | ||
| 113 | } | ||
| 114 | |||
| 115 | # override parse_config to use yahoo-marketing-apt.yml as the default config file | ||
| 116 | sub parse_config { | ||
| 117 | my ( $self, %args ) = @_; | ||
| 118 | |||
| 119 | $args{ path } = 'yahoo-marketing-apt.yml' unless defined $args{ path }; | ||
| 120 | $args{ section } = 'default' unless defined $args{ section }; | ||
| 121 | |||
| 122 | my $config = LoadFile( $args{ path } ); | ||
| 123 | |||
| 124 | foreach my $config_setting ( qw/ username password license endpoint uri version / ){ | ||
| 125 | my $value = $config->{ $args{ 'section' } }->{ $config_setting }; | ||
| 126 | croak "no configuration value found for $config_setting in $args{ path }\n" | ||
| 127 | unless $value; | ||
| 128 | $self->$config_setting( $value ); | ||
| 129 | } | ||
| 130 | |||
| 131 | foreach my $config_setting ( qw/ default_account default_on_behalf_of_username default_on_behalf_of_password / ){ | ||
| 132 | my $value = $config->{ $args{ 'section' } }->{ $config_setting }; | ||
| 133 | my $setting_name = $config_setting; | ||
| 134 | $setting_name =~ s/^default_//; | ||
| 135 | # Maybe we should let the default overwrite ??? | ||
| 136 | $self->$setting_name( $value ) if defined $value and not defined $self->$setting_name; | ||
| 137 | } | ||
| 138 | |||
| 139 | return $self; | ||
| 140 | } | ||
| 141 | |||
| 142 | # override _location, since we cache endpoint with accountID now, instead of masterAccountID. | ||
| 143 | sub _location { | ||
| 144 | my $self = shift; | ||
| 145 | |||
| 146 | unless( $self->use_location_service ){ | ||
| 147 | return $self->endpoint.'/'.$self->version; | ||
| 148 | } | ||
| 149 | |||
| 150 | my $locations = $self->cache->get( 'locations' ); | ||
| 151 | |||
| 152 | if( $locations | ||
| 153 | and $locations->{ $self->version }->{ $self->endpoint } | ||
| 154 | and $locations->{ $self->version }->{ $self->endpoint }->{ $self->account } ){ | ||
| 155 | return $locations->{ $self->version }->{ $self->endpoint }->{ $self->account }; | ||
| 156 | } | ||
| 157 | |||
| 158 | my $som = $self->_soap( $self->endpoint | ||
| 159 | .'/' | ||
| 160 | .$self->version | ||
| 161 | .'/LocationService' | ||
| 162 | ) | ||
| 163 | ->getAccountLocation( $self->_headers() ); | ||
| 164 | |||
| 165 | if( $som->fault ){ | ||
| 166 | $self->fault( $self->_get_api_fault_from_som( $som ) ); | ||
| 167 | $self->_die_with_soap_fault( $som ) unless $self->immortal; | ||
| 168 | warn "we could not determine the correct location endpoint, trying with default"; | ||
| 169 | return $self->endpoint.'/'.$self->version; | ||
| 170 | } | ||
| 171 | |||
| 172 | my $location = $som->valueof( '/Envelope/Body/getAccountLocationResponse/out' ); | ||
| 173 | |||
| 174 | die "failed to get Account Location!" unless $location; | ||
| 175 | |||
| 176 | $location .= '/'.$self->version; | ||
| 177 | |||
| 178 | $locations->{ $self->version }->{ $self->endpoint }->{ $self->account } = $location ; | ||
| 179 | |||
| 180 | $self->cache->set( 'locations', $locations, $self->cache_expire_time ); | ||
| 181 | |||
| 182 | return $location; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub _add_account_to_header { return 1; } # default to true | ||
| 186 | |||
| 187 | sub _add_master_account_to_header { return 0; } # default to false | ||
| 188 | |||
| 189 | sub _class_name { | ||
| 190 | __PACKAGE__ =~ /^(.+)Service/; | ||
| 191 | return $1; | ||
| 192 | } | ||
| 193 | |||
| 194 | |||
| 195 | # since APT web service v4.0, the wsdl has changed structure, | ||
| 196 | # so we have to override some methods from Service.pm in parent class. | ||
| 197 | sub _parse_wsdl { | ||
| 198 | my ( $self, ) = @_; | ||
| 199 | |||
| 200 | if( my $wsdl_data = $self->cache->get( $self->_wsdl ) ){ | ||
| 201 | $Yahoo::Marketing::Service::service_data->{ $self->_wsdl } = $wsdl_data; | ||
| 202 | return; | ||
| 203 | } | ||
| 204 | |||
| 205 | my $xpath = XML::XPath->new( | ||
| 206 | xml => SOAP::Schema->new(schema_url => $self->_wsdl )->access | ||
| 207 | ); | ||
| 208 | |||
| 209 | foreach my $node ( $xpath->find( q{/wsdl:definitions/wsdl:types/xsd:schema/* } )->get_nodelist ){ | ||
| 210 | my $name = $node->getName; | ||
| 211 | if( $name eq 'xsd:complexType' ){ | ||
| 212 | if( $node->getAttribute('name') and ($node->getAttribute('name') =~ /^[a-z].+Response(Type)?$/) ){ | ||
| 213 | $self->_parse_response_type( $node, $xpath ); | ||
| 214 | }elsif( $node->getAttribute('name') and ($node->getAttribute('name') =~ /^[a-z]/) ) { | ||
| 215 | $self->_parse_request_type( $node, $xpath ); | ||
| 216 | }else{ | ||
| 217 | $self->_parse_complex_type( $node, $xpath ); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | $self->cache->set( $self->_wsdl, $Yahoo::Marketing::Service::service_data->{ $self->_wsdl }, $self->cache_expire_time ); | ||
| 223 | return; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub _parse_request_type { | ||
| 227 | my ( $self, $node, $xpath ) = @_; | ||
| 228 | my $type_name = $node->getAttribute( 'name' ); | ||
| 229 | |||
| 230 | return unless $type_name; | ||
| 231 | |||
| 232 | my $def = $xpath->find( qq{/wsdl:definitions/wsdl:types/xsd:schema/xsd:complexType[\@name='$type_name']/xsd:sequence/xsd:element} ); | ||
| 233 | |||
| 234 | return unless $def; | ||
| 235 | |||
| 236 | foreach my $def_node ( $def->get_nodelist ){ | ||
| 237 | |||
| 238 | my $name = $def_node->getAttribute( 'name' ); | ||
| 239 | my $type = $def_node->getAttribute( 'type' ); | ||
| 240 | |||
| 241 | $Yahoo::Marketing::Service::service_data->{ $self->_wsdl }->{ type_map }->{ $type_name }->{ _name } = $name ; | ||
| 242 | $Yahoo::Marketing::Service::service_data->{ $self->_wsdl }->{ type_map }->{ $type_name }->{ $name } = $type ; | ||
| 243 | } | ||
| 244 | |||
| 245 | return; | ||
| 246 | } | ||
| 247 | |||
| 248 | sub _parse_response_type { | ||
| 249 | my ( $self, $node, $xpath ) = @_; | ||
| 250 | my $type_name = $node->getAttribute( 'name' ); | ||
| 251 | $type_name =~ s/(^tns:)|(^xsd:)//; | ||
| 252 | |||
| 253 | my $def = $xpath->find( qq{/wsdl:definitions/wsdl:types/xsd:schema/xsd:complexType[\@name='$type_name']/xsd:sequence/xsd:element[\@name='out']} ); | ||
| 254 | return unless $def; | ||
| 255 | |||
| 256 | my $def_node = ($def->get_nodelist)[0]; # there's always just one | ||
| 257 | |||
| 258 | ( my $name = $def_node->getAttribute( 'name' ) ) =~ s/^tns://; | ||
| 259 | ( my $type = $def_node->getAttribute( 'type' ) ) =~ s/^tns://; | ||
| 260 | |||
| 261 | $Yahoo::Marketing::Service::service_data->{ $self->_wsdl }->{ type_map }->{ $type_name }->{ _name } = $name ; | ||
| 262 | $Yahoo::Marketing::Service::service_data->{ $self->_wsdl }->{ type_map }->{ $type_name }->{ $name } = $type ; | ||
| 263 | |||
| 264 | return; | ||
| 265 | } | ||
| 266 | |||
| 267 | |||
| 268 | 1; # end of Yahoo::Marketing::APT::Service | ||
| 269 | |||
| 270 | |||
| 271 | =head1 NAME | ||
| 272 | |||
| 273 | Yahoo::Marketing::APT::Service - a sub-class of Yahoo::Marketing::Service, and the base class of APT service modules | ||
| 274 | |||
| 275 | =head1 SYNOPSIS | ||
| 276 | |||
| 277 | This module is a base class for various Service modules (SiteService, | ||
| 278 | FolderService, ReportService, etc) to inherit from. It should not be used directly. | ||
| 279 | |||
| 280 | Please see the Yahoo APT API docs at | ||
| 281 | |||
| 282 | http://help.yahoo.com/l/us/yahoo/ewsapt/webservices/reference/index.html | ||
| 283 | |||
| 284 | for details about what methods are available from each of the Services. | ||
| 285 | |||
| 286 | =head1 SEE ALSO | ||
| 287 | |||
| 288 | Yahoo::Marketing::Service L<http://search.cpan.org/dist/Yahoo-Marketing/lib/Yahoo/Marketing/Service.pm> | ||
| 289 | |||