« blib/lib/Webservice/OVH/Hosting/Web/Service.pm
↑ Summary
lib/Webservice/OVH/Cloud/Project/Network.pm »
| 1 | package Webservice::OVH::Me; | ||
| 2 | |||
| 3 | =encoding utf-8 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Webservice::OVH::Me | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Webservice::OVH; | ||
| 12 | |||
| 13 | my $ovh = Webservice::OVH->new_from_json("credentials.json"); | ||
| 14 | |||
| 15 | my $contacts = $ovh->me->contacts; | ||
| 16 | my $tasks_contact_change = $ovh->me->tasks_contact_change; | ||
| 17 | my $orders = $ovh->me->orders(DateTime->now->sub(days => -1), DateTime->now); | ||
| 18 | my $bills = $ovh->me->bills(DateTime->now->sub(days => -1), DateTime->now); | ||
| 19 | |||
| 20 | my $bill_id = $bills->[0]->id; | ||
| 21 | my $order_id = $orders->[0]->id; | ||
| 22 | |||
| 23 | my $bill = $me->ovh->bill($bill_id); | ||
| 24 | my $order = $me->ovh->bill($order_id); | ||
| 25 | |||
| 26 | print $bill->pdf_url; | ||
| 27 | print $order->url; | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | Module support for now only basic retrieval methods for contacs, tasks, orders and bills | ||
| 32 | |||
| 33 | =head1 METHODS | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | use strict; | ||
| 38 | use warnings; | ||
| 39 | use Carp qw{ carp croak }; | ||
| 40 | use Webservice::OVH::Helper; | ||
| 41 | |||
| 42 | our $VERSION = 0.47; | ||
| 43 | |||
| 44 | # sub modules | ||
| 45 | use Webservice::OVH::Me::Contact; | ||
| 46 | use Webservice::OVH::Me::Order; | ||
| 47 | use Webservice::OVH::Me::Bill; | ||
| 48 | use Webservice::OVH::Me::Task; | ||
| 49 | |||
| 50 | =head2 _new | ||
| 51 | |||
| 52 | Internal Method to create the me object. | ||
| 53 | This method is not ment to be called external. | ||
| 54 | |||
| 55 | =over | ||
| 56 | |||
| 57 | =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object | ||
| 58 | |||
| 59 | =item * Return: L<Webservice::OVH::Me> | ||
| 60 | |||
| 61 | =item * Synopsis: Webservice::OVH::Me->_new($ovh_api_wrapper, $self); | ||
| 62 | |||
| 63 | =back | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | sub _new { | ||
| 68 | |||
| 69 | my ( $class, %params ) = @_; | ||
| 70 | |||
| 71 | die "Missing module" unless $params{module}; | ||
| 72 | die "Missing wrapper" unless $params{wrapper}; | ||
| 73 | |||
| 74 | my $module = $params{module}; | ||
| 75 | my $api_wrapper = $params{wrapper}; | ||
| 76 | |||
| 77 | my $self = bless { _module => $module, _api_wrapper => $api_wrapper, _contacts => {}, _tasks_contact_change => {}, _orders => {}, _bills => {} }, $class; | ||
| 78 | |||
| 79 | return $self; | ||
| 80 | } | ||
| 81 | |||
| 82 | =head2 contacts | ||
| 83 | |||
| 84 | Produces an array of all available contacts that are stored for the used account. | ||
| 85 | |||
| 86 | =over | ||
| 87 | |||
| 88 | =item * Return: ARRAY | ||
| 89 | |||
| 90 | =item * Synopsis: my $contacts = $ovh->me->contacs(); | ||
| 91 | |||
| 92 | =back | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub contacts { | ||
| 97 | |||
| 98 | my ($self) = @_; | ||
| 99 | |||
| 100 | my $api = $self->{_api_wrapper}; | ||
| 101 | my $response = $api->rawCall( method => 'get', path => "/me/contact", noSignature => 0 ); | ||
| 102 | croak $response->error if $response->error; | ||
| 103 | |||
| 104 | my $contact_ids = $response->content; | ||
| 105 | my $contacts = []; | ||
| 106 | |||
| 107 | foreach my $contact_id (@$contact_ids) { | ||
| 108 | |||
| 109 | my $contact = $self->{_contacts}{$contact_id} = $self->{_contacts}{$contact_id} || Webservice::OVH::Me::Contact->_new_existing( wrapper => $api, id => $contact_id, module => $self->{_module} ); | ||
| 110 | push @$contacts, $contact; | ||
| 111 | } | ||
| 112 | |||
| 113 | return $contacts; | ||
| 114 | } | ||
| 115 | |||
| 116 | =head2 contact | ||
| 117 | |||
| 118 | Returns a single contact by id | ||
| 119 | |||
| 120 | =over | ||
| 121 | |||
| 122 | =item * Parameter: $contact_id - id | ||
| 123 | |||
| 124 | =item * Return: L<Webservice::OVH::Me::Contact> | ||
| 125 | |||
| 126 | =item * Synopsis: my $contact = $ovh->me->contact(1234567); | ||
| 127 | |||
| 128 | =back | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub contact { | ||
| 133 | |||
| 134 | my ( $self, $contact_id ) = @_; | ||
| 135 | |||
| 136 | my $api = $self->{_api_wrapper}; | ||
| 137 | my $contact = $self->{_contacts}{$contact_id} = $self->{_contacts}{$contact_id} || Webservice::OVH::Me::Contact->_new_existing( wrapper => $api, id => $contact_id, module => $self->{_module} ); | ||
| 138 | |||
| 139 | return $contact; | ||
| 140 | } | ||
| 141 | |||
| 142 | =head2 tasks_contact_change | ||
| 143 | |||
| 144 | Produces an array of all available contact change tasks. | ||
| 145 | |||
| 146 | =over | ||
| 147 | |||
| 148 | =item * Return: ARRAY | ||
| 149 | |||
| 150 | =item * Synopsis: my $tasks = $ovh->me->tasks_contact_change(); | ||
| 151 | |||
| 152 | =back | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | sub tasks_contact_change { | ||
| 157 | |||
| 158 | my ($self) = @_; | ||
| 159 | |||
| 160 | my $api = $self->{_api_wrapper}; | ||
| 161 | my $response = $api->rawCall( method => 'get', path => "/me/task/contactChange", noSignature => 0 ); | ||
| 162 | croak $response->error if $response->error; | ||
| 163 | |||
| 164 | my $task_ids = $response->content; | ||
| 165 | my $tasks = []; | ||
| 166 | |||
| 167 | foreach my $task_id (@$task_ids) { | ||
| 168 | |||
| 169 | my $task = $self->{_tasks_contact_change}{$task_id} = $self->{_tasks_contact_change}{$task_id} || Webservice::OVH::Me::Task->_new( wrapper => $api, type => "contact_change", id => $task_id, module => $self->{_module} ); | ||
| 170 | push @$tasks, $task; | ||
| 171 | } | ||
| 172 | |||
| 173 | return $tasks; | ||
| 174 | } | ||
| 175 | |||
| 176 | =head2 task_contact_change | ||
| 177 | |||
| 178 | Returns a single contact change task by id | ||
| 179 | |||
| 180 | =over | ||
| 181 | |||
| 182 | =item * Parameter: $task_id - id | ||
| 183 | |||
| 184 | =item * Return: L<Webservice::OVH::Me::Task> | ||
| 185 | |||
| 186 | =item * Synopsis: my $contact = $ovh->me->task_contact_change(1234567); | ||
| 187 | |||
| 188 | =back | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | sub task_contact_change { | ||
| 193 | |||
| 194 | my ( $self, $task_id ) = @_; | ||
| 195 | |||
| 196 | my $api = $self->{_api_wrapper}; | ||
| 197 | my $task = $self->{_tasks_contact_change}{$task_id} = $self->{_tasks_contact_change}{$task_id} || Webservice::OVH::Me::Task->_new( wrapper => $api, type => "contact_change", id => $task_id, module => $self->{_module} ); | ||
| 198 | |||
| 199 | return $task; | ||
| 200 | |||
| 201 | } | ||
| 202 | |||
| 203 | =head2 orders | ||
| 204 | |||
| 205 | Produces an array of all available orders. | ||
| 206 | Orders can be optionally filtered by date. | ||
| 207 | |||
| 208 | =over | ||
| 209 | |||
| 210 | =item * Parameter: $date_from - optional filter DateTime, $date_to - optional filter DateTime | ||
| 211 | |||
| 212 | =item * Return: ARRAY | ||
| 213 | |||
| 214 | =item * Synopsis: my $orders = $ovh->me->orders(DateTime->new(), DateTime->new()); | ||
| 215 | |||
| 216 | =back | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | sub orders { | ||
| 221 | |||
| 222 | my ( $self, $date_from, $date_to ) = @_; | ||
| 223 | |||
| 224 | my $str_date_from = $date_from ? $date_from->strftime("%Y-%m-%d") : ""; | ||
| 225 | my $str_date_to = $date_to ? $date_to->strftime("%Y-%m-%d") : ""; | ||
| 226 | my $filter = Webservice::OVH::Helper->construct_filter( "date.from" => $str_date_from, "date.to" => $str_date_to ); | ||
| 227 | |||
| 228 | my $api = $self->{_api_wrapper}; | ||
| 229 | my $response = $api->rawCall( method => 'get', path => "/me/order$filter", noSignature => 0 ); | ||
| 230 | croak $response->error if $response->error; | ||
| 231 | |||
| 232 | my $order_ids = $response->content; | ||
| 233 | my $orders = []; | ||
| 234 | |||
| 235 | foreach my $order_id (@$order_ids) { | ||
| 236 | |||
| 237 | my $order = $self->{_orders}{$order_id} = $self->{_orders}{$order_id} || Webservice::OVH::Me::Order->_new( wrapper => $api, id => $order_id, module => $self->{_module} ); | ||
| 238 | push @$orders, $order; | ||
| 239 | } | ||
| 240 | |||
| 241 | return $orders; | ||
| 242 | } | ||
| 243 | |||
| 244 | =head2 order | ||
| 245 | |||
| 246 | Returns a single order by id | ||
| 247 | |||
| 248 | =over | ||
| 249 | |||
| 250 | =item * Parameter: $order_id - id | ||
| 251 | |||
| 252 | =item * Return: L<Webservice::OVH::Me::Order> | ||
| 253 | |||
| 254 | =item * Synopsis: my $order = $ovh->me->order(1234567); | ||
| 255 | |||
| 256 | =back | ||
| 257 | |||
| 258 | =cut | ||
| 259 | |||
| 260 | sub order { | ||
| 261 | |||
| 262 | my ( $self, $order_id ) = @_; | ||
| 263 | |||
| 264 | my $api = $self->{_api_wrapper}; | ||
| 265 | my $order = $self->{_orders}{$order_id} = $self->{_orders}{$order_id} || Webservice::OVH::Me::Order->_new( wrapper => $api, id => $order_id, module => $self->{_module} ); | ||
| 266 | |||
| 267 | return $order; | ||
| 268 | } | ||
| 269 | |||
| 270 | =head2 bill | ||
| 271 | |||
| 272 | Returns a single bill by id | ||
| 273 | |||
| 274 | =over | ||
| 275 | |||
| 276 | =item * Parameter: $bill_id - id | ||
| 277 | |||
| 278 | =item * Return: L<Webservice::OVH::Me::Bill> | ||
| 279 | |||
| 280 | =item * Synopsis: my $order = $ovh->me->bill(1234567); | ||
| 281 | |||
| 282 | =back | ||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | sub bill { | ||
| 287 | |||
| 288 | my ( $self, $bill_id ) = @_; | ||
| 289 | |||
| 290 | my $api = $self->{_api_wrapper}; | ||
| 291 | my $bill = $self->{_bills}{$bill_id} = $self->{_bills}{$bill_id} || Webservice::OVH::Me::Bill->_new( wrapper => $api, id => $bill_id, module => $self->{_module} ); | ||
| 292 | |||
| 293 | return $bill; | ||
| 294 | } | ||
| 295 | |||
| 296 | =head2 bills | ||
| 297 | |||
| 298 | Produces an array of all available bills. | ||
| 299 | Bills can be optionally filtered by date. | ||
| 300 | |||
| 301 | =over | ||
| 302 | |||
| 303 | =item * Parameter: $date_from - optional filter DateTime, $date_to - optional filter DateTime | ||
| 304 | |||
| 305 | =item * Return: ARRAY | ||
| 306 | |||
| 307 | =item * Synopsis: my $bills = $ovh->me->bills(DateTime->new(), DateTime->new()); | ||
| 308 | |||
| 309 | =back | ||
| 310 | |||
| 311 | =cut | ||
| 312 | |||
| 313 | sub bills { | ||
| 314 | |||
| 315 | my ( $self, $date_from, $date_to ) = @_; | ||
| 316 | |||
| 317 | my $str_date_from = $date_from ? $date_from->strftime("%Y-%m-%d") : ""; | ||
| 318 | my $str_date_to = $date_to ? $date_to->strftime("%Y-%m-%d") : ""; | ||
| 319 | my $filter = Webservice::OVH::Helper->construct_filter( "date.from" => $str_date_from, "date.to" => $str_date_to ); | ||
| 320 | |||
| 321 | my $api = $self->{_api_wrapper}; | ||
| 322 | my $response = $api->rawCall( method => 'get', path => sprintf( "/me/bill%s", $filter ), noSignature => 0 ); | ||
| 323 | croak $response->error if $response->error; | ||
| 324 | |||
| 325 | my $bill_ids = $response->content; | ||
| 326 | my $bills = []; | ||
| 327 | |||
| 328 | foreach my $bill_id (@$bill_ids) { | ||
| 329 | |||
| 330 | my $bill = $self->{_bills}{$bill_id} = $self->{_bills}{$bill_id} || Webservice::OVH::Me::Bill->_new( wrapper => $api, id => $bill_id, module => $self->{_module} ); | ||
| 331 | push @$bills, $bill; | ||
| 332 | } | ||
| 333 | |||
| 334 | return $bills; | ||
| 335 | } | ||
| 336 | |||
| 337 | 1; |