| 1 | package Net::Google::CalendarV3; | ||
| 2 | $Net::Google::CalendarV3::VERSION = '0.16'; | ||
| 3 | =head NAME | ||
| 4 | |||
| 5 | Net::Google::CalendarV3 - Access Google Calendars using the v3 API | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use Moose; | ||
| 10 | use Kavorka; | ||
| 11 | use Try::Tiny; | ||
| 12 | use Types::Standard qw( ArrayRef ); | ||
| 13 | use Net::Google::CalendarV3::Types qw( CBool CalendarListEntry CalendarId Event to_Event ); | ||
| 14 | use Net::Google::CalendarV3::Calendar; | ||
| 15 | use Net::Google::CalendarV3::Event; | ||
| 16 | use WWW::JSON; | ||
| 17 | use JSON::XS; | ||
| 18 | |||
| 19 | has authentication => is => 'ro', lazy => 1, predicate => 'has_auth', builder => '_build_authentication'; | ||
| 20 | has oauth2_access_token => is => 'ro', predicate => 'has_token'; | ||
| 21 | |||
| 22 | has calendars => is => 'rw', default => sub { [] }, isa => ArrayRef[CalendarListEntry], coerce => 1; | ||
| 23 | has _service => is => 'ro', lazy => 1, builder => '_build_service'; | ||
| 24 | has _current_calendar => is => 'rw', isa => CalendarId, coerce => 1; | ||
| 25 | |||
| 26 | method _build_service { | ||
| 27 | WWW::JSON->new( base_url => 'https://www.googleapis.com/calendar/v3', | ||
| 28 | post_body_format => 'JSON', | ||
| 29 | ( $self->has_auth || $self->has_token ? (authentication => $self->authentication) : () ), | ||
| 30 | json => JSON::XS->new->utf8->allow_nonref->allow_blessed->convert_blessed, | ||
| 31 | ); | ||
| 32 | } | ||
| 33 | |||
| 34 | method _build_authentication { | ||
| 35 | die "Need a valid OAuth2 access token" | ||
| 36 | unless $self->has_token; | ||
| 37 | my $token = $self->oauth2_access_token; | ||
| 38 | return sub { $_[1]->header(Authorization => "Bearer $token") }; | ||
| 39 | } | ||
| 40 | |||
| 41 | method get_calendars (CBool $owned?) { | ||
| 42 | my $res = $self->_service->get('/users/me/calendarList', { minAccessRole => ($owned ? "owner" : "writer") } ); | ||
| 43 | die $res->error unless $res->success; | ||
| 44 | $self->calendars($res->res->{items}); | ||
| 45 | } | ||
| 46 | |||
| 47 | method set_calendar ($cal) { | ||
| 48 | $self->_current_calendar($cal); | ||
| 49 | } | ||
| 50 | |||
| 51 | method get_events (%filters) { | ||
| 52 | my $res = $self->_service->get('/calendars/[% calendarId %]/events', { -calendarId => $self->_current_calendar, %filters }); | ||
| 53 | die $res->error unless $res->success; | ||
| 54 | my @items = @{ $res->res->{items} }; | ||
| 55 | while (my $pt = $res->res->{nextPageToken}) { | ||
| 56 | $filters{pageToken} = $pt; | ||
| 57 | $res = $self->_service->get('/calendars/[% calendarId %]/events', { -calendarId => $self->_current_calendar, %filters }); | ||
| 58 | push @items, @{ $res->res->{items} }; | ||
| 59 | } | ||
| 60 | map { to_Event($_) } @items; | ||
| 61 | } | ||
| 62 | |||
| 63 | method get_entry ($entry_id) { | ||
| 64 | my $res = $self->_service->get('/calendars/[% calendarId %]/events/[% eventId %]', { -calendarId => $self->_current_calendar, -eventId => $entry_id }); | ||
| 65 | die $res->error unless $res->success; | ||
| 66 | to_Event( $res->res ); | ||
| 67 | } | ||
| 68 | |||
| 69 | method add_entry ($entry) { | ||
| 70 | $entry->{-calendarId} = $self->_current_calendar; | ||
| 71 | my $res = $self->_service->post('/calendars/[% calendarId %]/events', $entry); | ||
| 72 | die $res->error unless $res->success; | ||
| 73 | $entry = to_Event($res->res); | ||
| 74 | } | ||
| 75 | |||
| 76 | method delete_entry ($entry) { | ||
| 77 | my $res = $self->_service->delete('/calendars/[% calendarId %]/events/[% eventId %]', { -calendarId => $self->_current_calendar, -eventId => $entry->id }); | ||
| 78 | die $res->error unless $res->success or $res->code eq '404' or $res->code eq '410'; | ||
| 79 | 1; | ||
| 80 | } | ||
| 81 | |||
| 82 | method update_entry ($entry) { | ||
| 83 | $entry->{-calendarId} = $self->_current_calendar; | ||
| 84 | $entry->{-eventId} = $entry->id; | ||
| 85 | my $res = $self->_service->put('/calendars/[% calendarId %]/events/[% eventId %]', $entry); | ||
| 86 | die $res->error unless $res->success; | ||
| 87 | $entry = to_Event($res->res); | ||
| 88 | } | ||
| 89 | |||
| 90 | method move_entry ($entry_id, $new_calendar_id) { | ||
| 91 | my $res = $self->_service->post('/calendars/[% calendarId %]/events/[% eventId %]/move?destination=[% destination %]', { -calendarId => $self->_current_calendar, -eventId => $entry_id, -destination => $new_calendar_id }); | ||
| 92 | die $res->error unless $res->success; | ||
| 93 | my $entry = to_Event($res->res); | ||
| 94 | } | ||
| 95 | 1; | ||
| 96 |