SparkPost/php-sparkpost

DELETE Method

Closed this issue · 5 comments

Hi There,

use GuzzleHttp\Client;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
use SparkPost\SparkPost;

....
$httpClient = new GuzzleAdapter(new Client());
$this->sparky = new SparkPost($httpClient, [
'key' => $apiKey,
'async' => false]);
$this->apiKey = $apiKey;

....

->request('DELETE', '/webhooks/' . $webhookId);

is not working. Seems like it is converted into GET-Request.

If i use normal curl snippet it is working fine.

Best regards

Hey 👋 I'm having trouble replicating the problem. Can you share the version and system you're running on and maybe a more complete code snippet?

/**
*SparkpostApiClass
*
*/
namespace lib\sparkpost;

use GuzzleHttp\Client;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
use SparkPost\SparkPost;

class SparkpostApi
{
    protected $sparky;
    protected $lastCode;
    protected $lastError;
    protected $apiKey;

    /**
     * @param $apiKey
     * @return SparkpostApi
     */
    static function init($apiKey)
    {
        $class = self::class;
        $api = new $class($apiKey);
        return $api;
    }

    function __construct($apiKey)
    {
        $httpClient = new GuzzleAdapter(new Client());
        $this->sparky = new SparkPost($httpClient, [
            'key' => $apiKey,
            'async' => false]);
        $this->apiKey = $apiKey;

    }

    function checkKey()
    {
        $domains = $this->getSendingDomains();

        if ($this->lastCode == 200) {
            return $domains;
        }
        return false;
    }

    function getSendingDomains()
    {
        $this->lastError = null;
        try {
            $response = $this->sparky->request('GET', '/sending-domains');

            $this->lastCode = $response->getStatusCode();
            return $response->getBody();
        } catch (\Exception $e) {
            $this->lastCode = $e->getCode();
            $this->lastError = $e->getMessage();
            return false;
        }
    }

    
   
    function deleteWebhook($webhookId)
    {
        $this->lastError = null;
        try {
            $response = $this->sparky->request('DELETE', '/webhooks/' . $webhookId);
            $this->lastCode = $response->getStatusCode();
            print_r($response->getBody());
            return $response->getBody();
        } catch (\Exception $e) {
            $this->lastCode = $e->getCode();
            $this->lastError = $e->getMessage();
            return false;
        }
    }
}

Tested on CentOS 7 and win10
composer.json
....
"php-http/guzzle6-adapter": "dev-master",
"sparkpost/sparkpost": "2.1.0"
....

$curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://api.sparkpost.com/api/v1/webhooks/".$webhookId,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 2,
            CURLOPT_TIMEOUT => 10,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "DELETE",

            CURLOPT_HTTPHEADER => array(
                "Accept: application/json",
                "Authorization: " . $this->apiKey,
                "Cache-Control: no-cache",
                "Content-Type: application/json",

            ),
        ));


        $response = curl_exec($curl);

        $err = curl_error($curl);

        curl_close($curl);
        return json_decode($response);

this one is working fine

Hi, I've reproduced the issue using your original code. The leading character in the string /webhooks/ is not needed, and results in a URI of the form "https://api.sparkpost.com:443/api/v1//webhooks/afd20f50-865a-11eb-ac38-6d7965d56459" (with a double // in). This gives a 200 response and nothing is deleted.

The following code does delete the webhook, and gives the expected 204 response:

$httpClient = new GuzzleAdapter(new Client());
$sparky = new SparkPost($httpClient, [
    'key' => getenv('SPARKPOST_API_KEY'),
    'async' => false]);

$webhookId = 'afd20f50-865a-11eb-ac38-6d7965d56459';
$response = $sparky->request('DELETE', '/webhooks/' . $webhookId);
print($response->getStatusCode());

The code that builds the composite URL is here:

public function getUrl($path, $params = [])

It could probably be made smarter when concatenating the parts together.

I've addressed this by improving the base request description in the README in #201