Skip to content

Commit

Permalink
Use proper HTTP method when sending requests.
Browse files Browse the repository at this point in the history
Also switch library to use JSON for all responses. It's easier that way,
and XML is kind of a pain to deal with in PHP anyway.

Now to add real unit tests...
  • Loading branch information
powdahound committed Apr 15, 2010
1 parent 4acb25a commit 8696174
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 60 deletions.
107 changes: 56 additions & 51 deletions HipChat.php
Expand Up @@ -9,12 +9,6 @@ class HipChat {

const DEFAULT_TARGET = 'http://api.hipchat.com';

/**
* Response formats
*/
const FORMAT_JSON = 'json';
const FORMAT_XML = 'xml';

/**
* HTTP response codes from API
*
Expand Down Expand Up @@ -63,80 +57,74 @@ function __construct($api_token, $api_target = self::DEFAULT_TARGET,
*
* @see http://api.hipchat.com/docs/api/method/rooms/show
*/
public function get_room($room_id, $format = self::FORMAT_JSON) {
$args = array('room_id' => $room_id);
return $this->make_request("rooms/show", $args, $format);
public function get_room($room_id) {
$response = $this->make_request("rooms/show", array(
'room_id' => $room_id
));
return $response->room;
}

/**
* Get list of rooms
*
* @see http://api.hipchat.com/docs/api/method/rooms
* @see http://api.hipchat.com/docs/api/method/rooms/list
*/
public function get_rooms($format = self::FORMAT_JSON) {
return $this->make_request('rooms/list', array(), $format);
public function get_rooms() {
$response = $this->make_request('rooms/list');
return $response->rooms;
}

/**
* Get information about a user
*
* @see http://api.hipchat.com/docs/api/method/users/show
*/
public function get_user($user_id, $format = self::FORMAT_JSON) {
$args = array('user_id' => $user_id);
return $this->make_request("users/show", $args, $format);
public function get_user($user_id) {
$response = $this->make_request("users/show", array(
'user_id' => $user_id
));
return $response->user;
}

/**
* Get list of users
*
* @see http://api.hipchat.com/docs/api/method/users
* @see http://api.hipchat.com/docs/api/method/users/list
*/
public function get_users($format = self::FORMAT_JSON) {
return $this->make_request('users/list', array(), $format);
public function get_users() {
$response = $this->make_request('users/list');
return $response->users;
}

/**
* Send a message to a room
*
* @see http://api.hipchat.com/docs/api/method/rooms/message
*/
public function message_room($room_id, $from, $message,
$format = self::FORMAT_JSON) {
public function message_room($room_id, $from, $message) {
$args = array(
'room_id' => $room_id,
'from' => $from,
'message' => utf8_encode($message)
);
return $this->make_request("rooms/message", $args, $format);
$response = $this->make_request("rooms/message", $args);
return ($response->status == 'sent');
}


/////////////////////////////////////////////////////////////////////////////
// Private functions
/////////////////////////////////////////////////////////////////////////////

/**
* Make an API request using curl
*
* @param $method Which API method to hit. e.g.: 'rooms/show'
* @param $format Desired response format.
* @param $args Data to send via POST.
* @param $expected_response Expected HTTP response code (usually 200)
*/
private function make_request($method, $args = array(),
$format = self::FORMAT_JSON,
$expected_response = self::STATUS_OK) {
$url = "$this->api_target/$this->api_version/$method";
$headers = array("Authorization: HipChat $this->api_token");
private function curl_request($url, $headers = array(), $post_data = null) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
if (!empty($args)) {
if (is_array($post_data)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
$response = curl_exec($ch);

Expand All @@ -147,30 +135,47 @@ private function make_request($method, $args = array(),
throw new HipChat_Exception(self::STATUS_BAD_RESPONSE,
"CURL error: $errno - $error", $url);
}

// make sure we got a 200
$code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code != $expected_response) {
if ($code != self::STATUS_OK) {
throw new HipChat_Exception(self::STATUS_BAD_RESPONSE,
"HTTP status code: $code, response=$response", $url);
}

curl_close($ch);

// make sure response is valid
if ($format == self::FORMAT_JSON) {
$response = json_decode($response);
if (!$response) {
throw new HipChat_Exception(self::STATUS_BAD_RESPONSE,
"Invalid JSON received: $response", $url);
}
return $response;
}

/**
* Make an API request using curl
*
* @param $api_method Which API method to hit, like 'rooms/show'.
* @param $args Data to send.
* @param $http_method HTTP method (GET or POST).
*/
private function make_request($api_method, $args = array(),
$http_method = 'GET') {
$args['format'] = 'json';
$headers = array("Authorization: HipChat $this->api_token");
$url = "$this->api_target/$this->api_version/$api_method";
$post_data = null;

// add args to url for GET
if ($http_method == 'GET') {
$url .= '?'.http_build_query($args);
} else {
try {
$response = @new SimpleXMLElement($response);
} catch (Exception $e) {
throw new HipChat_Exception(self::STATUS_BAD_RESPONSE,
"Invalid XML received: $response", $url);
}
$post_data = $args;
}

$response = $this->curl_request($url, $headers, $post_data);

// make sure response is valid json
$response = json_decode($response);
if (!$response) {
throw new HipChat_Exception(self::STATUS_BAD_RESPONSE,
"Invalid JSON received: $response", $url);
}

return $response;
Expand Down
26 changes: 17 additions & 9 deletions test.php
Expand Up @@ -4,32 +4,40 @@
require 'HipChat.php';

if (!isset($argv[1])) {
echo "Usage: $argv[0] <token>\n";
echo "Usage: $argv[0] <token> [target]\n";
die;
}

$token = $argv[1];
$hc = new HipChat($token, 'http://api.hipchat.com');
$target = isset($argv[2]) ? $argv[2] : 'http://api.hipchat.com';
$hc = new HipChat($token, $target);

echo "Testing HipChat API with token=$token\n\n";
echo "Testing HipChat API.\nTarget: $target\nToken: $token\n\n";

// get rooms
echo "Hitting /rooms.json\n";
echo "Rooms:\n";
try {
$rooms = $hc->get_rooms();
foreach ($rooms->rooms as $room) {
echo " - $room->room_id = $room->name\n";
foreach ($rooms as $room) {
echo "Room $room->room_id\n";
echo " - Name: $room->name\n";
$room_data = $hc->get_room($room->room_id);
echo " - Participants: ".count($room_data->participants)."\n";
}
} catch (HipChat_Exception $e) {
echo "Oops! Error: ".$e->getMessage();
}

// get users
echo "\nHitting /users.json\n";
echo "\nUsers:\n";
try {
$users = $hc->get_users();
foreach ($users->users as $user) {
echo " - $user->user_id = $user->name\n";
foreach ($users as $user) {
echo "User $user->user_id\n";
echo " - Name: $user->name\n";
echo " - Email: $user->email\n";
$user_data = $hc->get_user($user->user_id);
echo " - Status: ".$user_data->status."\n";
}
} catch (HipChat_Exception $e) {
echo "Oops! Error: ".$e->getMessage();
Expand Down

0 comments on commit 8696174

Please sign in to comment.