Switch Message::respond() to use Http::class instead of curl directly

This commit is contained in:
Deon George 2022-09-05 22:26:08 +10:00
parent 2c791ccead
commit a1be3ccd09

View File

@ -7,6 +7,7 @@ use Carbon\CarbonInterface;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Slack\Blockkit\Blocks; use Slack\Blockkit\Blocks;
@ -188,30 +189,20 @@ final class Message extends BlockKit
* @note This URL can only be used 5 times in 30 minutes * @note This URL can only be used 5 times in 30 minutes
* @param string $url * @param string $url
* @param Carbon|null $delete * @param Carbon|null $delete
* @return string * @return object
* @throws SlackException * @throws SlackException
*/ */
public function respond(string $url,Carbon $delete=NULL) public function respond(string $url,Carbon $delete=NULL): object
{ {
// @todo change this to use Http::class or the API?
if (! $delete && $this->selfdestruct) if (! $delete && $this->selfdestruct)
$delete = $this->selfdestruct; $delete = $this->selfdestruct;
$request = curl_init(); $http = Http::acceptJson();
$http->withBody(json_encode($this),'application/json');
curl_setopt($request,CURLOPT_URL,$url);
curl_setopt($request,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($request,CURLINFO_HEADER_OUT,TRUE);
curl_setopt($request,CURLOPT_HTTPHEADER,['Content-Type: application/json; charset=utf-8']);
curl_setopt($request,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($request,CURLOPT_POSTFIELDS,json_encode($this));
try { try {
$result = curl_exec($request); $request = $http->post($url)->throw();
if (! $result) $response = $request->object();
throw new \Exception('CURL exec returned an empty response: '.serialize(curl_getinfo($request)));
$result = json_decode($result);
} catch (\Exception $e) { } catch (\Exception $e) {
Log::error(sprintf('%s:Got an error while posting to [%s] (%s)',static::LOGKEY,$url,$e->getMessage()),['m'=>__METHOD__]); Log::error(sprintf('%s:Got an error while posting to [%s] (%s)',static::LOGKEY,$url,$e->getMessage()),['m'=>__METHOD__]);
@ -219,12 +210,9 @@ final class Message extends BlockKit
throw new \Exception($e->getMessage()); throw new \Exception($e->getMessage());
} }
if (! $result->ok) { if (! $response->ok) {
switch ($result) { Log::critical(sprintf('%s:Generic Error',static::LOGKEY),['m'=>__METHOD__,'r'=>$response]);
default: throw new SlackException(serialize($response),$request->status());
Log::critical(sprintf('%s:Generic Error',static::LOGKEY),['m'=>__METHOD__,'r'=>$result]);
throw new SlackException($result,curl_getinfo($request,CURLINFO_HTTP_CODE));
}
} }
if ($delete) { if ($delete) {
@ -234,8 +222,7 @@ final class Message extends BlockKit
dispatch((new DeleteResponse($url))->onQueue('slack')->delay($delete)); dispatch((new DeleteResponse($url))->onQueue('slack')->delay($delete));
} }
curl_close($request); return $response;
return $result;
} }
/** /**