Jump to content
XboxLeaders
Sign in to follow this  
greena5

Wrapper

Recommended Posts

<?php
// This try/catch can be placed in the file where you wish to process the received data
// The $data variable will contain the data if exceptions are not thrown
try {
	$requestData = array();
	$api = new LeadersApi();
	foreach ($api->_allowedRequests as $var) {
		$requestData[$var] = (isset($_GET[$var])) ? $_GET[$var] : null;
	}
	$data = $api->processRequest($requestData);
} catch (Exception $e) {
	echo $e->getMessage();
}

// Below can go in a new file but remember to include it!!
Class LeadersApi
{	
	/**
	 * A list of all possible request variables
	 * 
	 * @var array
	 */
	public $_allowedRequests = array('type', 'tag', 'gameId');
	
	/**
	 * The base URL for the API
	 * 
	 * @var string
	 */
	protected $_baseUri = 'http://www.xboxleaders.com/api/';	
	
	/**
	 * Gets the data from a URL build with supplied parameters
	 * 
	 * @param $file String
	 * @param $requestVars array
	 */
	protected function getData($file, array $requestVars)
	{	
		try {
			$url = $this->_baseUri . strtolower($file) . ".json?" . http_build_query($requestVars, null, '&');
			return json_decode(file_get_contents($url));
		} catch (Exception $e) {
			return $e;			
		}
	}
	
	/**
	 * Construct method to ensure user has correct libs
	 */
	public function __construct()
	{	
		if (!function_exists('json_decode')) {
			throw new Exception('This wrapper requires the JSON library');
		}
	}
	
	/**
	 * Processed the variables collected from the view
	 *
	 * @param $options array
	 */
	public function processRequest(array $options)
	{	
		if(null !== $options['type']) {
			$type = ($options['type'] = ucfirst(strtolower($options['type'])));
			if(method_exists(get_class($this), 'get' . $type)) {
				$method = "get" . $type;
				return $this->$method($options);
			} else {
				throw new Exception("The type: " . $options['type'] . ", does not exist!");
			}
		} else {
			throw new Exception('The request type has not been set');
		}
	}
	
	/**
	 * Ensures the parameters for games have been set and can therefore process data
	 * Will otherwise throw an exception
	 * 
	 * @param $option array
	 */
	protected function getGames($option)
	{
		if (null !== $option['tag']) {
			$parameters = array(
				'gamertag' => $option['tag']
			);
			return $this->getData($option['type'], $parameters);
		} else {
			$message = $this->exceptionMessage(__FUNCTION__);
			throw new Exception($message);
		}
	}
	
	/**
	 * Ensures the parameters for profile have been set and can therefore process data
	 * Will otherwise throw an exception
	 * 
	 * @param $option array
	 */
	protected function getProfile($option)
	{	
		if (null !== $option['tag']) {
			$parameters = array(
				'gamertag' => $option['tag']
			);
			return $this->getData($option['type'], $parameters);
		} else {
			$message = $this->exceptionMessage(__FUNCTION__);
			throw new Exception($message);
		}
	}
	
	/**
	 * Ensures the parameters for achievements have been set and can therefore process data
	 * Will otherwise throw an exception
	 * 
	 * @param $option array
	 */
	protected function getAchievements($option)
	{
		if(null !== $option['tag'] && null !== $option['gameId'] ) {
			$parameters = array(
				'gamertag' => $option['tag'],
				'gameid'   => $option['gameId']
			);
			return $this->getData($option['type'], $parameters);
		} else {
			$message = $this->exceptionMessage(__FUNCTION__);
			throw new Exception($message);
		}
	}
	
	/**
	 * Ensures the parameters for friends have been set and can therefore process data
	 * Will otherwise trow an exception
	 * 
	 * @param $option array
	 */
	protected function getFriends($option)
	{
		if(null !== $option['tag']) {
			$parameters = array(
				'gamertag' => $option['tag']
			);
			return $this->getData($option['type'], $parameters);
		} else {
			$message = $this->exceptionMessage(__FUNCTION__);
			throw new Exception($message);
		}
	}
	
	/**
	 * Generic message for all of the 'get' 
	 * @param $message string (the function name it was initiated on)
	 */
	public function exceptionMessage($message) {
		return "Unable to process $message request, the variables have not been set";
	}
}

 

Here's a wrapper I quickly knocked up tonight and its loosely based on the wrapper I'm sure you've all seen on Github.  It only returns decoded JSON Object Array (Not XML, PHP Blah Blah Blah).

 

The idea is that URL or the POST request variables determine what action is to be done when the Class is instantiated.

 

There is however, as of yet, no validation around the gamertag which to be honest is in the wrapper on github so feel free to mash this and that together if desired (I would advise you to do it!).

 

Anyway, it's nothing that I am using, just thought I would try knocking one up.

 

Also,  It doesn't use cURL, not sure if this is a good thing or a bad thing as I'm quite a noob in terms of coding experience :)

 

Erm, what else....

 

The $_allowedRequests are the variable keys that must be passed for example:

 

www.mywebsite.com?type=profile&tag=gamertag

 

when 2 params are required such as the achievements request, this might look like:

 

www.mywebsite.com?type=achievements&tag=gamertag&gameId=1096157379

 

type can be: profile, achievements, games, friends.  Which then maps to the relevant method in the Class.

 

 

Regards

 

Andrew

 

Any questions, either post on here of course, or, you can send me a message on twitter @addictedtoxbox

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...