Skip to content

Commit

Permalink
Reset client between requests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronhuisinga committed Jan 12, 2021
1 parent 34746c1 commit 45c8e54
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/ClickSend.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
use CraftCodery\ClickSend\Models\ClickSendReturnAddress;
use CraftCodery\ClickSend\Traits\CanReceiveMailers;
use CraftCodery\ClickSend\Traits\CanSendMailers;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;

class ClickSend
{
protected string $base_url;
protected PendingRequest $client;

public function __construct()
{
$this->base_url = 'https://' . (app()->isProduction() ? 'rest.clicksend.com' : 'private-anon-5719b2dfad-clicksend.apiary-mock.com') . '/v3/';
$this->client = Http::withBasicAuth(config('clicksend.username'), config('services.clicksend.key'))->timeout(15);
}

/**
Expand All @@ -35,14 +32,16 @@ public function sendLetter($sender, $recipient, string $content): array
$pdf = $this->generatePdf($content);
$pdf_url = $this->uploadFile($pdf->output());

$this->client->post($this->base_url . 'post/letters/send', [
'file_url' => $pdf_url,
'template_used' => 0,
'colour' => 0,
'duplex' => 1,
'priority_post' => 0,
'recipients' => $this->formatDataForMailer($sender, $recipient),
]);
Http::withBasicAuth(config('clicksend.username'), config('services.clicksend.key'))
->timeout(15)
->post($this->base_url . 'post/letters/send', [
'file_url' => $pdf_url,
'template_used' => 0,
'colour' => 0,
'duplex' => 1,
'priority_post' => 0,
'recipients' => $this->formatDataForMailer($sender, $recipient),
]);

$page_count = $pdf->getDomPdf()->getCanvas()->get_page_count();

Expand Down Expand Up @@ -70,10 +69,12 @@ public function sendPostcard($sender, $recipient, string $front_pdf_url, string
$rear_pdf = $this->generatePdf($content, $rear_pdf_options);
$rear_pdf_url = $this->uploadFile($rear_pdf->output());

$this->client->post($this->base_url . 'post/postcards/send', [
'file_urls' => [$front_pdf_url, $rear_pdf_url],
'recipients' => $this->formatDataForMailer($sender, $recipient),
]);
Http::withBasicAuth(config('clicksend.username'), config('services.clicksend.key'))
->timeout(15)
->post($this->base_url . 'post/postcards/send', [
'file_urls' => [$front_pdf_url, $rear_pdf_url],
'recipients' => $this->formatDataForMailer($sender, $recipient),
]);
}

/**
Expand Down Expand Up @@ -125,7 +126,9 @@ protected function generatePdf(string $content, array $options = [])
*/
protected function uploadFile($file)
{
$response = $this->client->attach('file', $file, 'file.pdf')->post($this->base_url . 'uploads?convert=post');
$response = Http::withBasicAuth(config('clicksend.username'), config('services.clicksend.key'))
->timeout(15)
->attach('file', $file, 'file.pdf')->post($this->base_url . 'uploads?convert=post');

return $response->json()['data']['_url'];
}
Expand Down Expand Up @@ -177,7 +180,9 @@ protected function getReturnAddressId($sender)
*/
protected function createReturnAddress(array $return_address_data)
{
$response = $this->client->post($this->base_url . 'post/return-addresses', $return_address_data);
$response = Http::withBasicAuth(config('clicksend.username'), config('services.clicksend.key'))
->timeout(15)
->post($this->base_url . 'post/return-addresses', $return_address_data);
$response = $response->json()['data'];

$return_address = ClickSendReturnAddress::create([
Expand Down

0 comments on commit 45c8e54

Please sign in to comment.