diff --git a/examples/threads.php b/examples/threads.php index c8861d10..67d22b1e 100644 --- a/examples/threads.php +++ b/examples/threads.php @@ -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()); \ No newline at end of file diff --git a/src/Conversations/Threads/Source.php b/src/Conversations/Threads/Source.php new file mode 100644 index 00000000..52775d04 --- /dev/null +++ b/src/Conversations/Threads/Source.php @@ -0,0 +1,43 @@ +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; + } +} diff --git a/src/Conversations/Threads/ThreadsEndpoint.php b/src/Conversations/Threads/ThreadsEndpoint.php index e063b782..8feeb4f8 100644 --- a/src/Conversations/Threads/ThreadsEndpoint.php +++ b/src/Conversations/Threads/ThreadsEndpoint.php @@ -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 */ diff --git a/src/Endpoint.php b/src/Endpoint.php index 27dbf49a..d0d467b5 100644 --- a/src/Endpoint.php +++ b/src/Endpoint.php @@ -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(); } diff --git a/src/Http/RestClient.php b/src/Http/RestClient.php index 1b36b3d8..33cedfe0 100644 --- a/src/Http/RestClient.php +++ b/src/Http/RestClient.php @@ -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()); diff --git a/tests/Conversations/Threads/SourceTest.php b/tests/Conversations/Threads/SourceTest.php new file mode 100644 index 00000000..3a5e1949 --- /dev/null +++ b/tests/Conversations/Threads/SourceTest.php @@ -0,0 +1,44 @@ +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()); + } +} diff --git a/tests/Conversations/Threads/ThreadIntegrationTest.php b/tests/Conversations/Threads/ThreadIntegrationTest.php index 08b36519..4ef8c9eb 100644 --- a/tests/Conversations/Threads/ThreadIntegrationTest.php +++ b/tests/Conversations/Threads/ThreadIntegrationTest.php @@ -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'); + } } diff --git a/tests/Payloads/ThreadPayloads.php b/tests/Payloads/ThreadPayloads.php index 65b03727..44c534eb 100644 --- a/tests/Payloads/ThreadPayloads.php +++ b/tests/Payloads/ThreadPayloads.php @@ -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'), + ]; + } } diff --git a/tests/Payloads/original-source.txt b/tests/Payloads/original-source.txt new file mode 100644 index 00000000..45e134fd --- /dev/null +++ b/tests/Payloads/original-source.txt @@ -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":["my-sdk-mailbox@helpscout.net"]},"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: +Envelope-to: my-sdk-mailbox@helpscout.net +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 ) + id 1jieHH-0006c3-6I + for my-sdk-mailbox@helpscout.net; Tue, 09 Jun 2020 13:26:55 +0000 +Received: by mail-wr1-f54.google.com with SMTP id x6so21248147wrm.13 + for ; 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 +Date: Tue, 9 Jun 2020 09:26:43 -0400 +Message-ID: +Subject: Pizza is awesome +To: my-sdk-mailbox@helpscout.net +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" + +
I love pizza parties
+ +--0000000000004e123105a7a6ae81--