Skip to content

Commit

Permalink
split CurlHttpDriver out of Connection and simplified. ConnectionSett…
Browse files Browse the repository at this point in the history
…ings no longer needed: they are all driver parameters now
  • Loading branch information
pavarnos committed Aug 12, 2018
1 parent 05b7830 commit 0b26785
Show file tree
Hide file tree
Showing 28 changed files with 382 additions and 407 deletions.
19 changes: 8 additions & 11 deletions samples/testFetchPublicKey.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
<?php

use Threema\MsgApi\Connection;
use Threema\MsgApi\ConnectionSettings;

require_once '../vendor/autoload.php';

//define your connection settings
$settings = new ConnectionSettings(
'*YOUR_GATEWAY_THREEMA_ID',
'YOUR_GATEWAY_THREEMA_ID_SECRET'
$driver = new \Threema\MsgApi\HttpDriver\CurlHttpDriver(
'*YOUR_GATEWAY_THREEMA_ID',
'YOUR_GATEWAY_THREEMA_ID_SECRET'
);

//create a connection
$connector = new Connection($settings);

$connector = new Connection($driver);

$result = $connector->fetchPublicKey('ECHOECHO');
if($result->isSuccess()) {
echo 'public key '.$result->getPublicKey() . "\n";
}
else {
echo 'error '.$result->getErrorMessage() . "\n";
if ($result->isSuccess()) {
echo 'public key ' . $result->getPublicKey() . "\n";
} else {
echo 'error ' . $result->getErrorMessage() . "\n";
}
5 changes: 2 additions & 3 deletions samples/testKeyLookup.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

use Threema\MsgApi\Connection;
use Threema\MsgApi\ConnectionSettings;

require_once '../vendor/autoload.php';

//define your connection settings
$settings = new ConnectionSettings(
$driver = new \Threema\MsgApi\HttpDriver\CurlHttpDriver(
'*YOUR_GATEWAY_THREEMA_ID',
'YOUR_GATEWAY_THREEMA_ID_SECRET'
);

//create a connection
$connector = new Connection($settings);
$connector = new Connection($driver);

$result = $connector->keyLookupByPhoneNumber('123456789');
if ($result->isSuccess()) {
Expand Down
5 changes: 2 additions & 3 deletions samples/testSendE2EFile.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

use Threema\MsgApi\Connection;
use Threema\MsgApi\ConnectionSettings;

require_once '../vendor/autoload.php';

//define your connection settings
$settings = new ConnectionSettings(
$driver = new \Threema\MsgApi\HttpDriver\CurlHttpDriver(
'*YOUR_GATEWAY_THREEMA_ID',
'YOUR_GATEWAY_THREEMA_ID_SECRET'
);

//create a connection
$connector = new Connection($settings);
$connector = new Connection($driver);

$senderPrivateKey = "MY_PUBLIC_KEY_IN_BIN";
$filePath = "/path/to/my/file.pdf";
Expand Down
22 changes: 10 additions & 12 deletions samples/testSendE2EText.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
<?php

use Threema\MsgApi\Connection;
use Threema\MsgApi\ConnectionSettings;

require_once '../vendor/autoload.php';

//define your connection settings
$settings = new ConnectionSettings(
'*YOUR_GATEWAY_THREEMA_ID',
'YOUR_GATEWAY_THREEMA_ID_SECRET'
$driver = new \Threema\MsgApi\HttpDriver\CurlHttpDriver(
'*YOUR_GATEWAY_THREEMA_ID',
'YOUR_GATEWAY_THREEMA_ID_SECRET'
);

//create a connection
$connector = new Connection($settings);
$connector = new Connection($driver);

$senderPrivateKey = "MY_PUBLIC_KEY_IN_BIN";

$e2eHelper = new \Threema\MsgApi\Helpers\E2EHelper($senderPrivateKey,$connector);
$result = $e2eHelper->sendTextMessage("TEST1234", "thePublicKeyAsHex", "This is an end-to-end encrypted message");
$e2eHelper = new \Threema\MsgApi\Helpers\E2EHelper($senderPrivateKey, $connector);
$result = $e2eHelper->sendTextMessage("TEST1234", "thePublicKeyAsHex", "This is an end-to-end encrypted message");

if(true === $result->isSuccess()) {
echo 'Message ID: '.$result->getMessageId() . "\n";
}
else {
echo 'Error: '.$result->getErrorMessage() . "\n";
if (true === $result->isSuccess()) {
echo 'Message ID: ' . $result->getMessageId() . "\n";
} else {
echo 'Error: ' . $result->getErrorMessage() . "\n";
}
8 changes: 3 additions & 5 deletions samples/testSendSimple.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
<?php

use Threema\MsgApi\Connection;
use Threema\MsgApi\ConnectionSettings;
use Threema\MsgApi\Receiver;

require_once '../vendor/autoload.php';

//define your connection settings
$settings = new ConnectionSettings(
$driver = new \Threema\MsgApi\HttpDriver\CurlHttpDriver(
'*YOUR_GATEWAY_THREEMA_ID',
'YOUR_GATEWAY_THREEMA_ID_SECRET'
);

//create a connection
$connector = new Connection($settings);
$connector = new Connection($driver);

//create a receiver
$receiver = new Receiver('ECHOECHO',
Receiver::TYPE_ID);
$receiver = new Receiver('ECHOECHO', Receiver::TYPE_ID);

$result = $connector->sendSimple($receiver, "This is a Test Message");
if ($result->isSuccess()) {
Expand Down
5 changes: 2 additions & 3 deletions src/Threema/Console/Symfony/AbstractNetworkedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
use Threema\Core\Exception;
use Threema\MsgApi\Commands\Results\Result;
use Threema\MsgApi\Connection;
use Threema\MsgApi\ConnectionSettings;
use Threema\MsgApi\Constants;
use Threema\MsgApi\Exceptions\InvalidArgumentException;
use Threema\MsgApi\HttpDriver\CurlHttpDriver;

abstract class AbstractNetworkedCommand extends AbstractLocalCommand
{
Expand All @@ -33,8 +33,7 @@ protected function configure()
protected function getConnection(InputInterface $input, OutputInterface $output): Connection
{
$this->loadDefaults($input, $output);
$settings = new ConnectionSettings($this->getSenderID($input), $this->getSecret($input, $output));
return new Connection($settings);
return new Connection(new CurlHttpDriver($this->getSenderID($input), $this->getSecret($input, $output)));
}

protected function assertSuccess(Result $result)
Expand Down
6 changes: 3 additions & 3 deletions src/Threema/MsgApi/Commands/Capability.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public function __construct($threemaId)
/**
* @return array
*/
public function getParams()
public function getParams(): array
{
return [];
}

public function getPath()
public function getPath(): string
{
return 'capabilities/' . $this->threemaId;
}
Expand All @@ -41,7 +41,7 @@ public function getPath()
* @param object $res
* @return CapabilityResult
*/
public function parseResult($httpCode, $res)
public function parseResult($httpCode, $res): \Threema\MsgApi\Commands\Results\Result
{
return new CapabilityResult($httpCode, $res);
}
Expand Down
8 changes: 5 additions & 3 deletions src/Threema/MsgApi/Commands/CommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* @copyright Copyright (c) 2015-2016 Threema GmbH
*/

declare(strict_types=1);

namespace Threema\MsgApi\Commands;

use Threema\MsgApi\Commands\Results\Result;
Expand All @@ -13,17 +15,17 @@ interface CommandInterface
/**
* @return string
*/
public function getPath();
public function getPath(): string;

/**
* @return array
*/
public function getParams();
public function getParams(): array;

/**
* @param int $httpCode
* @param object $res
* @return Result
*/
public function parseResult($httpCode, $res);
public function parseResult($httpCode, $res): \Threema\MsgApi\Commands\Results\Result;
}
6 changes: 3 additions & 3 deletions src/Threema/MsgApi/Commands/Credits.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class Credits implements CommandInterface
/**
* @return array
*/
public function getParams()
public function getParams(): array
{
return [];
}

public function getPath()
public function getPath(): string
{
return 'credits';
}
Expand All @@ -28,7 +28,7 @@ public function getPath()
* @param object $res
* @return CreditsResult
*/
public function parseResult($httpCode, $res)
public function parseResult($httpCode, $res): \Threema\MsgApi\Commands\Results\Result
{
return new CreditsResult($httpCode, $res);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Threema/MsgApi/Commands/DownloadFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public function __construct($blobId)
/**
* @return array
*/
public function getParams()
public function getParams(): array
{
return [];
}

/**
* @return string
*/
public function getPath()
public function getPath(): string
{
return 'blobs/' . $this->blobId;
}
Expand All @@ -44,7 +44,7 @@ public function getPath()
* @param object $res
* @return DownloadFileResult
*/
public function parseResult($httpCode, $res)
public function parseResult($httpCode, $res): \Threema\MsgApi\Commands\Results\Result
{
return new DownloadFileResult($httpCode, $res);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Threema/MsgApi/Commands/FetchPublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public function __construct($threemaId)
/**
* @return array
*/
public function getParams()
public function getParams(): array
{
return [];
}

/**
* @return string
*/
public function getPath()
public function getPath(): string
{
return 'pubkeys/' . $this->threemaId;
}
Expand All @@ -44,7 +44,7 @@ public function getPath()
* @param object $res
* @return FetchPublicKeyResult
*/
public function parseResult($httpCode, $res)
public function parseResult($httpCode, $res): \Threema\MsgApi\Commands\Results\Result
{
return new FetchPublicKeyResult($httpCode, $res);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Threema/MsgApi/Commands/JsonCommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* @copyright Copyright (c) 2015-2016 Threema GmbH
*/

declare(strict_types=1);

namespace Threema\MsgApi\Commands;

interface JsonCommandInterface extends CommandInterface
Expand Down
6 changes: 3 additions & 3 deletions src/Threema/MsgApi/Commands/LookupBulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function findPhone(string $phoneHash): array
/**
* @return array
*/
public function getParams()
public function getParams(): array
{
return [];
}
Expand All @@ -77,7 +77,7 @@ public function getJson(): string
/**
* @return string
*/
public function getPath()
public function getPath(): string
{
return 'lookup/bulk';
}
Expand All @@ -87,7 +87,7 @@ public function getPath()
* @param object $res
* @return LookupBulkResult
*/
public function parseResult($httpCode, $res)
public function parseResult($httpCode, $res): \Threema\MsgApi\Commands\Results\Result
{
return new LookupBulkResult($httpCode, $res, $this);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Threema/MsgApi/Commands/LookupEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public function getEmailAddress()
/**
* @return array
*/
public function getParams()
public function getParams(): array
{
return [];
}

/**
* @return string
*/
public function getPath()
public function getPath(): string
{
return 'lookup/email_hash/' . urlencode(AbstractEncryptor::getInstance()->hashEmail($this->emailAddress));
}
Expand All @@ -53,7 +53,7 @@ public function getPath()
* @param object $res
* @return LookupIdResult
*/
public function parseResult($httpCode, $res)
public function parseResult($httpCode, $res): \Threema\MsgApi\Commands\Results\Result
{
return new LookupIdResult($httpCode, $res);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Threema/MsgApi/Commands/LookupPhone.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public function getPhoneNumber()
/**
* @return array
*/
public function getParams()
public function getParams(): array
{
return [];
}

/**
* @return string
*/
public function getPath()
public function getPath(): string
{
return 'lookup/phone_hash/' . urlencode(AbstractEncryptor::getInstance()->hashPhoneNo($this->phoneNumber));
}
Expand All @@ -53,7 +53,7 @@ public function getPath()
* @param object $res
* @return LookupIdResult
*/
public function parseResult($httpCode, $res)
public function parseResult($httpCode, $res): \Threema\MsgApi\Commands\Results\Result
{
return new LookupIdResult($httpCode, $res);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Threema/MsgApi/Commands/MultiPartCommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
* @copyright Copyright (c) 2015-2016 Threema GmbH
*/

declare(strict_types=1);

namespace Threema\MsgApi\Commands;

interface MultiPartCommandInterface extends CommandInterface
{
public function getData();
public function getData(): string;
}
Loading

0 comments on commit 0b26785

Please sign in to comment.