Skip to content

Commit

Permalink
Merge pull request #243 from helpscout/v3-thread-source
Browse files Browse the repository at this point in the history
SDK v3: Added ability to get a thread's source
  • Loading branch information
bkuhl authored Jun 30, 2020
2 parents 4871e48 + 01ba6ed commit d53f4db
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 6 deletions.
4 changes: 4 additions & 0 deletions examples/threads.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@
$conversationId = 0;
$threadId = 0;
$client->threads()->updateText(18, $threadId, 'I need help please');

// Get the source of a thread
$source = $client->threads()->getSource(1189656771, 3394140565);
print_r($source->getOriginal());
43 changes: 43 additions & 0 deletions src/Conversations/Threads/Source.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace HelpScout\Api\Conversations\Threads;

use HelpScout\Api\Entity\Extractable;
use HelpScout\Api\Entity\Hydratable;

class Source implements Extractable, Hydratable
{
/**
* @var string
*/
private $original;

public function hydrate(array $data, array $embedded = [])
{
$this->setOriginal($data['original']);
}

/**
* {@inheritdoc}
*/
public function extract(): array
{
return [
'original' => $this->getOriginal(),
];
}

public function getOriginal(): ?string
{
return $this->original;
}

public function setOriginal(string $original): self
{
$this->original = $original;

return $this;
}
}
11 changes: 11 additions & 0 deletions src/Conversations/Threads/ThreadsEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ public function updateText(int $conversationId, int $threadId, string $newText):
);
}

public function getSource(int $conversationId, int $threadId): Source
{
return $this->loadResource(
Source::class,
sprintf('/v2/conversations/%d/threads/%d/original-source', $conversationId, $threadId),
[
'Accept' => 'application/json',
]
);
}

/**
* @return Thread[]|PagedCollection
*/
Expand Down
9 changes: 6 additions & 3 deletions src/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ public function __construct(RestClient $restClient)
/**
* @return mixed
*/
protected function loadResource(string $entityClass, string $url)
{
$resource = $this->restClient->getResource($entityClass, $url);
protected function loadResource(
string $entityClass,
string $url,
array $headers = []
) {
$resource = $this->restClient->getResource($entityClass, $url, $headers);

return $resource->getEntity();
}
Expand Down
9 changes: 6 additions & 3 deletions src/Http/RestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,15 @@ public function deleteResource(string $uri): void
/**
* @param Closure|string $entityClass
*/
public function getResource($entityClass, string $uri): HalResource
{
public function getResource(
$entityClass,
string $uri,
array $headers = []
): HalResource {
$request = new Request(
'GET',
$uri,
$this->getDefaultHeaders()
array_merge($this->getDefaultHeaders(), $headers)
);
$response = $this->send($request);
$halDocument = HalDeserializer::deserializeDocument((string) $response->getBody());
Expand Down
44 changes: 44 additions & 0 deletions tests/Conversations/Threads/SourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace HelpScout\Api\Tests\Conversations\Threads;

use HelpScout\Api\Conversations\Threads\Source;
use PHPUnit\Framework\TestCase;

class SourceTest extends TestCase
{
public function testHydrate()
{
$original = 'original email text';
$source = new Source();
$source->hydrate([
'original' => $original,
]);

$this->assertSame($original, $source->getOriginal());
}

public function testExtract()
{
$original = 'original email text';
$source = new Source();
$source->setOriginal($original);

$extracted = $source->extract();

$this->assertEquals($original, $extracted['original']);
}

public function testSetAndGetOriginal()
{
$source = new Source();
$this->assertNull($source->getOriginal());
$original = 'original email text';

$source->setOriginal($original);

$this->assertEquals($original, $source->getOriginal());
}
}
8 changes: 8 additions & 0 deletions tests/Conversations/Threads/ThreadIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,12 @@ public function testCanUpdateThreadText()
'value' => 'This is new text',
]);
}

public function testCanGetThreadSource()
{
$this->stubResponse($this->getResponse(200, json_encode(ThreadPayloads::getThreadSource())));
$this->client->threads()->getSource(1, 1432);

$this->verifySingleRequest('https://api.helpscout.net/v2/conversations/1/threads/1432/original-source');
}
}
7 changes: 7 additions & 0 deletions tests/Payloads/ThreadPayloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,11 @@ private static function thread(int $id): array
'createdAt' => '2017-04-21T14:39:56Z',
];
}

public static function getThreadSource(): array
{
return [
'original' => file_get_contents(__DIR__.'/original-source.txt'),
];
}
}
56 changes: 56 additions & 0 deletions tests/Payloads/original-source.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Correlation-Id: f8d4c6c8-bd1c-44df-ad4a-513aa17674f9
X-Hs-Spam-Score: -4.463879
X-Spam-Checks: {"ARC_NA":{"score":0},"R_DKIM_ALLOW":{"score":-0.2,"options":["helpscout.com:s=google"]},"RWL_MAILSPIKE_POSSIBLE":{"score":0,"options":["54.221.85.209.rep.mailspike.net : 127.0.0.17"]},"FROM_HAS_DN":{"score":0},"R_SPF_ALLOW":{"score":-0.2,"options":["+ip4:209.85.128.0\/17"]},"MIME_GOOD":{"score":-0.1,"options":["multipart\/alternative","text\/plain"]},"PREVIOUSLY_DELIVERED":{"score":0,"options":["[email protected]"]},"TO_DN_NONE":{"score":0},"RCPT_COUNT_ONE":{"score":0,"options":["1"]},"DKIM_TRACE":{"score":0,"options":["helpscout.com:+"]},"DMARC_POLICY_ALLOW":{"score":-0.5,"options":["helpscout.com","none"]},"RCVD_IN_DNSWL_NONE":{"score":0,"options":["54.221.85.209.list.dnswl.org : 127.0.5.0"]},"NEURAL_HAM":{"score":-0,"options":["-1.000","0"]},"FROM_EQ_ENVFROM":{"score":0},"MIME_TRACE":{"score":0,"options":["0:+","1:+","2:~"]},"IP_SCORE":{"score":-3.463879,"options":["ip: (-9.05), ipnet: 209.85.128.0\/17(-4.55), asn: 15169(-3.64), country: US(-0.09)"]},"ASN":{"score":0,"options":["asn:15169, ipnet:209.85.128.0\/17, country:US"]},"HFILTER_HOSTNAME_UNKNOWN":{"score":0},"RCVD_TLS_ALL":{"score":0},"RCVD_COUNT_TWO":{"score":0,"options":["2"]}}
X-Incoming-Source: email
Return-path: <[email protected]>
Envelope-to: [email protected]
Delivery-date: Tue, 09 Jun 2020 13:26:55 +0000
Received: from mail-wr1-f54.google.com ([209.85.221.54])
by mx1-1d.helpscout.net with esmtps (TLS1.2:RSA_AES_128_CBC_SHA1:128)
(Exim 4.82)
(envelope-from <[email protected]>)
id 1jieHH-0006c3-6I
for [email protected]; Tue, 09 Jun 2020 13:26:55 +0000
Received: by mail-wr1-f54.google.com with SMTP id x6so21248147wrm.13
for <[email protected]>; Tue, 09 Jun 2020 06:26:55 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=helpscout.com; s=google;
h=mime-version:from:date:message-id:subject:to;
bh=6Z7hq0L6aBpZwdkU1h+vKuK04L9HZukkazoBEn3amIE=;
b=WpvlC+d2vjMABkSxaupG4QpOHuMG52tZPAylx1V1ixe0GJRs14FdW7ZHkL9/MHiTNR
B+U8lAoI7l8G/Q4kXtt7lHVNNyes2Y3vjZ1GG6+N3rJILmQSPiqNnZTx4517Y4qwXY6c
QhDeYFBg6BMsJQBMKVi9jgo90NWH7zYXM6Y0I=
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20161025;
h=x-gm-message-state:mime-version:from:date:message-id:subject:to;
bh=6Z7hq0L6aBpZwdkU1h+vKuK04L9HZukkazoBEn3amIE=;
b=rPcVBNWjf2s/NRHyHQGli2FAX8CzkeFEeJuPsLBSamHHnFQur1pgomPAVNNQ7HhQOh
E/Gj1Mqz58Tkwr+3EQL9ZltRls+pYOw5p8jE0cRxjXaBiYRQ61XBinQKvTfdM7pCAV1U
gKMd0lfSxmp01SZb8/yzfjd90OtmlDJDw8GvkUAfrxW28Ztt1VEIPSXHqI4L1YTlQ6YG
tSL8C+SUmJ9JjNFiJH5PQ+Xuw8g0nnKtbrvzffoR9lAXWzcKl8MvHy+YWa2YOv6DZ9gw
hmNBDDcdJSKdz3mt3HGMLLLykuIwQa5W99zPNp16gPHXjf6Tz4og4u19Q3SWZmUCxn1B
5xDg==
X-Gm-Message-State: AOAM5322pI+asDDh0bhopd5OvnY+UTrXuhuwke8ufO1lK/Pd7w779xgN
oMGCoSmXSBVh4oqDy1vbe4VqegnWx+EF44tm78j51xuiovY=
X-Google-Smtp-Source: ABdhPJxDRtJa5jCAWmHe3aWcSZEQi4Cg100t3mt4unaRwbjKbznbs+ZB9cVMq7O/tnEocZpyloL0wUx4wwlahOaREzU=
X-Received: by 2002:adf:df03:: with SMTP id y3mr4279506wrl.376.1591709214305;
Tue, 09 Jun 2020 06:26:54 -0700 (PDT)
MIME-Version: 1.0
From: Customer A <[email protected]>
Date: Tue, 9 Jun 2020 09:26:43 -0400
Message-ID: <CAOED0cxvvDSHOV6v2nYKKD_SovaSZ6G5xYDY77KWaxoRj+9mnA@mail.gmail.com>
Subject: Pizza is awesome
To: [email protected]
Content-Type: multipart/alternative; boundary="0000000000004e123105a7a6ae81"

--0000000000004e123105a7a6ae81
Content-Type: text/plain; charset="UTF-8"

I love pizza parties

--0000000000004e123105a7a6ae81
Content-Type: text/html; charset="UTF-8"

<div dir="ltr"><span style="color:rgb(37,53,64);font-family:&quot;Aktiv Grotesk&quot;,&quot;Helvetica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px">I love pizza parties</span><br></div>

--0000000000004e123105a7a6ae81--

0 comments on commit d53f4db

Please sign in to comment.