HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

JSON API for some bank account

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
accountbanksomeforjsonapi

Problem

I have been developing this class, and was wondering if anyone had any thoughts on how I can improve the performance of it.

```
APIUsername = $APIUsername;
$this->APIPassword = $APIPassword;
} catch (Exception $e) {
die($e->getMessage());
}
}

/*
* Authenticate Account
*/
public function authenticate ($EmailAddress, $Password) {
try {
$data['EmailAddress'] = $EmailAddress;
$data['Password'] = $Password;

$data = $this->setJSON($data);

$result = $this->fetch('authenticate', $data);
} catch (Exception $e) {
$result = $e->getMessage();
}

return $result;
}

/*
* Get Contacts
*/
public function contacts ($EmailAddress, $Password, $PIN) {
try {
$data['EmailAddress'] = $EmailAddress;
$data['Password'] = $Password;
$data['PIN'] = $PIN;

$data = $this->setJSON($data);

$result = $this->fetch('contacts', $data);
} catch (Exception $e) {
$result = $e->getMessage();
}

return $result;
}

/*
* Get Transactions
*/
public function transactions ($EmailAddress, $Password, $PIN) {
try {
$data['EmailAddress'] = $EmailAddress;
$data['Password'] = $Password;
$data['PIN'] = $PIN;

$data = $this->setJSON($data);

$result = $this->fetch('transactions', $data);
} catch (Exception $e) {
$result = $e->getMessage();
}

return $result;
}

/*
* Validate PIN
*/
public function validatepin ($EmailAddress, $Password, $PIN) {
try {
$data['EmailAddress'] = $EmailAddress;
$data['Password'] = $Password;
$data['PIN'] = $PIN;

$data = $this->setJSON($data);

$result = $this->fetch('validatepin', $data);
} catch (Ex

Solution

Some things I noticed.

It seems like a lot/all of your public functions are simply doing the same thing, setting the functions params into an array, calling setJSON which simply appends the APIusername and APIpassword and then you fetch and catch any errors. You could have one method that does this and only methods that differ from this logic would do something else.

looks like you can just use PHP's compact() function for most of what you are doing.

For setJSON(), instead of looping through $data and remaking the same array, why not just add $data['APIUsername'] = $this->APIUsername; and $data['APIPassword'] = $this->APIPassword; and then return the json_encode($data)

For your fetch() method, you could get rid of the curl requirement and just use streams in PHP using a combination of stream_context_create() and file_get_contents().

I think it also might be useful to glance over this articles about Exceptions .

If you really are just passing along data with the username and password attached, you may just want to just use the magic __call method, that will check an array of acceptable API methods to call and then take the params, compact, add username and password, and then call your fetch method with this data and return. Example below.

 array('EmailAddress', 'Password'),
    );

    private $_api_username;
        private $_api_password;
    private $_api_url = 'someurl.com';

    public function __construct($username, $password)
    {
        if (!$username || !$password) {
            throw new APIException('You must provide API credentials');
        }

        $this->_api_username = $username;
        $this->_api_password = $password;
    }

    public function __call($method_name, $arguments)
    {
        if (!in_array($method_name, array_keys($this->_api_methods)) {
            throw new APIException("$method_name is not a valid API method");
        }

        $data = $this->setJSON($method_name, $arguments);
        return $this->fetch($method_name, $data);
    }

    protected function setJSON($method_name, $data)
    {
        if (empty($data)) {
            throw new APIException('No data provided');
        }

        $data = array_combine($this->_api_methods[$method_name], $data);

        $data['api_username'] = $this->_api_username;
        $data['api_password'] = $this->_api_password;

        return json_encode($data);
    }

    protected function fetch($method, $data)
    {
        $context = stream_context_create(array(
            'http' => array(
              'method'  => 'GET',
              'timeout' => 5,
            ),
        ));

        try {
            $ret = file_get_contents($this->_api_url . $method, false, $context); 
        } catch (APIException $e) {
                $ret = $e->getMessage();
        }

        return $ret;
    }
}

class APIException extends Exception {}

Code Snippets

<?php
class API {
    protected $_api_methods = array(
        'balance' => array('EmailAddress', 'Password'),
    );

    private $_api_username;
        private $_api_password;
    private $_api_url = 'someurl.com';

    public function __construct($username, $password)
    {
        if (!$username || !$password) {
            throw new APIException('You must provide API credentials');
        }

        $this->_api_username = $username;
        $this->_api_password = $password;
    }

    public function __call($method_name, $arguments)
    {
        if (!in_array($method_name, array_keys($this->_api_methods)) {
            throw new APIException("$method_name is not a valid API method");
        }

        $data = $this->setJSON($method_name, $arguments);
        return $this->fetch($method_name, $data);
    }

    protected function setJSON($method_name, $data)
    {
        if (empty($data)) {
            throw new APIException('No data provided');
        }

        $data = array_combine($this->_api_methods[$method_name], $data);

        $data['api_username'] = $this->_api_username;
        $data['api_password'] = $this->_api_password;

        return json_encode($data);
    }

    protected function fetch($method, $data)
    {
        $context = stream_context_create(array(
            'http' => array(
              'method'  => 'GET',
              'timeout' => 5,
            ),
        ));

        try {
            $ret = file_get_contents($this->_api_url . $method, false, $context); 
        } catch (APIException $e) {
                $ret = $e->getMessage();
        }

        return $ret;
    }
}

class APIException extends Exception {}

Context

StackExchange Code Review Q#427, answer score: 2

Revisions (0)

No revisions yet.