Skip to content

Commit

Permalink
Query params
Browse files Browse the repository at this point in the history
  • Loading branch information
LasseRafn committed Aug 22, 2017
1 parent f35eb44 commit 73ecfee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
12 changes: 10 additions & 2 deletions src/Utils/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,24 @@ public function all( array $fields = null )
* @param array|null $fields
* @param int $start
* @param int $limit
* @param array $parameters
*
* @return array
*/
public function get( array $fields = null, $start = 0, $limit = 100 )
public function get( array $fields = null, $start = 0, $limit = 100, $parameters = [] )
{
$models = [];

$query = '';

foreach ( $parameters as $q ) {
$attr = key( $q );
$value = urlencode( $q[ $attr ] );
$query .= "&{$attr}={$value}";
}

try {
$items = $this->request->get( $this->entity, null, $fields, $start, $limit );
$items = $this->request->get( $this->entity, null, $fields, $start, $limit, $query );

if ( is_array( $items ) ) {
foreach ( $items as $item ) {
Expand Down
54 changes: 19 additions & 35 deletions src/Utils/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function setApiToken( $token )

private function buildEntity( $entity, $id = null, $fields = null )
{
return ( $fields ) ? $entity . ':(' . implode( ',', $fields ) . ')' . ( $id ? '/' . $id : '' ) : $entity . ( $id ? '/' . $id : '' );
return $fields ? $entity . ':(' . implode( ',', $fields ) . ')' . ( $id ? '/' . $id : '' ) : $entity . ( $id ? '/' . $id : '' );
}

private function getData( $data )
Expand All @@ -43,55 +43,47 @@ private function getData( $data )

public function getSimple( string $entity )
{
try
{
try {
$url = config( 'pipedrive.endpoint' ) . $this->buildEntity( $entity ) . '?api_token=' . $this->api_token;
$response = $this->curl->get( $url );

return $this->getData( $response->getBody() );
} catch ( \Exception $exception )
{
} catch ( \Exception $exception ) {
throw new CurlError( $exception->getMessage(), $exception->getCode() );
}
}

public function get( string $entity, int $id = null, array $fields = null, int $start = 0, int $limit = 100 )
public function get( string $entity, int $id = null, array $fields = null, int $start = 0, int $limit = 100, $query = '' )
{
try
{
$url = config( 'pipedrive.endpoint' ) . $this->buildEntity( $entity, $id, $fields ) . '?api_token=' . $this->api_token . '&start=' . $start . '&limit=' . $limit;
try {
$url = config( 'pipedrive.endpoint' ) . $this->buildEntity( $entity, $id, $fields ) . '?api_token=' . $this->api_token . '&start=' . $start . '&limit=' . $limit . $query;
$response = $this->curl->get( $url );

return $this->getData( $response->getBody() );
} catch ( \Exception $exception )
{
} catch ( \Exception $exception ) {
throw new CurlError( $exception->getMessage(), $exception->getCode() );
}
}

public function getBy( string $entity, $attr, $value, int $start = 0, int $limit = 1 )
{
try
{
try {
$value = urlencode( $value );
$url = config( 'pipedrive.endpoint' ) . $this->buildEntity( $entity ) . '/find?api_token=' . $this->api_token . '&start=' . $start . '&limit=' . $limit . "&{$attr}={$value}";
$response = $this->curl->get( $url );

return $this->getData( $response->getBody() );
} catch ( \Exception $exception )
{
} catch ( \Exception $exception ) {
throw new CurlError( $exception->getMessage(), $exception->getCode() );
}
}

public function getByMany( string $entity, $queries, int $start = 0, int $limit = 1 )
{
try
{
try {
$query = '';

foreach ( $queries as $q )
{
foreach ( $queries as $q ) {
$attr = key( $q );
$value = urlencode( $q[ $attr ] );
$query .= "&{$attr}={$value}";
Expand All @@ -102,53 +94,46 @@ public function getByMany( string $entity, $queries, int $start = 0, int $limit
$response = $this->curl->get( $url );

return $this->getData( $response->getBody() );
} catch ( \Exception $exception )
{
} catch ( \Exception $exception ) {
throw new CurlError( $exception->getMessage(), $exception->getCode() );
}
}

public function post( string $entity, $data = [], $multipartFormData = null )
{
try
{
try {
$url = $this->buildEntity( $entity ) . '?api_token=' . $this->api_token;

$requestData = [
'api_token' => $this->api_token
];

if ( $multipartFormData )
{
if ( $multipartFormData ) {
$requestData['multipart'] = [];
$requestData['multipart'][] = $multipartFormData;

foreach ( $data as $key => $value )
{
foreach ( $data as $key => $value ) {
$requestData['multipart'][] = [
'name' => $key,
'contents' => $value
];
}
}
else
{
else {
$requestData['json'] = $data;
}

$response = $this->curl->post( $url, $requestData );

return $this->getData( $response->getBody() );
} catch ( \Exception $exception )
{
} catch ( \Exception $exception ) {
throw new CurlError( $exception->getMessage(), $exception->getCode() );
}
}

public function put( string $entity, $id, $data = [] )
{
try
{
try {
$url = $this->buildEntity( $entity, $id ) . '?api_token=' . $this->api_token;

$response = $this->curl->put( $url, [
Expand All @@ -157,8 +142,7 @@ public function put( string $entity, $id, $data = [] )
] );

return $this->getData( $response->getBody() );
} catch ( \Exception $exception )
{
} catch ( \Exception $exception ) {
throw new CurlError( $exception->getMessage(), $exception->getCode() );
}
}
Expand Down

0 comments on commit 73ecfee

Please sign in to comment.