| 1 | package Finance::GDAX::API::Report; | ||
| 2 | our $VERSION = '0.02'; | ||
| 3 | use 5.20.0; | ||
| 4 | use warnings; | ||
| 5 | use Moose; | ||
| 6 | use Finance::GDAX::API::TypeConstraints; | ||
| 7 | use Finance::GDAX::API; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | extends 'Finance::GDAX::API'; | ||
| 11 | |||
| 12 | has 'type' => (is => 'rw', | ||
| 13 | isa => 'ReportType', | ||
| 14 | ); | ||
| 15 | has 'start_date' => (is => 'rw', | ||
| 16 | isa => 'Str', | ||
| 17 | ); | ||
| 18 | has 'end_date' => (is => 'rw', | ||
| 19 | isa => 'Str', | ||
| 20 | ); | ||
| 21 | has 'product_id' => (is => 'rw', | ||
| 22 | isa => 'Str', | ||
| 23 | ); | ||
| 24 | has 'account_id' => (is => 'rw', | ||
| 25 | isa => 'Str', | ||
| 26 | ); | ||
| 27 | has 'format' => (is => 'rw', | ||
| 28 | isa => 'ReportFormat', | ||
| 29 | default => 'pdf', | ||
| 30 | ); | ||
| 31 | has 'email' => (is => 'rw', | ||
| 32 | isa => 'Str', | ||
| 33 | ); | ||
| 34 | |||
| 35 | # For checking with "get" method | ||
| 36 | has 'report_id' => (is => 'rw', | ||
| 37 | isa => 'Str', | ||
| 38 | ); | ||
| 39 | |||
| 40 | sub get { | ||
| 41 | my ($self, $report_id) = @_; | ||
| 42 | $report_id = $report_id || $self->report_id; | ||
| 43 | die 'no report_id specified to get' unless $report_id; | ||
| 44 | $self->report_id($report_id); | ||
| 45 | my $path = "/reports/$report_id"; | ||
| 46 | $self->path($path); | ||
| 47 | $self->method('GET'); | ||
| 48 | return $self->send; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub create { | ||
| 52 | my $self = shift; | ||
| 53 | die 'report type is required' unless $self->type; | ||
| 54 | die 'report start date is required' unless $self->start_date; | ||
| 55 | die 'report end date is required' unless $self->end_date; | ||
| 56 | my %body = ( type => $self->type, | ||
| 57 | start_date => $self->start_date, | ||
| 58 | end_date => $self->end_date, | ||
| 59 | format => $self->format ); | ||
| 60 | if ($self->type eq 'fills') { | ||
| 61 | die 'product_id is required for fills report' unless $self->product_id; | ||
| 62 | $body{product_id} = $self->product_id; | ||
| 63 | } | ||
| 64 | if ($self->type eq 'account') { | ||
| 65 | die 'account_id is required for account report' unless $self->account_id; | ||
| 66 | $body{account_id} = $self->account_id; | ||
| 67 | } | ||
| 68 | $body{email} = $self->email if $self->email; | ||
| 69 | $self->method('POST'); | ||
| 70 | $self->body(\%body); | ||
| 71 | $self->path('/reports'); | ||
| 72 | return $self->send; | ||
| 73 | } | ||
| 74 | |||
| 75 | __PACKAGE__->meta->make_immutable; | ||
| 76 | 1; | ||
| 77 | |||
| 78 | =head1 NAME | ||
| 79 | |||
| 80 | Finance::GDAX::API::Report - Generate GDAX Reports | ||
| 81 | |||
| 82 | =head1 SYNOPSIS | ||
| 83 | |||
| 84 | use Finance::GDAX::API::Report; | ||
| 85 | |||
| 86 | $report = Finance::GDAX::API::Report->new( | ||
| 87 | start_date => '2017-06-01T00:00:00.000Z', | ||
| 88 | end_date => '2017-06-15T00:00:00.000Z', | ||
| 89 | type => 'fills'); | ||
| 90 | |||
| 91 | $report->product_id('BTC-USD'); | ||
| 92 | $result = $report->create; | ||
| 93 | |||
| 94 | $report_id = $$result{id}; | ||
| 95 | |||
| 96 | # After you create the report, you check if it's generated yet | ||
| 97 | |||
| 98 | $report = Finance::GDAX::API::Report->new; | ||
| 99 | $result = $report->get($report_id); | ||
| 100 | |||
| 101 | if ($$result{status} eq 'ready') { | ||
| 102 | `wget $$result{file_url}`; | ||
| 103 | } | ||
| 104 | |||
| 105 | =head2 DESCRIPTION | ||
| 106 | |||
| 107 | Generating reports at GDAX is a 2-step process. First you must tell | ||
| 108 | GDAX to create the report, then you must check to see if the report is | ||
| 109 | ready for download at a URL. You can also specify and email address to | ||
| 110 | have it mailed. | ||
| 111 | |||
| 112 | Reports can be "fills" or "account". If fills, then a product_id is | ||
| 113 | needed. If account then an account_id is needed. | ||
| 114 | |||
| 115 | The format can be "pdf" or "csv" and defaults to "pdf". | ||
| 116 | |||
| 117 | =head1 ATTRIBUTES | ||
| 118 | |||
| 119 | =head2 C<type> $string | ||
| 120 | |||
| 121 | Report type, either "fills" or "account". This must be set before | ||
| 122 | calling the "create" method. | ||
| 123 | |||
| 124 | =head2 C<start_date> $datetime_string | ||
| 125 | |||
| 126 | Start of datetime range of report in the format | ||
| 127 | "2014-11-01T00:00:00.000Z" (required for create) | ||
| 128 | |||
| 129 | =head2 C<end_date> $datetime_string | ||
| 130 | |||
| 131 | End of datetime range of report in the format | ||
| 132 | "2014-11-01T00:00:00.000Z" (required for create) | ||
| 133 | |||
| 134 | =head2 C<product_id> $string | ||
| 135 | |||
| 136 | The product ID, eg 'BTC-USD'. Required for fills type. | ||
| 137 | |||
| 138 | =head2 C<account_id> $string | ||
| 139 | |||
| 140 | The account ID. Required for account type. | ||
| 141 | |||
| 142 | =head2 C<format> $string | ||
| 143 | |||
| 144 | Output format of report, either "pdf" or "csv" (default "pdf") | ||
| 145 | |||
| 146 | =head2 C<email> $string | ||
| 147 | |||
| 148 | Email address to send the report to (optional) | ||
| 149 | |||
| 150 | =head2 C<report_id> $string | ||
| 151 | |||
| 152 | This is used for the "get" method only, and can also be passed as a | ||
| 153 | parameter to the "get" method. | ||
| 154 | |||
| 155 | It is the report id as returned by the "create" method. | ||
| 156 | |||
| 157 | =head1 METHODS | ||
| 158 | |||
| 159 | =head2 C<create> | ||
| 160 | |||
| 161 | Creates the GDAX report based upon the attributes set and returns a | ||
| 162 | hash result as documented in the API: | ||
| 163 | |||
| 164 | { | ||
| 165 | "id": "0428b97b-bec1-429e-a94c-59232926778d", | ||
| 166 | "type": "fills", | ||
| 167 | "status": "pending", | ||
| 168 | "created_at": "2015-01-06T10:34:47.000Z", | ||
| 169 | "completed_at": undefined, | ||
| 170 | "expires_at": "2015-01-13T10:35:47.000Z", | ||
| 171 | "file_url": undefined, | ||
| 172 | "params": { | ||
| 173 | "start_date": "2014-11-01T00:00:00.000Z", | ||
| 174 | "end_date": "2014-11-30T23:59:59.000Z" | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | =head2 C<get> [$report_id] | ||
| 179 | |||
| 180 | Returns a hash representing the status of the report created with the | ||
| 181 | "create" method. | ||
| 182 | |||
| 183 | The parameter $report_id is optional - if it is passed to the method, | ||
| 184 | it overrides the object's report_id attribute. | ||
| 185 | |||
| 186 | The result when first creating the report might look like this: | ||
| 187 | |||
| 188 | { | ||
| 189 | "id": "0428b97b-bec1-429e-a94c-59232926778d", | ||
| 190 | "type": "fills", | ||
| 191 | "status": "creating", | ||
| 192 | "created_at": "2015-01-06T10:34:47.000Z", | ||
| 193 | "completed_at": undefined, | ||
| 194 | "expires_at": "2015-01-13T10:35:47.000Z", | ||
| 195 | "file_url": undefined, | ||
| 196 | "params": { | ||
| 197 | "start_date": "2014-11-01T00:00:00.000Z", | ||
| 198 | "end_date": "2014-11-30T23:59:59.000Z" | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | While the result when GDAX finishes generating the report might look | ||
| 203 | like this: | ||
| 204 | |||
| 205 | { | ||
| 206 | "id": "0428b97b-bec1-429e-a94c-59232926778d", | ||
| 207 | "type": "fills", | ||
| 208 | "status": "ready", | ||
| 209 | "created_at": "2015-01-06T10:34:47.000Z", | ||
| 210 | "completed_at": "2015-01-06T10:35:47.000Z", | ||
| 211 | "expires_at": "2015-01-13T10:35:47.000Z", | ||
| 212 | "file_url": "https://example.com/0428b97b.../fills.pdf", | ||
| 213 | "params": { | ||
| 214 | "start_date": "2014-11-01T00:00:00.000Z", | ||
| 215 | "end_date": "2014-11-30T23:59:59.000Z" | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | |||
| 222 | =head1 AUTHOR | ||
| 223 | |||
| 224 | Mark Rushing <mark@orbislumen.net> | ||
| 225 | |||
| 226 | =head1 COPYRIGHT AND LICENSE | ||
| 227 | |||
| 228 | This software is copyright (c) 2017 by Home Grown Systems, SPC. | ||
| 229 | |||
| 230 | This is free software; you can redistribute it and/or modify it under | ||
| 231 | the same terms as the Perl 5 programming language system itself. | ||
| 232 | |||
| 233 | =cut | ||
| 234 |