Skip to content

Commit

Permalink
Merge pull request #91 from turbo124/main
Browse files Browse the repository at this point in the history
Allow downloading PDFs via API
  • Loading branch information
turbo124 authored Sep 7, 2022
2 parents 176a9d4 + 7b4c8ea commit f69f001
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/Endpoints/BaseEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,14 @@ public function create(array $entity, array $includes = [])
return $this->ninja->send("POST", "{$this->uri}", $query);
}

public function download(array $entity, array $includes = [])
{
$data = ['ids' => $entity, 'action' => 'download', 'stream' => false];

$query = ['form_params' => $data, 'query' => $includes];

return $this->ninja->stream("POST", "{$this->uri}/bulk", $query);

}
}

27 changes: 27 additions & 0 deletions src/InvoiceNinja.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,33 @@ public function send(string $method, string $uri, array $payload)
}
}

public function stream(string $method, string $uri, array $payload)
{

$this->httpClient();

$url = $this->getUrl() . $uri;

try{

$response = $this->httpClient->request($method, $url, $payload);

$body = $response->getBody();

return (string)$body;

}
catch(GuzzleException $e) {

if (method_exists($e, 'hasResponse') && method_exists($e, 'getResponse') && $e->hasResponse())
throw ApiException::createFromResponse($e->getResponse());

throw new ApiException($e->getMessage(), $e->getCode());

}

}

}


10 changes: 6 additions & 4 deletions tests/ExpenseCategoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testExpenseCategoriesPost()
$ninja = new InvoiceNinja($this->token);
$ninja->setUrl($this->url);

$expense_categories = $ninja->expense_categories->create(['name' => $this->faker->firstName]);
$expense_categories = $ninja->expense_categories->create(['name' => $this->faker->text(10)]);

$this->assertTrue(is_array($expense_categories));

Expand All @@ -48,7 +48,7 @@ public function testExpenseCategoryGet()
$ninja = new InvoiceNinja($this->token);
$ninja->setUrl($this->url);

$expense_categories = $ninja->expense_categories->create(['name' => $this->faker->firstName]);
$expense_categories = $ninja->expense_categories->create(['name' => $this->faker->text(10)]);

$this->assertTrue(is_array($expense_categories));

Expand All @@ -68,9 +68,11 @@ public function testExpenseCategoryPut()
$ninja = new InvoiceNinja($this->token);
$ninja->setUrl($this->url);

$expense_categories = $ninja->expense_categories->create(['name' => $this->faker->firstName]);
$expense_category = $this->faker->text(10);

$expense_categories = $ninja->expense_categories->create(['name' => $expense_category]);

$expense_categories = $ninja->expense_categories->update($expense_categories['data']['id'], ['name' => $this->faker->firstName]);
$expense_categories = $ninja->expense_categories->update($expense_categories['data']['id'], ['name' => $this->faker->text(10)]);

$this->assertTrue(is_array($expense_categories));

Expand Down
1 change: 0 additions & 1 deletion tests/GroupSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public function testAddGroupSetting()
$ninja = new InvoiceNinja($this->token);
$ninja->setUrl($this->url);


$settings = new \stdClass;
$settings->currency_id = '1';

Expand Down
34 changes: 34 additions & 0 deletions tests/InvoicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,38 @@ public function testInvoicePostWithMultiItemsMarkSent()

}

public function testDownloadInvoice()
{

$ninja = new InvoiceNinja($this->token);
$ninja->setUrl($this->url);

$client = $ninja->clients->create(['name' => 'Brand spanking new client']);

$invoice = [
'client_id' => $client['data']['id'],
'line_items' => [
[
'product_key' => 'test',
'notes' => 'description',
'quantity' => 1,
'cost' => 10
],
[
'product_key' => 'test',
'notes' => 'description',
'quantity' => 1,
'cost' => 10
],
],
];

$invoice = $ninja->invoices->create($invoice, ['mark_sent' => "true"]);

$download = $ninja->invoices->download([$invoice['data']['id']]);

$this->assertNotNull($download);

}

}
4 changes: 3 additions & 1 deletion tests/PaymentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public function testInvoicePost()
$ninja = new InvoiceNinja($this->token);
$ninja->setUrl($this->url);

$payments = $ninja->payments->create(['client_id' => '7LDdwRb1YK', 'amount' => 10]);
$client = $ninja->clients->create(['name' => 'Brand spanking new client']);

$payments = $ninja->payments->create(['client_id' => $client['data']['id'], 'amount' => 10]);

$this->assertTrue(is_array($payments));

Expand Down

0 comments on commit f69f001

Please sign in to comment.