Skip to content

Commit

Permalink
Added url encode and decode
Browse files Browse the repository at this point in the history
  • Loading branch information
Fermin committed Feb 1, 2023
1 parent b80ad51 commit a125199
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,29 @@ try {
}
```

Url encode
```php
<?php

require_once 'vendor/autoload.php';

use Vicidial\Api\Wrapper\Admin\Client;

$fields['first_name'] = "John";
$fields['last_name'] = "Doe";
$fields['address1'] = "123 Fake St";
$fields['phone_number'] = "18002474747";

try {
$agent_api = new Client("10.0.0.15", "apiuser", "apipass", "test", false);
echo $agent_api
->withUrlEncode(true)
->add_lead($fields);
} catch (Exception $e) {
echo 'Exception: ', $e->getMessage(), "\n";
}
```

### Docs:
- [Agent](https://github.com/masterfermin02/vicidial-api-wrapper/blob/main/docs/agent.md)
- [Admin](https://github.com/masterfermin02/vicidial-api-wrapper/blob/main/docs/admin.md)
49 changes: 31 additions & 18 deletions src/Admin/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@
* @method string update_cid_group_entry(array $options)
* @method string copy_user(array $options)
*/
class Client extends BaseClient {
class Client extends BaseClient
{

/**
* @var string
*/
protected $base_url;
protected string $base_url;
protected bool $encodeUrl = false;

protected $actions = [
'version',
Expand Down Expand Up @@ -111,17 +110,19 @@ class Client extends BaseClient {
'update_campaign',
'add_did',
'update_did',
'update_cid_group_entry',
'copy_user'
'update_cid_group_entry',
'copy_user',
];

/**
* Client constructor.
* @param $server_ip
* @param $api_user
* @param $api_password
* @param $source
*
* @param $server_ip
* @param $api_user
* @param $api_password
* @param $source
* @param bool $hasSSl
*
* @throws InvalidIpException
*/
public function __construct(
Expand Down Expand Up @@ -160,32 +161,44 @@ public static function create(
);
}

public function withUrlEncode(bool $encodeUrl): self
{
$this->encodeUrl = $encodeUrl;

return $this;
}

/**
* Make the api call and return an string
*
* @throws BadMethodCallException
* @param string $fun
* @param array $options
* @param array $options
*
* @return string
* @throws BadMethodCallException
* @throws Exception
*/
public function run(string $fun, array $options = []): string
{
if (!in_array($fun, $this->actions)) {
throw new BadMethodCallException("Method {$fun} does not exist");
}
$options = $this->encode($options) + [
'function' => $fun
];
$options = $this->encodeUrl ? $this->encode($options) : $options;

return $this->callApiUrl($this->base_url, $options);
return $this->callApiUrl(
$this->base_url,
$options + [
'function' => $fun,
]
);
}

/**
* Handle calls to non-existent methods.
*
* @param string $method
* @param array $arguments
* @param array $arguments
*
* @return mixed
*/
public function __call($method, array $arguments = [])
Expand Down
10 changes: 6 additions & 4 deletions src/Agent/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function change_ingroups($agent_user, $options)
*/
public function update_fields($agent_user, $fields_to_update)
{
if ( !is_array($fields_to_update)) {
if (!is_array($fields_to_update)) {
throw new Exception('Fields must be an array');
}

Expand All @@ -256,15 +256,17 @@ public function update_fields($agent_user, $fields_to_update)
);

// Validate that every single field to update us valid
foreach( $fields_to_update as $key => $value ){
if ( !in_array($key, $permited_fields))
foreach($fields_to_update as $key => $value ) {
if (!in_array($key, $permited_fields)) {
throw new Exception("$key is not a valid field");
}
}

$options = $fields_to_update + [
'agent_user' => urlencode(trim($agent_user)),
'function' => 'update_fields'
];
];

return $this->callApiUrl($this->base_url,$options);
}

Expand Down

0 comments on commit a125199

Please sign in to comment.