#StackBounty: #magento-1.7 #database #api #rest #performance Creating an API to communicate directly with Magento DB

Bounty: 50

We have been using the Magento Rest API to communicate between external systems and Magento. Painfully, we have discovered that the REST API does not scale well. Specifically, often times requests time-out when requesting large amounts of data (say, about fifty products). We are using version 1.7.0.2.

One solution we are investigating is creating a custom API not written using Magento for the purpose of reading/writing into Magento.

  • Is this a solution typically used?
  • If so, any packages that simplify read/write operations from Magneto EAV tables?
  • If not, are there any ways to speed up the Magento REST API?

We are in the process of scaling and speed is very important to us.


Get this bounty!!!

#StackBounty: #java #json #rest #javafx #response The registered message body readers compatible with the MIME media type are: applicat…

Bounty: 50

I’m using Spring Rest API in server side and jersey API from client side.

I’m creating a screen where it will fetch last 5 customer redeem transaction.

From server side i’m returning list of RedeemTransactionDetails and accepting the same in client side.

I had debugged server side code it’s returns the valid list,
and in client side response code is 200 , whereas while getting
entity i’m getting error from client side.

Server side:

    @RestController
    @RequestMapping("/rest/api")
    public class CustomerRestController {
            @Autowired private CustomerService customerService;

            @RequestMapping(value="/redeemTransactionList/{clientId}/{mobileNumber}/{numOfTransaction}" , method=RequestMethod.POST , produces = "application/json; charset=UTF-8")
            public @ResponseBody List<RedeemTransactionDetails> redeemTransaction(@PathVariable(value = "clientId") int clientId, @PathVariable(value = "mobileNumber") String mobileNumber , @PathVariable(value="numOfTransaction") int numOfTransaction) {
            LOG.debug("We are in redeemTransaction method for user {} " , clientId);
            List<RedeemTransactionDetails> redeemList = null ;
            try {
                redeemList =  customerService.redeemTransactionList(clientId, mobileNumber,numOfTransaction);

            } catch (Exception e) {
                LOG.debug("Excption while fetching redeemTransaction ");
            }
            return  redeemList;
        }
    }

Client Side :

public List<RedeemTransactionDetails> getRedeemTransactions(String mobileNumber, String clientId, String numberOfTransaction) {
    log.debug("inside authenticate() ");
    List<RedeemTransactionDetails> result = null; 
    try{
        webResource = client.resource(uri + "/redeemTransactionList").path(clientId).path(mobileNumber).path(numberOfTransaction) ;
        ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class);
        if (response.getStatus() != 200) {
            log.debug("response.getStatus() : " +  response.getStatus() );  
            throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
         } 
        response.getType() ;
        result = (List<RedeemTransactionDetails>) response.getEntity(RedeemTransactionDetails.class);
        log.debug("user Details " + result);
    }
    catch(Exception e){
        log.debug(e);
    }
    return result ;
}
}

NOTE: I had used the following dependencies in pom xml file

<dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.19.3</version>
    </dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.6</version>
</dependency>

EDIT:

ERROR LOG

SEVERE: A message body reader for Java class com.prom.via.rest.dto.RedeemTransactionDetails, and Java type class com.prom.via.rest.dto.RedeemTransactionDetails, and MIME media type application/json;charset=UTF-8 was not found
Feb 23, 2017 4:52:17 PM com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: The registered message body readers compatible with the MIME media type are:
*/* ->
  com.sun.jersey.core.impl.provider.entity.FormProvider
  com.sun.jersey.core.impl.provider.entity.StringProvider
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.ReaderProvider
  com.sun.jersey.core.impl.provider.entity.DocumentProvider
  com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
  com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
  com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
  com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
  com.sun.jersey.core.impl.provider.entity.EntityHolderReader


Get this bounty!!!

#StackBounty: #php #rest #curl #phpunit Curl-based REST client library (round 2)

Bounty: 100

This is a second iteration on an earlier review – cURL based REST client library

I have done some refactoring to split out the REST HTTP response into it’s own class from what was in the previous review and implement.

You might find it useful to read the library’s README file before performing this review for further background and usage examples, which I have omitted here for brevity and to allow the question to focus on the code.

I am only reviewing the single REST call portion of the library here.

I have also opened a separate review to cover the curl_multi_* based functionality for performing multiple requests in parallel.

RestClient class

<?php

namespace MikeBrantRestClientLib;

/**
 * @desc Class for executing RESTful service calls using a fluent interface.
 */
class RestClient
{
    /**
      * Flag to determine if basic authentication is to be used.
     * 
     * @var boolean
     */
    protected $useBasicAuth = false;

    /**
     * User Name for HTTP Basic Auth
     * 
     * @var string
     */
    protected $basicAuthUsername = null;

    /**
     * Password for HTTP Basic Auth
     *
     * @var string
     */
    protected $basicAuthPassword = null;

    /**
     * Flag to determine if SSL is used
     * 
     * @var boolean
     */
    protected $useSsl = false;

    /**
     * Flag to determine is we are to run in test mode where host's SSL cert is not verified
     * 
     * @var boolean
     */
    protected $useSslTestMode = false;

    /**
     * Integer value representing number of seconds to set for curl timeout option. Defaults to 30 seconds.
     * 
     * @var integer
     */
    protected $timeout = 30;

    /**
     * Variable to store remote host name
     * 
     * @var string
     */
    protected $remoteHost = null;

    /**
     * Variable to hold setting to determine if redirects are followed
     * 
     * @var boolean
     */
    protected $followRedirects = false;

    /**
     * Variable to hold value for maximum number of redirects to follow for cases when redirect are being followed.
     * Default value of 0 will allow for following of unlimited redirects.
     * 
     * @var integer
     */
    protected $maxRedirects = 0;

    /**
     * Variable which can hold a URI base for all actions
     * 
     * @var string
     */
    protected $uriBase = '/';

    /**
     * Stores curl handle
     *
     * @var mixed
     */
    private $curl = null;

    /**
     * Variable to store request URL that is formed before a request is made
     * 
     * @var string
     */
    private $requestUrl = null;

    /**
     * Array containing headers to be used for request
     * 
     * @var array
     */
    private $headers = array();

    /**
     * Variable to store the request header as sent
     * 
     * @var string
     */

    /**
     * Variable to store CurlHttpResponse result from curl call
     * 
     * @var CurlHttpResponse
     */
    private $response = null;

    /**
     * Constructor method. Currently there is no instantiation logic.
     *
     * @return void
     */
    public function __construct() {}

    /**
     * Method to execute GET on server
     * 
     * @param string $action
     * @return CurlHttpResponse
     * @throws InvalidArgumentException
     * @throws Exception
     */
    public function get($action) {
        $this->validateAction($action);
        $this->curlSetup();
        $this->setRequestUrl($action);
        curl_setopt($this->curl, CURLOPT_HTTPGET, true);
        // execute call. Can throw Exception.
        $this->curlExec();

        return $this->response;
    }

    /**
     * Method to exexute POST on server
     * 
     * @param mixed $action
     * @param mixed $data
     * @return CurlHttpResponse
     * @throws InvalidArgumentException
     * @throws Exception
     */
    public function post($action, $data) {
        $this->validateAction($action);
        $this->validateData($data);
        $this->curlSetup();
        $this->setRequestUrl($action);
        $this->setRequestData($data);
        curl_setopt($this->curl, CURLOPT_POST, true);
        // execute call. Can throw Exception.
        $this->curlExec();

        return $this->response;
    }

    /**
     * Method to execute PUT on server
     * 
     * @param string $action
     * @param mixed $data
     * @return CurlHttpResponse
     * @throws InvalidArgumentException
     * @throws Exception
     */
    public function put($action, $data) {
        $this->validateAction($action);
        $this->validateData($data);
        $this->curlSetup();
        $this->setRequestUrl($action);
        $this->setRequestData($data);
        curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
        // execute call. Can throw Exception.
        $this->curlExec();

        return $this->response;
    }

    /**
     * Method to execute DELETE on server
     * 
     * @param string $action
     * @return CurlHttpResponse
     * @throws InvalidArgumentException
     * @throws Exception
     */
    public function delete($action) {
        $this->validateAction($action);
        $this->curlSetup();
        $this->setRequestUrl($action);
        curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
        // execute call. Can throw Exception.
        $this->curlExec();

        return $this->response;
    }

    /**
     * Method to execute HEAD on server
     * 
     * @param string $action
     * @return CurlHttpResponse
     * @throws InvalidArgumentException
     * @throws Exception
     */
    public function head($action) {
        $this->validateAction($action);
        $this->curlSetup();
        $this->setRequestUrl($action);
        curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
        curl_setopt($this->curl, CURLOPT_NOBODY, true);
        // execute call. Can throw Exception.
        $this->curlExec();

        return $this->response;
    }

    /**
     * Sets host name of remote server
     * 
     * @param string $host
     * @return RestClient
     * @throws InvalidArgumentException
     */
    public function setRemoteHost($host) {
        if(empty($host)) {
            throw new InvalidArgumentException('Host name not provided.');
        } else if(!is_string($host)) {
            throw new InvalidArgumentException('Non-string host name provided.');
        }

        // remove any http(s):// at beginning of host name
        $httpsPattern = '#https://#i';
        $httpPattern = '#http://#i';
        if (1 === preg_match($httpsPattern, $host)) {
            // this needs to be SSL request
            $this->setUseSsl(true);
            $host = str_ireplace('https://', '', $host);
        } else if (1 === preg_match($httpPattern, $host)) {
            $host = str_ireplace('http://', '', $host);
        }

        // remove trailing slash in host name
        $host = rtrim($host, '/');

        // look for common SSL port values in host name to see if SSL is needed
        $portPatterns = array(
            '/:443$/',
            '/:8443$/',
        );
        foreach ($portPatterns as $pattern) {
            if (1 === preg_match($pattern, $host)) {
                $this->setUseSsl(true);
            }
        }

        $this->remoteHost = $host;

        return $this;
    }

    /**
     * Sets URI base for the instance
     * 
     * @param string $uriBase
     * @return RestClient
     * @throws InvalidArgumentException
     */
    public function setUriBase($uriBase) {
        if(empty($uriBase)) {
            throw new InvalidArgumentException('URI base not provided.');
        } else if(!is_string($uriBase)) {
            throw new InvalidArgumentException('Non-string URI base provided.');
        }

        // make sure we always have forward slash at beginning and end of uriBase
        $uriBase = '/' . ltrim($uriBase, '/');
        $uriBase = rtrim($uriBase, '/') . '/';
        $this->uriBase = $uriBase;

        return $this;
    }

    /**
     * Sets whether SSL is to be used
     * 
     * @param boolean $value
     * @return RestClient
     * @throws InvalidArgumentException
     */
    public function setUseSsl($value) {
        if (!is_bool($value)) {
            throw new InvalidArgumentException('Non-boolean value passed as parameter.');
        }
        $this->useSsl = $value;

        return $this;
    }

    /**
     * Sets whether SSL Test Mode is to be used
     * 
     * @param boolean $value
     * @return RestClient
     * @throws InvalidArgumentException
     */
    public function setUseSslTestMode($value) {
        if (!is_bool($value)) {
            throw new InvalidArgumentException('Non-boolean value passed as parameter.');
        }
        $this->useSslTestMode = $value;

        return $this;
    }
    /**
     * Sets basic authentication credentials
     * 
     * @param string $user
     * @param string $password
     * @return RestClient
     * @throws InvalidArgumentException
     */
    public function setBasicAuthCredentials($user, $password) {
        if (empty($user)) {
            throw new InvalidArgumentException('User name not provided when trying to set basic authentication credentials.');
        }
        if (empty($password)) {
            throw new InvalidArgumentException('Password not provided when trying to set basic authentication credentials.');
        }

        $this->useBasicAuth = true;
        $this->basicAuthUsername = $user;
        $this->basicAuthPassword = $password;

        return $this;
    }

    /**
     * Sets HTTP headers from an associative array where key is header name and value is the header value
     * 
     * @param array $headers
     * @return RestClient
     */
    public function setHeaders(array $headers) {
        if(empty($headers)) {
            throw new InvalidArgumentException('Empty array passed when triyng to set headers');
        }
        $this->headers = $headers;

        return $this;  
    }

    /**
     * Sets maximum timeout for curl requests
     * 
     * @param integer $seconds
     * @return RestClient
     * @throws InvalidArgumentException
     */
    public function setTimeout($seconds) {
        if(!is_integer($seconds) || $seconds < 0) {
            throw new InvalidArgumentException('A non-negative integer value must be passed when trying to set timeout');
        }
        $this->timeout = $seconds;

        return $this;
    }

    /**
     * Sets flag on whether to follow 3XX redirects.
     * 
     * @param boolean $follow
     * @return RestClient
     * @throws InvalidArgumentException
     */
    public function setFollowRedirects($follow) {
        if(!is_bool($follow)) {
            throw new InvalidArgumentException('Non-boolean value passed as parameter.');
        }
        $this->followRedirects = $follow;

        return $this;
    }

    /**
     * Sets maximum number of redirects to follow. A value of 0 represents no redirect limit. Also sets followRedirects property to true .
     * 
     * @param integer $redirects
     * @return RestClient
     * @throws InvalidArgumentException
     */
    public function setMaxRedirects($redirects) {
        if(!is_integer($redirects) || $redirects < 0) {
            throw new InvalidArgumentException('A non-negative integer value must be passed when trying to set max redirects.');
        }
        $this->maxRedirects = $redirects;
        $this->setFollowRedirects(true);

        return $this;
    }

    /**
     * Get remote host setting
     * 
     * @return string
     */
    public function getRemoteHost() {
        return $this->remoteHost;
    }

    /**
     * Get URI Base setting
     * 
     * @return string
     */
    public function getUriBase() {
        return $this->uriBase;
    }

    /**
     * Get boolean setting indicating whether SSL is to be used
     * 
     * @return boolean
     */
    public function isUsingSsl() {
        return $this->useSsl;
    }

    /**
     * Get boolean setting indicating whether SSL test mode is enabled
     * 
     * @return boolean
     */
    public function isUsingSslTestMode() {
        return $this->useSslTestMode;
    }

    /**
     * Get timeout setting
     * 
     * @return integer
     */
    public function getTimeout() {
        return $this->timeout;
    }

    /**
     * Get follow redirects setting
     * 
     * @return boolean
     */
    public function isFollowingRedirects() {
        return $this->followRedirects;
    }

    /**
     * Get max redirects setting
     * 
     * @return integer
     */
    public function getMaxRedirects() {
        return $this->maxRedirects;
    }

    /**
     * Method to initialize curl handle in object
     * 
     * @return void
     * @throws Exception
     */
    private function curlSetup() {        
        // reset all request/response properties
        $this->resetRequestResponseProperties();

        // initialize curl. Throws Exception on failure.
        $this->curl = $this->curlInit();
    }

    /**
     * Method to initilize a curl handle
     * 
     * @return resource
     * @throws Exception
     */
    protected function curlInit() {
        // initialize curl
        $curl = curl_init();
        if($curl === false) {
            throw new Exception('curl failed to initialize.');
        }
        // set timeout
        curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);

        // set basic HTTP authentication settings
        if (true === $this->useBasicAuth) {
            curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($curl, CURLOPT_USERPWD, $this->basicAuthUsername . ':' . $this->basicAuthPassword);
        }

        // set headers
        if (!empty($this->headers)) {
            $headers = array();
            foreach ($this->headers as $key=>$val) {
                $headers[] = $key . ': ' . $val;
            }
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }

        // if not in production environment, we want to ignore SSL validation
        if (true === $this->useSsl && true === $this->useSslTestMode) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }

        // set option to add request header information to curl_getinfo output
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);

        // set option to return content body
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        // set redirect options
        if (true === $this->followRedirects) {
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
            if ($this->maxRedirects > 0) {
                curl_setopt($curl, CURLOPT_MAXREDIRS, $this->maxRedirects);
            }
        }

        return $curl;
    }

    /**
     * Method to to teardown curl fixtures at end of request
     * 
     * @return void
     */
    private function curlTeardown() {
        $this->curlClose($this->curl);
        $this->curl = null;
    }

    /**
     * Method to close curl handle
     * 
     * @return void
     */
    protected function curlClose($curl) {
        curl_close($curl);
    }

    /**
     * Method to execute curl call
     * 
     * @return void
     * @throws Exception
     */
    private function curlExec() {
        $curlResult = curl_exec($this->curl);
        if($curlResult === false) {
            // our curl call failed for some reason
            $curlError = curl_error($this->curl);
            $this->curlTeardown();
            throw new Exception('curl call failed with message: "' . $curlError. '"');
        }

        // return CurlHttpResponse
        try {
            $this->response = new CurlHttpResponse($curlResult, curl_getinfo($this->curl));
        } catch (InvalidArgumentException $e) {
            throw new Exception(
                'Unable to instantiate CurlHttpResponse. Message: "' . $e->getMessage() . '"',
                $e->getCode(),
                $e
            );
        } finally {
            $this->curlTeardown();
        }
    }

    /**
     * Method to reset all properties specific to a particular request/response sequence.
     * 
     * @return void
     */
    protected function resetRequestResponseProperties() {
        $this->requestUrl = null;
        $this->response = null;
    }

    /**
     * Method to set the url on curl handle based on passed action
     * 
     * @param string $action
     * @return void
     */
    protected function setRequestUrl($action) {
        $url = $this->buildUrl($action);
        $this->requestUrl = $url;
        curl_setopt($this->curl, CURLOPT_URL, $url);
    }

    /**
     * Method to build URL based on class settings and passed action
     * 
     * @param string $action
     * @return string
     */
    protected function buildUrl($action) {
        $url = 'http://';
        if (true === $this->useSsl) {
            $url = 'https://';
        }
        $url = $url . $this->remoteHost . $this->uriBase . $action;
        return $url;
    }

    /**
     * Method to set data to be sent along with POST/PUT requests
     * 
     * @param mixed $data
     * @return void
     */
    protected function setRequestData($data) {
        $this->requestData = $data;
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
    }

    /**
     * Method to provide common validation for action parameters
     * 
     * @param string $action
     * @return void
     * @throws InvalidArgumentException
     */
    protected function validateAction($action) {
        if(!is_string($action)) {
            throw new InvalidArgumentException('A non-string value was passed for action parameter');
        }
    }

    /**
     * Method to provide common validation for data parameters
     * 
     * @param mixed $data
     * @return void
     * @throws InvalidArgumentException
     */
    protected function validateData($data) {
        if(empty($data)) {
            throw new InvalidArgumentException('An empty value was passed for data parameter');
        }
    }
}

RestClient Unit Tests

<?php

namespace MikeBrantRestClientLib;

use PHPUnitFrameworkTestCase;

/**
 * Mock for curl_init global function
 * 
 * @return mixed
 */
function curl_init() {
    if (!is_null(RestClientTest::$curlInitResponse)) {
        return RestClientTest::$curlInitResponse;
    }
    return curl_init();
}

/**
 * Mock for curl_exec global function
 * 
 * @param resource curl handle
 * @return mixed
 */
function curl_exec($curl) {
    if (!is_null(RestClientTest::$curlExecResponse)) {
        return RestClientTest::$curlExecResponse;
    }
    return curl_exec($curl);
}

/**
 * Mock for curl_error global function
 * 
 * @param resource curl handle
 * @return mixed
 */
function curl_error($curl) {
    if (!is_null(RestClientTest::$curlErrorResponse)) {
        return RestClientTest::$curlErrorResponse;
    }
    return curl_error($curl);
}

/**
 * This is hacky workaround for avoiding double definition of this global method override
 * when running full test suite on this library.
 */
if(!function_exists('MikeBrantRestClientLibcurl_getinfo')) {

    /**
     * Mock for curl_getinfo function
     * 
     * @param resource curl handle
     * @return mixed
     */
    function curl_getinfo($curl) {
        $backtrace = debug_backtrace();
        $testClass = $backtrace[1]['class'] . 'Test';
        if (!is_null($testClass::$curlGetinfoResponse)) {
            return $testClass::$curlGetinfoResponse;
        }
        return curl_getinfo($curl);
    }
}

class RestClientTest extends TestCase
{
    public static $curlInitResponse = null;

    public static $curlExecResponse = null;

    public static $curlErrorResponse = null;

    public static $curlGetinfoResponse = null;

    protected $client = null;

    protected $curlExecMockResponse = 'Test Response';

    protected $curlGetinfoMockResponse = array(
        'url' => 'http://google.com/',
        'content_type' => 'text/html; charset=UTF-8',
        'http_code' => 200,
        'header_size' => 321,
        'request_size' => 49,
        'filetime' => -1,
        'ssl_verify_result' => 0,
        'redirect_count' => 0,
        'total_time' => 1.123264,
        'namelookup_time' => 1.045272,
        'connect_time' => 1.070183,
        'pretransfer_time' => 1.071139,
        'size_upload' => 0,
        'size_download' => 219,
        'speed_download' => 194,
        'speed_upload' => 0,
        'download_content_length' => 219,
        'upload_content_length' => -1,
        'starttransfer_time' => 1.122377,
        'redirect_time' => 0,
        'redirect_url' => 'http://www.google.com/',
        'primary_ip' => '216.58.194.142',
        'certinfo' => array(),
        'primary_port' => 80,
        'local_ip' => '192.168.1.74',
        'local_port' => 59733,
        'request_header' => "GET / HTTP/1.1nHost: google.comnAccept: */*",
    );

    protected function setUp() {
        self::$curlInitResponse = null;
        self::$curlExecResponse = null;
        self::$curlErrorResponse = null;
        self::$curlGetinfoResponse = null;
        $this->client = new RestClient();
    }

    protected function tearDown() {
        $this->client = null;
    }

    public function notStringProvider() {
        return array(
            array(null),
            array(new stdClass()),
            array(1),
            array(0),
            array(true),
            array(false),
            array(array())
        );
    }

    public function emptyProvider() {
        return array(
            array(null),
            array(''),
            array(0),
            array(0.0),
            array(false),
            array('0'),
            array(array())
        );
    }

    public function notStringAndEmptyProvider() {
        return array(
            array(null),
            array(''),
            array(new stdClass()),
            array(1),
            array(0),
            array(0.0),
            array('0'),
            array(true),
            array(false),
            array(array())
        );
    }

    public function hostProvider() {
        return array(
            array('somedomain.com', 'somedomain.com', false),
            array('somedomain.com/', 'somedomain.com', false),
            array('https://somedomain.com', 'somedomain.com', true),
            array('http://somedomain.com', 'somedomain.com', false),
            array('somedomain.com:80', 'somedomain.com:80', false),
            array('somedomain.com:443', 'somedomain.com:443', true),
            array('somedomain.com:8443', 'somedomain.com:8443', true)
        );
    }

    public function notBooleanProvider() {
        return array(
            array(null),
            array(''),
            array('string'),
            array('true'),
            array('false'),
            array(1),
            array(0),
            array('1'),
            array('0'),
            array(0.0),
            array(new stdClass()),
            array(array())
        );
    }

    public function uriBaseProvider() {
        return array(
            array('test', '/test/'),
            array('/test', '/test/'),
            array('test/', '/test/'),
            array('/test/', '/test/')
        );
    }

    public function notZeroOrPositiveIntegerProvider() {
        return array(
            array(-1),
            array(null),
            array(''),
            array(new stdClass()),
            array(1.0),
            array('1'),
            array(array())
        );
    }

    public function headersProvider() {
        return array(
            array(
                array(
                    'header1' => 'header1 value',
                    'header2' => 'header2 value'
                )
            )
        );
    }

    public function curlExecExceptionProvider() {
        return array(
            array(false, $this->curlGetinfoMockResponse),
            array('test', array())
        );
    }

    public function buildUrlProvider() {
        return array(
            array(true, 'google.com', 'base', 'action', 'https://google.com/base/action'),
            array(false, 'google.com', 'base', 'action', 'http://google.com/base/action')
        );
    }

    /**
     * @dataProvider notStringProvider
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibRestClient::validateAction
     */
    public function testValidateActionThrowsExceptions($action) {
        $this->client->get($action);
    }

    /**
     * @dataProvider emptyProvider
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibRestClient::validateData
     */
    public function testValidateDataThrowsExceptions($data) {
        $this->client->post('', $data);
    }

    /**
     * @dataProvider notStringAndEmptyProvider
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibRestClient::setRemoteHost
     */
    public function testSetRemoteHostThrowsExceptions($host) {
        $this->client->setRemoteHost($host);
    }

    /**
     * @dataProvider hostProvider
     * @covers MikeBrantRestClientLibRestClient::setRemoteHost
     * @covers MikeBrantRestClientLibRestClient::getRemoteHost
     */
    public function testSetRemoteHost($hostInput, $hostOutput, $useSslSet) {
        $this->client->setRemoteHost($hostInput);
        $this->assertEquals($hostOutput, $this->client->getRemoteHost());
        $this->assertEquals($useSslSet, $this->client->isUsingSsl());
    }

    /**
     * @dataProvider notStringAndEmptyProvider
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibRestClient::setUriBase
     */
    public function testSetUriBaseThrowsExceptions($string) {
        $this->client->setUriBase($string);
    }

    /**
     * @dataProvider uriBaseProvider
     * @covers MikeBrantRestClientLibRestClient::setUriBase
     * @covers MikeBrantRestClientLibRestClient::getUriBase
     */
    public function testSetUriBase($stringInput, $stringOutput) {
        $this->client->setUriBase($stringInput);
        $this->assertEquals($stringOutput, $this->client->getUriBase());
    }

    /**
     * @dataProvider notBooleanProvider
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibRestClient::setUseSsl
     */
    public function testSetUseSslThrowsExceptions($boolean) {
        $this->client->setUseSsl($boolean);
    }

    /**
     * @covers MikeBrantRestClientLibRestClient::setUseSsl
     * @covers MikeBrantRestClientLibRestClient::isUsingSsl
     */
    public function testSetUseSsl() {
        $this->client->setUseSsl(true);
        $this->assertTrue($this->client->isUsingSsl());
        $this->client->setUseSsl(false);
        $this->assertFalse($this->client->isUsingSsl());
    }

    /**
     * @dataProvider notBooleanProvider
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibRestClient::setUseSslTestMode
     */
    public function testSetUseSslTestModeThrowsExceptions($boolean) {
        $this->client->setUseSslTestMode($boolean);
    }

    /**
     * @covers MikeBrantRestClientLibRestClient::setUseSslTestMode
     * @covers MikeBrantRestClientLibRestClient::isUsingSslTestMode
     */
    public function testSetUseSslTestMode() {
        $this->client->setUseSslTestMode(true);
        $this->assertTrue($this->client->isUsingSslTestMode());
        $this->client->setUseSslTestMode(false);
        $this->assertFalse($this->client->isUsingSslTestMode());
    }

    /**
     * @dataProvider emptyProvider
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibRestClient::setBasicAuthCredentials
     */
    public function testSetBasicAuthCredentialsThrowsExceptionOnEmptyUser($user) {
        $this->client->setBasicAuthCredentials($user, 'password');
    }

    /**
     * @dataProvider emptyProvider
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibRestClient::setBasicAuthCredentials
     */
    public function testSetBasicAuthCredentialsThrowsExceptionOnEmptyPassword($password) {
        $this->client->setBasicAuthCredentials('user', $password);
    }

    /**
     * @covers MikeBrantRestClientLibRestClient::setBasicAuthCredentials
     */
    public function testSetBasicAuthCredentials() {
        $this->client->setBasicAuthCredentials('user', 'password');
        $this->assertAttributeEquals('user', 'basicAuthUsername', $this->client);
        $this->assertAttributeEquals('password', 'basicAuthPassword', $this->client);
        $this->assertAttributeEquals(true, 'useBasicAuth', $this->client);
    }

    /**
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibRestClient::setHeaders
     */
    public function testSetHeadersThrowsExceptionOnEmptyArray() {
        $this->client->setHeaders(array());
    }

    /**
     * @dataProvider headersProvider
     * @covers MikeBrantRestClientLibRestClient::setHeaders
     */
    public function testSetHeaders($headers) {
        $this->client->setHeaders($headers);
        $this->assertAttributeEquals($headers, 'headers', $this->client);
    }

    /**
     * @dataProvider notZeroOrPositiveIntegerProvider
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibRestClient::setTimeout
     */
    public function testSetTimeoutThrowsExceptions($int) {
        $this->client->setTimeout($int);
    }

    /**
     * @covers MikeBrantRestClientLibRestClient::setTimeout
     * @covers MikeBrantRestClientLibRestClient::getTimeout
     */
    public function testSetTimeout() {
        $this->client->setTimeout(30);
        $this->assertEquals(30, $this->client->getTimeout());
        $this->client->setTimeout(0);
        $this->assertEquals(0, $this->client->getTimeout());
    }

    /**
     * @dataProvider notBooleanProvider
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibRestClient::setFollowRedirects
     */
    public function testSetFollowRedirectsThrowsExceptions($boolean) {
        $this->client->setFollowRedirects($boolean);
    }

    /**
     * @covers MikeBrantRestClientLibRestClient::setFollowRedirects
     * @covers MikeBrantRestClientLibRestClient::isFollowingRedirects
     */
    public function testSetFollowRedirects() {
        $this->client->setFollowRedirects(true);
        $this->assertTrue($this->client->isFollowingRedirects());
        $this->client->setFollowRedirects(false);
        $this->assertFalse($this->client->isFollowingRedirects());
    }

    /**
     * @dataProvider notZeroOrPositiveIntegerProvider
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibRestClient::setMaxRedirects
     */
    public function testSetMaxRedirectsThrowsExceptions($int) {
        $this->client->setMaxRedirects($int);
    }

    /**
     * @covers MikeBrantRestClientLibRestClient::setMaxRedirects
     * @covers MikeBrantRestClientLibRestClient::getMaxRedirects
     */
    public function testSetMaxRedirects() {
        $this->client->setMaxRedirects(1);
        $this->assertEquals(1, $this->client->getMaxRedirects());
        $this->assertTrue($this->client->isFollowingRedirects());
        $this->client->setMaxRedirects(0);
        $this->assertEquals(0, $this->client->getMaxRedirects());
        $this->assertTrue($this->client->isFollowingRedirects());
    }

    /**
     * @expectedException Exception
     * @covers MikeBrantRestClientLibRestClient::curlInit
     */
    public function testCurlInitThrowsException() {
        self::$curlInitResponse = false;
        $this->client->get('action');
    }

    /**
     * @dataProvider curlExecExceptionProvider
     * @expectedException Exception
     * @covers MikeBrantRestClientLibRestClient::curlExec
     */
    public function testCurlExecThrowsException($response, $getinfo) {
        self::$curlExecResponse = $response;
        self::$curlErrorResponse = 'test error';
        self::$curlGetinfoResponse = $getinfo;
        $this->client->get('action');
    }

    /**
     * @dataProvider buildUrlProvider
     * @covers MikeBrantRestClientLibRestClient::get
     * @covers MikeBrantRestClientLibRestClient::validateAction
     * @covers MikeBrantRestClientLibRestClient::buildUrl
     * @covers MikeBrantRestClientLibRestClient::curlSetup
     * @covers MikeBrantRestClientLibRestClient::resetRequestResponseProperties
     * @covers MikeBrantRestClientLibRestClient::curlInit
     * @covers MikeBrantRestClientLibRestClient::setRequestUrl
     * @covers MikeBrantRestClientLibRestClient::curlExec
     * @covers MikeBrantRestClientLibRestClient::curlTeardown
     * @covers MikeBrantRestClientLibRestClient::curlClose
     */
    public function testGet($useSsl, $host, $uriBase, $action, $expectedUrl) {
        self::$curlExecResponse = $this->curlExecMockResponse;
        self::$curlGetinfoResponse = $this->curlGetinfoMockResponse;
        $this->client->setBasicAuthCredentials('user', 'password')
                     ->setHeaders(array('header' => 'header value'))
                     ->setUseSsl($useSsl)
                     ->setUseSslTestMode(true)
                     ->setFollowRedirects(true)
                     ->setMaxRedirects(1)
                     ->setremoteHost($host)
                     ->setUriBase($uriBase);
        $response = $this->client->get($action);
        $this->assertInstanceOf(CurlHttpResponse::class, $response);
        $this->assertAttributeEquals(null, 'curl', $this->client);
    }

    /**
     * @covers MikeBrantRestClientLibRestClient::post
     * @covers MikeBrantRestClientLibRestClient::validateData
     * @covers MikeBrantRestClientLibRestClient::setRequestData
     */
    public function testPost() {
        self::$curlExecResponse = $this->curlExecMockResponse;
        self::$curlGetinfoResponse = $this->curlGetinfoMockResponse;
        $response = $this->client->post('', 'test post data');
        $this->assertInstanceOf(CurlHttpResponse::class, $response);
        $this->assertAttributeEquals(null, 'curl', $this->client);
   }

    /**
     * @covers MikeBrantRestClientLibRestClient::put
     */
    public function testPut() {
        self::$curlExecResponse = $this->curlExecMockResponse;
        self::$curlGetinfoResponse = $this->curlGetinfoMockResponse;
        $response = $this->client->put('', 'test put data');
        $this->assertInstanceOf(CurlHttpResponse::class, $response);
        $this->assertAttributeEquals(null, 'curl', $this->client);
    }

    /**
     * @covers MikeBrantRestClientLibRestClient::delete
     */
    public function testDelete() {
        self::$curlExecResponse = $this->curlExecMockResponse;
        self::$curlGetinfoResponse = $this->curlGetinfoMockResponse;
        $response = $this->client->delete('');
        $this->assertInstanceOf(CurlHttpResponse::class, $response);
        $this->assertAttributeEquals(null, 'curl', $this->client);
    }

    /**
     * @covers MikeBrantRestClientLibRestClient::head
     */
    public function testHead() {
        self::$curlExecResponse = '';
        self::$curlGetinfoResponse = $this->curlGetinfoMockResponse;
        $response = $this->client->head('');
        $this->assertInstanceOf(CurlHttpResponse::class, $response);
        $this->assertAttributeEquals(null, 'curl', $this->client);
    }
}

CurlHttpResponse class

<?php

namespace MikeBrantRestClientLib;

/**
 * @desc Class representing HTTP response as returned from curl call.
 */
class CurlHttpResponse
{
    /**
     * Variable to store response body
     */
    protected $body = null;

    /**
     * Variable to store HTTP repsonse code
     * 
     * @var integer
     */
    protected $httpCode = null;

    /**
     * Variable to store response content type header
     * 
     * @var string
     */
    protected $contentType = null;

    /**
     * Variable to store URL used in request as reported via curl_getinfo().
     * 
     * @var string
     */
    protected $requestUrl = null;

    /**
     * Variable to store header used in request as reported via curl_getinfo().
     * 
     * @var string
     */
    protected $requestHeader = null;

    /**
     * Variable to store curl getinfo array.
     * See documentation at http://php.net/manual/en/function.curl-getinfo.php for expected array format.
     * 
     * @var array
     */
    protected $curlGetinfo = null;

    /**
     * Constructor method.
     * 
     * @param mixed $responseBody Response body as returned from a curl request.
     * @param array $curlGetinto Array returned form curl_getinfo() function call for request.
     * @return void
     * @throws InvalidArgumentException
     */
    public function __construct($responseBody, array $curlGetinfo) {
        $this->validateGetinfoArray($curlGetinfo);
        $this->body = $responseBody;
        $this->httpCode = $curlGetinfo['http_code'];
        $this->contentType = $curlGetinfo['content_type'];
        $this->requestUrl = $curlGetinfo['url'];
        $this->requestHeader = $curlGetinfo['request_header'];
        $this->curlGetinfo = $curlGetinfo;
    }

    /**
     * Returns response body for request
     * 
     * @return mixed
     */
    public function getBody() {
        return $this->body;
    }

    /**
     * Returns HTTP response code for request
     * 
     * @return integer
     */
    public function getHttpCode() {
        return $this->httpCode;
    }

    /**
     * Returns URL used in request as reported via curl_getinfo().
     * 
     * @return string
     */
    public function getRequestUrl() {
        return $this->requestUrl;
    }

    /**
     * Returns header used in request as reported via curl_getinfo().
     * 
     * @return string
     */
    public function getRequestHeader() {
        return $this->requestHeader;
    }

    /**
     * Returns curl getinfo array.
     * See documentation at http://php.net/manual/en/function.curl-getinfo.php for expected array format.
     * 
     * @return array
     */
    public function getCurlGetinfo() {
        return $this->curlGetinfo;
    }

    /**
     * Method to perform minimal validation of input array as having keys expected to be returned from
     * curl_getinfo().
     * 
     * @throws InvalidArgumentException
     */
    protected function validateGetinfoArray(array $getinfo) {
        if(empty($getinfo)) {
            throw new InvalidArgumentException('Empty array passed. Valid curl_getinfo() result array expected.');
        }
        if(!isset($getinfo['http_code']) || !is_integer($getinfo['http_code'])) {
            throw new InvalidArgumentException('curl_getinfo() response array expects integer value at http_code key.');
        }
        if(!isset($getinfo['content_type']) || !is_string($getinfo['content_type'])) {
            throw new InvalidArgumentException('curl_getinfo() response array expects string value at content_type key.');
        }
        if(!isset($getinfo['url']) || !is_string($getinfo['url'])) {
            throw new InvalidArgumentException('curl_getinfo() response array expects string value at url key.');
        }
        if(!isset($getinfo['request_header']) || !is_string($getinfo['request_header'])) {
            throw new InvalidArgumentException('curl_getinfo() response array expects string value at request_header key.');
        }
    }
}

CurlHttpResponse Unit Tests

<?php

namespace MikeBrantRestClientLib;

use PHPUnitFrameworkTestCase;

class CurlHttpResponseTest extends TestCase
{
    protected $curlExecMockResponse = 'Test Response';

    protected $curlGetinfoMockResponse = array(
        'url' => 'http://google.com/',
        'content_type' => 'text/html; charset=UTF-8',
        'http_code' => 200,
        'header_size' => 321,
        'request_size' => 49,
        'filetime' => -1,
        'ssl_verify_result' => 0,
        'redirect_count' => 0,
        'total_time' => 1.123264,
        'namelookup_time' => 1.045272,
        'connect_time' => 1.070183,
        'pretransfer_time' => 1.071139,
        'size_upload' => 0,
        'size_download' => 219,
        'speed_download' => 194,
        'speed_upload' => 0,
        'download_content_length' => 219,
        'upload_content_length' => -1,
        'starttransfer_time' => 1.122377,
        'redirect_time' => 0,
        'redirect_url' => 'http://www.google.com/',
        'primary_ip' => '216.58.194.142',
        'certinfo' => array(),
        'primary_port' => 80,
        'local_ip' => '192.168.1.74',
        'local_port' => 59733,
        'request_header' => "GET / HTTP/1.1nHost: google.comnAccept: */*",
    );

    public function invalidGetinfoProvider() {
        return array(
            array(
                array()
            ),
            array(
                array('no keys')
            ),
            array(
                array(
                    'http_code' => 'not integer'
                )
            ),
            array(
                array(
                    'http_code' => 200
                )
            ),
            array(
                array(
                   'http_code' => 200,
                   'content_type' => false
                )
            ),
            array(
                array(
                   'http_code' => 200,
                   'content_type' => 'text/html'
                )
            ),
            array(
                array(
                   'http_code' => 200,
                   'content_type' => 'text/html',
                   'url' => false
                )
            ),
            array(
                array(
                   'http_code' => 200,
                   'content_type' => 'text/html',
                   'url' => 'htttp://somedomain.com'
                )
            ),
            array(
                array(
                   'http_code' => 200,
                   'content_type' => 'text/html',
                   'url' => 'htttp://somedomain.com',
                   'request_header' => false
                )
            )
        );
    }

    /**
     * @dataProvider invalidGetinfoProvider
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibCurlHttpResponse::validateGetinfoArray
     */
    public function testValidateGetinfoArrayThrowsExceptions($getinfo) {
        $response = new CurlHttpResponse('test', $getinfo);
    }

    /**
     * @covers MikeBrantRestClientLibCurlHttpResponse::__construct
     * @covers MikeBrantRestClientLibCurlHttpResponse::validateGetinfoArray
     * @covers MikeBrantRestClientLibCurlHttpResponse::getBody
     * @covers MikeBrantRestClientLibCurlHttpResponse::getHttpCode
     * @covers MikeBrantRestClientLibCurlHttpResponse::getRequestUrl
     * @covers MikeBrantRestClientLibCurlHttpResponse::getRequestHeader
     * @covers MikeBrantRestClientLibCurlHttpResponse::getCurlGetinfo
     */
    public function testConstructor() {
        $response = new CurlHttpResponse($this->curlExecMockResponse, $this->curlGetinfoMockResponse);
        $this->assertEquals($this->curlExecMockResponse, $response->getBody());
        $this->assertEquals($this->curlGetinfoMockResponse['http_code'], $response->getHttpCode());
        $this->assertEquals($this->curlGetinfoMockResponse['url'], $response->getRequestUrl());
        $this->assertEquals($this->curlGetinfoMockResponse['request_header'], $response->getRequestHeader());
        $this->assertEquals($this->curlGetinfoMockResponse, $response->getCurlGetinfo());
    }
}


Get this bounty!!!

#StackBounty: #php #rest #curl #phpunit Curl-based REST Client Library (round 3)

Bounty: 100

This code review request relates to this code review which covers the basic single REST call use case in this this REST client library.

This code review covers the classes and unit tests for the multiple parallel REST call functionality provided by the library, which leverage the PHP cURL extension’s curl_multi_* functionality.

You might find it useful to read the library’s README file before performing this review for further background and usage examples, which I have omitted here for brevity and to allow the question to focus on the code.

RestMultiClient class

<?php

namespace MikeBrantRestClientLib;

/**
* @desc Class which extendd RestClient to provide curl_multi capabilities, allowing for multiple REST calls to be made in parallel.
*/
class RestMultiClient extends RestClient
{
    /**
     * Store array of curl handles for multi_exec
     * 
     * @var array
     */
    private $curlHandles = array();

    /**
     * Stores curl multi handle to which individual handles in curlHandles are added
     * 
     * @var mixed
     */
    private $curlMultiHandle = null;

    /**
     * Variable to store the maximum number of handles to be used for curl_multi_exec
     * 
     * @var integer
     */
    private $maxHandles = 10;

    /**
     * Variable to store an array of request headers sent in a multi_exec request
     * 
     * @var array
     */
    private $requestHeaders = array();

    /**
     * Variable to store an array of request data sent for multi_exec POST/PUT requests.
     * 
     * @var array
     */
    private $requestDataArray = array();

    /**
     * Variable to store CurlMultiHttpResponse object
     * 
     * @var CurlMultiHttpResponse
     */
    private $curlMultiHttpResponse = null;

    /**
     * Constructor method. Currently there is no instantiation logic.
     * 
     * @return void
     */
    public function __construct() {}

    /**
     * Method to perform multiple GET actions using curl_multi_exec.
     * 
     * @param array $actions
     * @param integer $maxHandles
     * @return RestMultiClient
     * @throws Exception
     * @throws InvalidArgumentException
     * @throws LengthException
     */
    public function get($actions) {
        $this->validateActionArray($actions);

        // set up curl handles
        $this->curlMultiSetup(count($actions));
        $this->setRequestUrls($actions);
        foreach($this->curlHandles as $curl) {
            curl_setopt($curl, CURLOPT_HTTPGET, true); // explicitly set the method to GET    
        }
        $this->curlMultiExec();

        return $this->curlMultiHttpResponse;
    }

    /**
     * Method to perform multiple POST actions using curl_multi_exec.
     * 
     * @param array $actions
     * @param array $data
     * @return RestMultiClient
     * @throws Exception
     * @throws InvalidArgumentException
     * @throws LengthException
     */
    public function post($actions, $data) {
        $this->validateActionArray($actions);
        $this->validateDataArray($data);
        // verify that the number of data elements matches the number of action elements
        if (count($actions) !== count($data)) {
            throw new LengthException('The number of actions requested does not match the number of data elements provided.'); 
        }

        // set up curl handles
        $this->curlMultiSetup(count($actions));
        $this->setRequestUrls($actions);
        $this->setRequestDataArray($data);
        foreach($this->curlHandles as $curl) {
            curl_setopt($curl, CURLOPT_POST, true); // explicitly set the method to POST 
        }
        $this->curlMultiExec();

        return $this->curlMultiHttpResponse;
    }

    /**
     * Method to perform multiple PUT actions using curl_multi_exec.
     * 
     * @param array $actions
     * @param array $data
     * @return RestMultiClient
     * @throws Exception
     * @throws InvalidArgumentException
     * @throws LengthException
     */
    public function put($actions, $data) {
        $this->validateActionArray($actions);
        $this->validateDataArray($data);
        // verify that the number of data elements matches the number of action elements
        if (count($actions) !== count($data)) {
            throw new LengthException('The number of actions requested does not match the number of data elements provided.'); 
        }

        // set up curl handles
        $this->curlMultiSetup(count($actions));
        $this->setRequestUrls($actions);
        $this->setRequestDataArray($data);
        foreach($this->curlHandles as $curl) {
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); // explicitly set the method to PUT 
        }
        $this->curlMultiExec();

        return $this->curlMultiHttpResponse;
    }

    /**
     * Method to perform multiple DELETE actions using curl_multi_exec.
     * 
     * @param array $actions
     * @param integer $maxHandles
     * @return RestMultiClient
     * @throws Exception
     * @throws InvalidArgumentException
     * @throws LengthException
     */
    public function delete($actions) {
        $this->validateActionArray($actions);

        // set up curl handles
        $this->curlMultiSetup(count($actions));
        $this->setRequestUrls($actions);
        foreach($this->curlHandles as $curl) {
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); // explicitly set the method to DELETE
        }
        $this->curlMultiExec();

        return $this->curlMultiHttpResponse;
    }

    /**
     * Method to perform multiple HEAD actions using curl_multi_exec.
     * 
     * @param array $actions
     * @return RestMultiClient
     * @throws Exception
     * @throws InvalidArgumentException
     * @throws LengthException
     */
    public function head($actions) {
        $this->validateActionArray($actions);

        // set up curl handles
        $this->curlMultiSetup(count($actions));
        $this->setRequestUrls($actions);
        foreach($this->curlHandles as $curl) {
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
            curl_setopt($curl, CURLOPT_NOBODY, true);
        }
        $this->curlMultiExec();

        return $this->curlMultiHttpResponse;
    }

    /**
     * Sets maximum number of handles that will be instantiated for curl_multi_exec calls
     * 
     * @param integer $maxHandles
     * @return RestMultiClient
     * @throws InvalidArgumentException
     */
    public function setMaxHandles($maxHandles) {
        if (!is_integer($maxHandles) || $maxHandles <= 0) {
            throw new InvalidArgumentException('A non-integer value was passed for max_handles parameter.');     
        }
        $this->maxHandles = $maxHandles;

        return $this->curlMultiHttpResponse;
    }

    /**
     * Getter for maxHandles setting
     * 
     * @return integer
     */
    public function getMaxHandles() {
        return $this->maxHandles;
    }

    /**
     * Method to set up a given number of curl handles for use with curl_multi_exec
     * 
     * @param integer $handlesNeeded
     * @return void
     * @throws Exception
     */
    private function curlMultiSetup($handlesNeeded) {
        $multiCurl = curl_multi_init();
        if($multiCurl === false) {
            throw new Exception('multi_curl handle failed to initialize.');
        }
        $this->curlMultiHandle = $multiCurl;

        for($i = 0; $i < $handlesNeeded; $i++) {
            $curl = $this->curlInit();
            $this->curlHandles[$i] = $curl;
            curl_multi_add_handle($this->curlMultiHandle, $curl);
        }
    }

    /**
     * Method to reset the curlMultiHandle and all individual curlHandles related to it.
     * 
     * @return void
     */
    private function curlMultiTeardown() {
        foreach ($this->curlHandles as $curl) {
            curl_multi_remove_handle($this->curlMultiHandle, $curl);
            $this->curlClose($curl);
        }
        curl_multi_close($this->curlMultiHandle);
        $this->curlHandles = array();
        $this->curlMultiHandle = null;
    }

    /**
     * Method to execute curl_multi call
     * 
     * @return void
     * @throws Exception
     */
    private function curlMultiExec() {
        // start multi_exec execution
        do {
            $status = curl_multi_exec($this->curlMultiHandle, $active);
        } while ($status === CURLM_CALL_MULTI_PERFORM || $active);

        // see if there are any errors on the multi_exec call as a whole
        if($status !== CURLM_OK) {
            throw new Exception('curl_multi_exec failed with status "' . $status . '"');
        }

        // process the results. Note there could be individual errors on specific calls
        $this->curlMultiHttpResponse = new CurlMultiHttpResponse();
        foreach($this->curlHandles as $i => $curl) {
            try {
                $response = new CurlHttpResponse(
                    curl_multi_getcontent($curl),
                    curl_getinfo($curl)
                );
            } catch (InvalidArgumentException $e) {
                $this->curlMultiTeardown();
                throw new Exception(
                   'Unable to instantiate CurlHttpResponse. Message: "' . $e->getMessage() . '"',
                   $e->getCode(),
                   $e
                );
            }
            $this->curlMultiHttpResponse->addResponse($response);
        }
        $this->curlMultiTeardown();
    }

    /**
     * Method to reset all properties specific to a particular request/response sequence.
     * 
     * @return void
     */
    protected function resetRequestResponseProperties() {
        $this->$curlMultiHttpResponse = null;
        $this->requestHeaders = array();
        $this->requestDataArray = array();
    }

    /**
     * Method to set the urls for  multi_exec action
     * 
     * @param array $actions
     * @return void
     */
    private function setRequestUrls(array $actions) {
        for ($i = 0; $i < count($actions); $i++) {
            $url = $this->buildUrl($actions[$i]);
            $this->requestUrls[$i] = $url;
            curl_setopt($this->curlHandles[$i], CURLOPT_URL, $url);
        }   
    }

    /**
     * Method to set array of data to be sent along with multi_exec POST/PUT requests
     * 
     * @param array $data
     * @return void
     */
    private function setRequestDataArray(array $data) {
        for ($i = 0; $i < count($data); $i++) {
            $data = $data[$i];
            $this->requestDataArray[$i] = $data;
            curl_setopt($this->curlHandles[$i], CURLOPT_POSTFIELDS, $data);
        }
    }

    /**
     * Method to provide common validation for action array parameters
     * 
     * @param array $actions
     * @return void
     * @throws InvalidArgumentException
     * @throws LengthException
     */
    private function validateActionArray(array $actions) {
        if(empty($actions)) {
            throw new InvalidArgumentException('An empty array was passed for actions parameter.');
        }
        if(count($actions) > $this->maxHandles) {
            throw new LengthException('Length of actions array exceeds maxHandles setting.');
        }
        foreach($actions as $action) {
            $this->validateAction($action);
        }
    }

    /**
     * Method to provide common validation for data array parameters
     * 
     * @param array $data
     * @return void
     * @throws InvalidArgumentException
     * @throws LengthException
     */
    private function validateDataArray(array $data) {
        if(empty($data)) {
            throw new InvalidArgumentException('An empty array was passed for data parameter');
        }
        if(count($data) > $this->maxHandles) {
            throw new LengthException('Length of data array exceeds maxHandles setting.');
        }
        foreach($data as $item) {
            $this->validateData($item);
        }
    }
}

RestMultiClient unit tests

<?php

namespace MikeBrantRestClientLib;

use PHPUnitFrameworkTestCase;

/**
 * Mock for curl_multi_init global function
 * 
 * @return mixed
 */
function curl_multi_init() {
    if (!is_null(RestMultiClientTest::$curlMultiInitResponse)) {
        return RestMultiClientTest::$curlMultiInitResponse;
    }
    return curl_multi_init();
}

/**
 * Mock for curl_multi_exec global function
 * 
 * @param resource curl_multi handle
 * @param integer flag indicating if there are still active handles.
 * @return integer
 */
function curl_multi_exec($multiCurl, &$active) {
    if (is_null(RestMultiClientTest::$curlMultiExecResponse)) {
        return curl_multi_exec($multiCurl, $active);
    }
    $active = 0;
    return RestMultiClientTest::$curlMultiExecResponse;
}

/**
 * Mock for curl_multi_getcontent global function
 * 
 * @param resource curl handle
 * @return string
 */
function curl_multi_getcontent($curl) {
    if (!is_null(RestMultiClientTest::$curlMultiGetcontentResponse)) {
        return RestMultiClientTest::$curlMultiGetcontentResponse;
    }
    return curl_multi_getcontent($curl);
}

/**
 * This is hacky workaround for avoiding double definition of this global method override
 * when running full test suite on this library.
 */
if(!function_exists('MikeBrantRestClientLibcurl_getinfo')) {

    /**
     * Mock for curl_getinfo function
     * 
     * @param resource curl handle
     * @return mixed
     */
    function curl_getinfo($curl) {
        $backtrace = debug_backtrace();
        $testClass = $backtrace[1]['class'] . 'Test';
        if (!is_null($testClass::$curlGetinfoResponse)) {
            return $testClass::$curlGetinfoResponse;
        }
        return curl_getinfo($curl);
    }
}

class RestMultiClientTest extends TestCase
{
    public static $curlMultiInitResponse = null;

    public static $curlMultiExecResponse = null;

    public static $curlMultiGetcontentResponse = null;

    public static $curlGetinfoResponse = null;

    protected $client = null;

    protected $curlMultiExecFailedResponse = CURLM_INTERNAL_ERROR;

    protected $curlMultiExecCompleteResponse = CURLM_OK;

    protected $curlGetinfoMockResponse = array(
        'url' => 'http://google.com/',
        'content_type' => 'text/html; charset=UTF-8',
        'http_code' => 200,
        'header_size' => 321,
        'request_size' => 49,
        'filetime' => -1,
        'ssl_verify_result' => 0,
        'redirect_count' => 0,
        'total_time' => 1.123264,
        'namelookup_time' => 1.045272,
        'connect_time' => 1.070183,
        'pretransfer_time' => 1.071139,
        'size_upload' => 0,
        'size_download' => 219,
        'speed_download' => 194,
        'speed_upload' => 0,
        'download_content_length' => 219,
        'upload_content_length' => -1,
        'starttransfer_time' => 1.122377,
        'redirect_time' => 0,
        'redirect_url' => 'http://www.google.com/',
        'primary_ip' => '216.58.194.142',
        'certinfo' => array(),
        'primary_port' => 80,
        'local_ip' => '192.168.1.74',
        'local_port' => 59733,
        'request_header' => "GET / HTTP/1.1nHost: google.comnAccept: */*",
    );

    protected function setUp() {
        self::$curlMultiInitResponse = null;
        self::$curlMultiExecResponse = null;
        self::$curlMultiGetcontentResponse = null;
        self::$curlGetinfoResponse = null;
        $this->client = new RestMultiClient();
    }

    protected function tearDown() {
        $this->client = null;
    }

    /**
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibRestMultiClient::validateActionArray
     */
    public function testValidateActionArrayThrowsExceptionOnEmptyArray() {
        $this->client->get(array());
    }

    /**
     * @expectedException LengthException
     * @covers MikeBrantRestClientLibRestMultiClient::validateActionArray
     */
    public function testValidateActionArrayThrowsExceptionOnOversizedArray() {
        $maxHandles = $this->client->getMaxHandles();
        $this->client->get(
            array_fill(0, $maxHandles + 1, 'action')
        );
    }

    /**
     * @expectedException InvalidArgumentException
     * @covers MikeBrantRestClientLibRestMultiClient::validateDataArray
     */
    public function testValidateDataArrayThrowsExceptionOnEmptyArray() {
        $this->client->get(array());
    }

    /**
     * @expectedException LengthException
     * @covers MikeBrantRestClientLibRestMultiClient::validateDataArray
     */
    public function testValidateDataArrayThrowsExceptionOnOversizedArray() {
        $maxHandles = $this->client->getMaxHandles();
        $this->client->post(
            array_fill(0, $maxHandles, 'action'),
            array_fill(0, $maxHandles + 1, 'data')
        );
    }

    /**
     * @expectedException Exception
     * @covers MikeBrantRestClientLibRestMultiClient::curlMultiSetup
     */
    public function testCurlMultiSetupThrowsExceptionOnCurlMultiInitFailure() {
        self::$curlMultiInitResponse = false;
        $this->client->get(
            array_fill(0, 2, 'action')
        );
    }
    /**
     * @expectedException Exception
     * @covers MikeBrantRestClientLibRestMultiClient::curlMultiExec
     */
    public function testCurlMultiExecThrowsExceptionOnMultiCurlFailure() {
        self::$curlMultiExecResponse = $this->curlMultiExecFailedResponse;
        $this->client->get(
            array_fill(0, 2, 'action')
        );
    }

    /**
     * @expectedException Exception
     * @covers MikeBrantRestClientLibRestMultiClient::curlMultiExec
     */
    public function testCurlMultiExecThrowsExceptionOnMalformedCurlHttpResponse() {
        self::$curlMultiExecResponse = $this->curlMultiExecCompleteResponse;
        self::$curlMultiGetcontentResponse = 'test';
        self::$curlGetinfoResponse = array();
        $this->client->get(
            array_fill(0, 2, 'action')
        );
    }
    /**
     * @covers MikeBrantRestClientLibRestMultiClient::get
     * @covers MikeBrantRestClientLibRestMultiClient::validateActionArray
     * @covers MikeBrantRestClientLibRestMultiClient::curlMultiSetup
     * @covers MikeBrantRestClientLibRestMultiClient::resetRequestResponseProperties
     * @covers MikeBrantRestClientLibRestMultiClient::setRequestUrls
     * @covers MikeBrantRestClientLibRestMultiClient::curlMultiExec
     * @covers MikeBrantRestClientLibRestMultiClient::curlMultiTeardown
     */
    public function testGet() {
        self::$curlMultiExecResponse = $this->curlMultiExecCompleteResponse;
        self::$curlMultiGetcontentResponse = 'test';
        self::$curlGetinfoResponse = $this->curlGetinfoMockResponse;
        $response = $this->client->get(
            array_fill(0, 2, 'action')
        );
        $this->assertInstanceOf(CurlMultiHttpResponse::class, $response);
        $this->assertAttributeEquals(null, 'curlMultiHandle', $this->client);
    }

    /**
     * @expectedException LengthException
     * @covers MikeBrantRestClientLibRestMultiClient::post
     */
    public function testPostThrowsExceptionOnArraySizeMismatch() {
        $maxHandles = $this->client->getMaxHandles();
        $this->client->post(
            array_fill(0, $maxHandles, 'action'),
            array_fill(0, $maxHandles - 1, 'data')
        );
    }

    /**
     * @covers MikeBrantRestClientLibRestMultiClient::post
     * @covers MikeBrantRestClientLibRestMultiClient::validateData
     * @covers MikeBrantRestClientLibRestMultiClient::setRequestData
     */
    public function testPost() {
        self::$curlMultiExecResponse = $this->curlMultiExecCompleteResponse;
        self::$curlMultiGetcontentResponse = 'test';
        self::$curlGetinfoResponse = $this->curlGetinfoMockResponse;
        $response = $this->client->post(
            array_fill(0, 2, 'action'),
            array_fill(0, 2, 'data')
        );
        $this->assertInstanceOf(CurlMultiHttpResponse::class, $response);
        $this->assertAttributeEquals(null, 'curlMultiHandle', $this->client);
   }

    /**
     * @expectedException LengthException
     * @covers MikeBrantRestClientLibRestMultiClient::put
     */
    public function testPutThrowsExceptionOnArraySizeMismatch() {
        $maxHandles = $this->client->getMaxHandles();
        $this->client->put(
            array_fill(0, $maxHandles, 'action'),
            array_fill(0, $maxHandles - 1, 'data')
        );
    }

    /**
     * @covers MikeBrantRestClientLibRestMultiClient::put
     */
    public function testPut() {
        self::$curlMultiExecResponse = $this->curlMultiExecCompleteResponse;
        self::$curlMultiGetcontentResponse = 'test';
        self::$curlGetinfoResponse = $this->curlGetinfoMockResponse;
        $response = $this->client->put(
            array_fill(0, 2, 'action'),
            array_fill(0, 2, 'data')
        );
        $this->assertInstanceOf(CurlMultiHttpResponse::class, $response);
        $this->assertAttributeEquals(null, 'curlMultiHandle', $this->client);
    }

    /**
     * @covers MikeBrantRestClientLibRestMultiClient::delete
     */
    public function testDelete() {
        self::$curlMultiExecResponse = $this->curlMultiExecCompleteResponse;
        self::$curlMultiGetcontentResponse = 'test';
        self::$curlGetinfoResponse = $this->curlGetinfoMockResponse;
        $response = $this->client->delete(
            array_fill(0, 2, 'action')
        );
        $this->assertInstanceOf(CurlMultiHttpResponse::class, $response);
        $this->assertAttributeEquals(null, 'curlMultiHandle', $this->client);
    }

    /**
     * @covers MikeBrantRestClientLibRestMultiClient::head
     */
    public function testHead() {
        self::$curlMultiExecResponse = $this->curlMultiExecCompleteResponse;
        self::$curlMultiGetcontentResponse = 'test';
        self::$curlGetinfoResponse = $this->curlGetinfoMockResponse;
        $response = $this->client->head(
            array_fill(0, 2, 'action')
        );
        $this->assertInstanceOf(CurlMultiHttpResponse::class, $response);
        $this->assertAttributeEquals(null, 'curlMultiHandle', $this->client);
    }
}

CurlMultiHttpResponse class

<?php

namespace MikeBrantRestClientLib;

class CurlMultiHttpResponse
{
    /**
     * Variable to store individual CurlHttpResponse objects from curl_multi call
     * 
     * @var array
     */
    protected $curlHttpResponses = array();

    /**
     * Constructor method. Currently there is no instantiation logic.
     */
    public function __construct() {}

    /**
     * Method to add CurlHttpResponse object to collection
     * 
     * @param CurlHttpResponse $response
     * @return void
     */
    public function addResponse(CurlHttpResponse $response) {
        $this->curlHttpResponses[] = $response;
    }

    /**
     * Returns array of all CurlHttpResponse objects in collection.
     * 
     * @return array
     */
    public function getCurlHttpResponses() {
        return $this->curlHttpResponses;
    }

    /**
     * Alias for getCurlHttpResponses
     * 
     * @return array
     */
    public function getAll() {
        return $this->getCurlHttpResponses();
    }

    /**
     * Returns array of response bodies for each response in collection.
     * 
     * @return array
     */
    public function getResponseBodies() {
        return array_map(
            function(CurlHttpResponse $value) {
                return $value->getBody();
            },
            $this->curlHttpResponses
        );
    }

    /**
     * Returns array of response codes for each response in collection.
     * 
     * @return array
     */
    public function getHttpCodes() {
        return array_map(
            function(CurlHttpResponse $value) {
                return $value->getHttpCode();
            },
            $this->curlHttpResponses
        );
    }

    /**
     * Returns array of URL's used for each response in collectoin as returned via curl_getinfo.
     * 
     * @return array
     */
    public function getRequestUrls() {
        return array_map(
            function(CurlHttpResponse $value) {
                return $value->getRequestUrl();
            },
            $this->curlHttpResponses
        );
    }

    /**
     * Returns array of request headers for each response in collection as returned via curl_getinfo.
     * 
     * @return array
     */
    public function getRequestHeaders() {
        return array_map(
            function(CurlHttpResponse $value) {
                return $value->getRequestHeader();
            },
            $this->curlHttpResponses
        );
    }

    /**
     * Returns array of curl_getinfo arrays for each response in collection.
     * See documentation at http://php.net/manual/en/function.curl-getinfo.php for expected format for each array element.
     * 
     * @return array
     */
    public function getCurlGetinfoArrays() {
        return array_map(
            function(CurlHttpResponse $value) {
                return $value->getCurlGetinfo();
            },
            $this->curlHttpResponses
        );
    }
}

CurlMultiHttpResponse unit tests

<?php

namespace MikeBrantRestClientLib;

use PHPUnitFrameworkTestCase;

class CurlMultiHttpResponseTest extends TestCase
{
    protected $curlMultiHttpResponse = null;

    protected $curlExecMockResponse = 'Test Response';

    protected $curlGetinfoMockResponse = array(
        'url' => 'http://google.com/',
        'content_type' => 'text/html; charset=UTF-8',
        'http_code' => 200,
        'header_size' => 321,
        'request_size' => 49,
        'filetime' => -1,
        'ssl_verify_result' => 0,
        'redirect_count' => 0,
        'total_time' => 1.123264,
        'namelookup_time' => 1.045272,
        'connect_time' => 1.070183,
        'pretransfer_time' => 1.071139,
        'size_upload' => 0,
        'size_download' => 219,
        'speed_download' => 194,
        'speed_upload' => 0,
        'download_content_length' => 219,
        'upload_content_length' => -1,
        'starttransfer_time' => 1.122377,
        'redirect_time' => 0,
        'redirect_url' => 'http://www.google.com/',
        'primary_ip' => '216.58.194.142',
        'certinfo' => array(),
        'primary_port' => 80,
        'local_ip' => '192.168.1.74',
        'local_port' => 59733,
        'request_header' => "GET / HTTP/1.1nHost: google.comnAccept: */*",
    );

    protected function setUp() {
        $this->curlMultiHttpResponse = new CurlMultiHttpResponse();
    }

    public function curlHttpResponseProvider() {
        return array(
            array(
                new CurlHttpResponse($this->curlExecMockResponse, $this->curlGetinfoMockResponse)
            )
        );
    }

    /**
     * @dataProvider curlHttpResponseProvider
     * @covers MikeBrantRestClientLibCurlMultiHttpResponse::addResponse
     * @covers MikeBrantRestClientLibCurlMultiHttpResponse::getCurlHttpResponses
     * @covers MikeBrantRestClientLibCurlMultiHttpResponse::getAll
     */
    public function testAddResponse($curlHttpResponse) {
        $responseArray = array_fill(0, 5, $curlHttpResponse);
        for ($i = 0; $i < count($responseArray); $i++) {
            $this->curlMultiHttpResponse->addResponse($curlHttpResponse);
        }
        $this->assertEquals($responseArray, $this->curlMultiHttpResponse->getCurlHttpResponses());
        $this->assertEquals($responseArray, $this->curlMultiHttpResponse->getAll());
    }

    /**
     * @dataProvider curlHttpResponseProvider
     * @covers MikeBrantRestClientLibCurlMultiHttpResponse::getResponseBodies
     */
    public function testGetRepsonseBodies($curlHttpResponse) {
        $responseArray = array_fill(0, 5, $curlHttpResponse);
        for ($i = 0; $i < count($responseArray); $i++) {
            $this->curlMultiHttpResponse->addResponse($curlHttpResponse);
        }
        $responseBodies = array_map(
            function($val) {
                return $val->getBody();
            },
            $responseArray
        );
        $this->assertEquals($responseBodies, $this->curlMultiHttpResponse->getResponseBodies());
    }

    /**
     * @dataProvider curlHttpResponseProvider
     * @covers MikeBrantRestClientLibCurlMultiHttpResponse::getHttpCodes
     */
    public function testgetHttpCodes($curlHttpResponse) {
        $responseArray = array_fill(0, 5, $curlHttpResponse);
        for ($i = 0; $i < count($responseArray); $i++) {
            $this->curlMultiHttpResponse->addResponse($curlHttpResponse);
        }
        $responseCodes = array_map(
            function($val) {
                return $val->getHttpCode();
            },
            $responseArray
        );
        $this->assertEquals($responseCodes, $this->curlMultiHttpResponse->getHttpCodes());
    }

    /**
     * @dataProvider curlHttpResponseProvider
     * @covers MikeBrantRestClientLibCurlMultiHttpResponse::getRequestUrls
     */
    public function testGetRequestUrls($curlHttpResponse) {
        $responseArray = array_fill(0, 5, $curlHttpResponse);
        for ($i = 0; $i < count($responseArray); $i++) {
            $this->curlMultiHttpResponse->addResponse($curlHttpResponse);
        }
        $requestUrls = array_map(
            function($val) {
                return $val->getRequestUrl();
            },
            $responseArray
        );
        $this->assertEquals($requestUrls, $this->curlMultiHttpResponse->getRequestUrls());
    }

    /**
     * @dataProvider curlHttpResponseProvider
     * @covers MikeBrantRestClientLibCurlMultiHttpResponse::getRequestHeaders
     */
    public function testGetRequestHeaders($curlHttpResponse) {
        $responseArray = array_fill(0, 5, $curlHttpResponse);
        for ($i = 0; $i < count($responseArray); $i++) {
            $this->curlMultiHttpResponse->addResponse($curlHttpResponse);
        }
        $requestHeaders = array_map(
            function($val) {
                return $val->getRequestHeader();
            },
            $responseArray
        );
        $this->assertEquals($requestHeaders, $this->curlMultiHttpResponse->getRequestHeaders());
    }

    /**
     * @dataProvider curlHttpResponseProvider
     * @covers MikeBrantRestClientLibCurlMultiHttpResponse::getCurlGetinfoArrays
     */
    public function testGetCurlGetinfoArrays($curlHttpResponse) {
        $responseArray = array_fill(0, 5, $curlHttpResponse);
        for ($i = 0; $i < count($responseArray); $i++) {
            $this->curlMultiHttpResponse->addResponse($curlHttpResponse);
        }
        $requestInfoArrays = array_map(
            function($val) {
                return $val->getCurlGetinfo();
            },
            $responseArray
        );
        $this->assertEquals($requestInfoArrays, $this->curlMultiHttpResponse->getCurlGetinfoArrays());
    }
}


Get this bounty!!!

Installing Apache UserGrid on linux

About the Project

Apache Usergrid is an open-source Backend-as-a-Service (BaaS or mBaaS) composed of an integrated distributed NoSQL database, application layer and client tier with SDKs for developers looking to rapidly build web and/or mobile applications. It provides elementary services and retrieval features like:

  • User Registration & Management
  • Data Storage
  • File Storage
  • Queues
  • Full Text Search
  • Geolocation Search
  • Joins

It is a multi-tenant system designed for deployment to public cloud environments (such as Amazon Web Services, Rackspace, etc.) or to run on traditional server infrastructures so that anyone can run their own private BaaS deployment.

For architects and back-end teams, it aims to provide a distributed, easily extendable, operationally predictable and highly scalable solution. For front-end developers, it aims to simplify the development process by enabling them to rapidly build and operate mobile and web applications without requiring backend expertise.

Usergrid 2.1.0 Deployment Guide

Though the Usergrid Deployment guide seems to be simple enough, I faced certain hiccups and it took me about 4 days to figure out what I was doing wrong.

This document explains how to deploy the Usergrid v2.1.0 Backend-as-a-Service (BaaS), which comprises the Usergrid Stack, a Java web application, and the Usergrid Portal, which is an HTML5/JavaScript application.

Prerequsites

Below are the software requirements for Usergrid 2.1.0 Stack and Portal. You can install them all on one computer for development purposes, and for deployment you can deploy them separately using clustering.

Linux or a UNIX-like system (Usergrid may run on Windows, but we haven’t tried it)

Download the Apache Usergrid 2.1.0 binary release from the official Usergrid releases page:

After untarring the files that you need for deploying Usergrid Stack and Portal are ROOT.war and usergrid-portal.tar.

Stack STEP #1: Setup Cassandra

As mentioned in prerequisites, follow the installation guide given in link

Usergrid uses Cassandra’s Thrift protocol
Before starting cassandra, on Cassandra 2.x releases you MUST enable Thrift by setting start_rpc in your cassandra.yaml file:

    #Whether to start the thrift rpc server.
    start_rpc: true

Note:DataStax no longer supports the DataStax Community version of Apache Cassandra or the DataStax Distribution of Apache Cassandra. It is best to follow the Apache Documentation

Once you are up and running make a note of these things:

  • The name of the Cassandra cluster
  • Hostname or IP address of each Cassandra node
    • in case of same machine as Usergrid, then localhost. Usergrid would then be running on single machine embedded mode.
  • Port number used for Cassandra RPC (the default is 9160)
  • Replication factor of Cassandra cluster

Stack STEP #2: Setup ElasticSearch

Usergrid also needs access to at least one ElasticSearch node. As with Cassandra, you can setup single ElasticSearch node on your computer, and you should run a cluster in production.

Steps:

  • Download and unzip Elasticsearch
  • Run bin/elasticsearch (or bin\elasticsearch -d on Linux as Background Process) (or bin\elasticsearch.bat on Windows)
  • Run curl http://localhost:9200/

Once you are up and running make a note of these things:

  • The name of the ElasticSearch cluster
  • Hostname or IP address of each ElasticSearch node
    • in case of same machine as Usergrid, then localhost. Usergrid would then be running on single machine embedded mode.
  • Port number used for ElasticSearch protocol (the default is 9200)

Stack STEP #3: Setup Tomcat

The Usergrid Stack is contained in a file named ROOT.war, a standard Java EE WAR ready for deployment to Tomcat. On each machine that will run the Usergrid Stack you must install the Java SE 8 JDK and Tomcat 7+.

Stack STEP #4: Configure Usergrid Stack

You must create a Usergrid properties file called usergrid-deployment.properties. The properties in this file tell Usergrid how to communicate with Cassandra and ElasticSearch, and how to form URLs using the hostname you wish to use for Usegrid. There are many properties that you can set to configure Usergrid.

Once you have created your Usergrid properties file, place it in the Tomcat lib directory. On a Linux system, that directory is probably located at /path/to/tomcat7/lib/

The Default Usergrid Properties File

You should review the defaults in the above file. To get you started, let’s look at a minimal example properties file that you can edit and use as your own.

Please note that if you are installing Usergrid on the same machine as Cassandra Server, then set the following property to true

   #Tell Usergrid that Cassandra is not embedded.
   cassandra.embedded=true

Stack STEP #5: Deploy ROOT.war to Tomcat

The next step is to deploy the Usergrid Stack software to Tomcat. There are a variety of ways of doing this and the simplest is probably to place the Usergrid Stack ROOT.war file into the Tomcat webapps directory, then restart Tomcat.

Look for messages like this, which indicate that the ROOT.war file was deployed:

INFO: Starting service Catalina
Jan 29, 2016 1:00:32 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.59
Jan 29, 2016 1:00:32 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /usr/share/tomcat7/webapps/ROOT.war

Does it work?

you can use curl:

curl http://localhost:8080/status

If you get a JSON file of status data, then you’re ready to move to the next step. You should see a response that begins like this:

{
“timestamp” : 1454090178953,
“duration” : 10,
“status” : {
“started” : 1453957327516,
“uptime” : 132851437,
“version” : “201601240200-595955dff9ee4a706de9d97b86c5f0636fe24b43”,
“cassandraAvailable” : true,
“cassandraStatus” : “GREEN”,
“managementAppIndexStatus” : “GREEN”,
“queueDepth” : 0,
“org.apache.usergrid.count.AbstractBatcher” : {
“add_invocation” : {
“type” : “timer”,
“unit” : “microseconds”,
… etc. …

Initialize the Usergrid Database

Next, you must initialize the Usergrid database, index and query systems.

To do this you must issue a series of HTTP operations using the superuser credentials. You can only do this if Usergrid is configured to allow superused login via this property usergrid.sysadmin.login.allowed=true and if you used the above example properties file, it is allowed.

The three operation you must perform are expressed by the curl commands below and, of course, you will have ot change the password test to match the superuser password that you set in your Usergrid properties file.

curl -X PUT http://localhost:8080/system/database/setup -u superuser:test
curl -X PUT http://localhost:8080/system/database/bootstrap -u superuser:test
curl -X GET http://localhost:8080/system/superuser/setup -u superuser:test

When you issue each of those curl commands, you should see a success message like this:

{
“action” : “cassandra setup”,
“status” : “ok”,
“timestamp” : 1454100922067,
“duration” : 374
}

Now that you’ve gotten Usergrid up and running, you’re ready to deploy the Usergrid Portal.

Deploying the Usergrid Portal

The Usergrid Portal is an HTML5/JavaScript application, a bunch of static files that can be deployed to any web server, e.g. Apache HTTPD or Tomcat.

To deploy the Portal to a web server, you will un-tar the usergrid-portal.tar file into directory that serves as the root directory of your web pages.

Once you have done that there is one more step. You need to configure the portal so that it can find the Usergrid stack. You do that by editing the portal/config.js and changing this line:

Usergrid.overrideUrl = ’http://localhost:8080/‘;

To set the hostname that you will be using for your Usergrid installation.

I have deployed a sample instance and tested the same. You can find the system ready configurations in TechUtils repository

REST Application in Java

Below are the basic steps to create a working REST API Application in java, using Eclipse.
The demo shows just a “To Uppercase String” application

Steps:

  • In Eclipse, create a new Project(Dynamic Web Project)
  • Create the new Project and give it a suitable name.
  • after project creation, right click on the project, and select Configure-> Convert to Maven Project
  • After process completion, a pom.xml would be made available.
  • Add the dependencies from the pom.xml that I have presented below.
  • Create new Class as sample shown below and create methods that you need to expose.
  • Add business logic as per need.
  • Add Annotations to the class as shown below
  • In the Web Content folder, modify the web.xml as shown in the sample web.xml

The basic mode is complete. Now just configure your local web server and deploy your application to the web server.

Use any REST Client like Postman(Chrome Plugin) to test the app.

Code Samples

Unirest: Lightweight HTTP Request Client Libraries

UniRest

Unirest is a set of lightweight HTTP libraries available in multiple languages, built and maintained by the Mashape team.

Unirest.post("http://httpbin.org/post")
  .queryString("name", "Mark")
  .field("last", "Polo")
  .asJson()

Features

  • Make GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS requests
  • Both syncronous and asynchronous (non-blocking) requests
  • It supports form parameters, file uploads and custom body entities
  • Easily add route parameters without ugly string concatenations
  • Supports gzip
  • Supports Basic Authentication natively
  • Customizable timeout, concurrency levels and proxy settings
  • Customizable default headers for every request (DRY)
  • Customizable HttpClient and HttpAsyncClient implementation
  • Automatic JSON parsing into a native object for JSON responses
  • Customizable binding, with mapping from response body to java Object

Installing

Is easy as pie. Kidding. It’s about as easy as doing these little steps:

With Maven

You can use Maven by including the library:

<dependency>
    <groupId>com.mashape.unirest</groupId>
    <artifactId>unirest-java</artifactId>
    <version>1.4.7</version>
</dependency>

There are dependencies for Unirest-Java, these should be already installed, and they are as follows:

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.3.6</version>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpasyncclient</artifactId>
  <version>4.0.2</version>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpmime</artifactId>
  <version>4.3.6</version>
</dependency>
<dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20140107</version>
</dependency>

If you would like to run tests, also add the following dependency along with the others:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.4</version>
  <scope>test</scope>
</dependency>

Without Maven

Alternatively if you don’t use Maven, you can directly include the JAR file in the classpath:http://oss.sonatype.org/content/repositories/releases/com/mashape/unirest/unirest-java/1.4.7/unirest-java-1.4.7.jar

Don’t forget to also install the dependencies (org.json, httpclient 4.3.6, httpmime 4.3.6,httpasyncclient 4.0.2) in the classpath too.

There is also a way to generate a Unirest-Java JAR file that already includes the required dependencies, but you will need Maven to generate it. Follow the instructions at http://blog.mashape.com/post/69117323931/installing-unirest-java-with-the-maven-assembly-plugin

Creating Request

So you’re probably wondering how using Unirest makes creating requests in Java easier, here is a basic POST request that will explain everything:

HttpResponse<JsonNode> jsonResponse = Unirest.post("http://httpbin.org/post")
  .header("accept", "application/json")
  .queryString("apiKey", "123")
  .field("parameter", "value")
  .field("foo", "bar")
  .asJson();

Requests are made when as[Type]() is invoked, possible types include Json, Binary, String, Object.

If the request supports and it is of type HttpRequestWithBody, a body it can be passed along with.body(String|JsonNode|Object). For using .body(Object) some pre-configuration is needed (see below).

If you already have a map of parameters or do not wish to use seperate field methods for each one there is a.fields(Map<String, Object> fields) method that will serialize each key – value to form parameters on your request.

.headers(Map<String, String> headers) is also supported in replacement of multiple header methods.

Full Documentation @ unirest.io