Skip to content

Commit

Permalink
[smarcet]
Browse files Browse the repository at this point in the history
* meetup pro api auth update ( from api key to oauth2)
  • Loading branch information
smarcet committed Jul 17, 2019
1 parent 72be330 commit 99f65b5
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 17 deletions.
134 changes: 123 additions & 11 deletions events/code/infrastructure/services/MeetupApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,134 @@ final class MeetupApi implements IExternalEventsApi
{

const BaseUrl = 'https://api.meetup.com';
const BaseAuthUrl = 'https://secure.meetup.com';

/**
* @var string
* @var Client
*/
private $apiKey;
private $client;

/**
* @var Client
* @param string $key
* @return CacheProxy|Zend_Cache_Core
*/
private $client;
private function getCache($key = 'all')
{
return SS_Cache::factory(strtolower(get_class($this)) . '_MeetupApi_'.strtolower($key));
}

/**
* @param string $apiKey
* @param $key
* @return mixed|null
*/
public function setApiKey(string $apiKey)
private function loadRAWFromCache($key)
{
$this->apiKey = $apiKey;
if ($result = $this->getCache()->load(md5($key))) {
return unserialize($result);
}
return null;
}

/**
* @param $key
* @param $data
* @param $lifetime
* @throws Zend_Cache_Exception
*/
private function saveRAW2Cache($key, $data, $lifetime)
{
$this->getCache()->save(serialize($data), md5($key), [], $lifetime);
}

/**
* @return string
* @throws Exception
*/
private function getAccessToken():string {

$access_token = $this->loadRAWFromCache("access_token");
if(!empty($access_token)) return $access_token;
$response = $this->client->get(self::BaseAuthUrl."/oauth2/authorize" , [
'headers' => [
'Accept' => 'application/json'
],
'query' => [
'client_id' => MEETUP_OAUTH_CLIENT_ID,
'redirect_uri' => MEETUP_OAUTH_REDIRECT_URL,
'response_type' => 'anonymous_code',
]
]);

if($response->getStatusCode() !== 200)
throw new Exception('invalid status code!');
$content_type = $response->getHeaderLine('content-type');
if(empty($content_type))
throw new Exception('invalid content type!');
if(!strstr($content_type,'application/json'))
throw new Exception('invalid content type!');

$json_content = $response->getBody()->getContents();
$json_response = json_decode($json_content, true);
if(!key_exists('code', $json_response))
throw new InvalidArgumentException();

$code = $json_response['code'];


$response = $this->client->post(self::BaseAuthUrl."/oauth2/access" , [
'headers' => [
'Accept' => 'application/json'
],
'query' => [
'client_id' => MEETUP_OAUTH_CLIENT_ID,
'client_secret' => MEETUP_OAUTH_CLIENT_SECRET,
'redirect_uri' => MEETUP_OAUTH_REDIRECT_URL,
'grant_type' => 'anonymous_code',
'code' => $code,
]
]);

if($response->getStatusCode() !== 200)
throw new Exception('invalid status code!');
$content_type = $response->getHeaderLine('content-type');
if(empty($content_type))
throw new Exception('invalid content type!');
if(!strstr($content_type,'application/json'))
throw new Exception('invalid content type!');

$json_content = $response->getBody()->getContents();
$json_response = json_decode($json_content, true);
if(!key_exists('access_token', $json_response))
throw new InvalidArgumentException();

$access_token = $json_response['access_token'];

$response = $this->client->post(self::BaseUrl."/sessions" , [
'headers' => [
'Accept' => 'application/json',
'Authorization' => sprintf("Bearer %s", $access_token)
],
'query' => [
'email' => MEETUP_USER,
'password' => MEETUP_PASSWORD,
]
]);

if($response->getStatusCode() !== 200)
throw new Exception('invalid status code!');
$content_type = $response->getHeaderLine('content-type');
if(empty($content_type))
throw new Exception('invalid content type!');
if(!strstr($content_type,'application/json'))
throw new Exception('invalid content type!');

$json_content = $response->getBody()->getContents();
$json_response = json_decode($json_content, true);
if(!key_exists('oauth_token', $json_response))
throw new InvalidArgumentException();
$access_token = $json_response['oauth_token'];
$this->saveRAW2Cache('access_token', $access_token, intval($json_response['expires_in']) * 0.9);
return $access_token;
}

public function __construct()
Expand Down Expand Up @@ -73,8 +184,9 @@ public function getGroups(int $page = 20):array{

$query = [
'page' => $page,
'key' => $this->apiKey
'access_token' => $this->getAccessToken()
];

$api_url = sprintf("%s/self/groups", self::BaseUrl);
$response = $this->client->get($api_url, array
(
Expand Down Expand Up @@ -104,9 +216,9 @@ public function getGroups(int $page = 20):array{
public function getGroupIncomingEvents(string $groupSlug, int $page = 20):array {

$query = [
'page' => $page,
'key' => $this->apiKey,
'status' => 'upcoming'
'page' => $page,
'access_token' => $this->getAccessToken(),
'status' => 'upcoming'
];
$api_url = sprintf("%s/%s/events", self::BaseUrl, $groupSlug);
$response = $this->client->get($api_url, array
Expand Down
7 changes: 5 additions & 2 deletions events/code/model/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ final class EventManager implements IEventManager {
*/
private $validator_factory;

/**
* @var IExternalEventsApi|null
*/
private $external_event_api;

/**
* @param IEntityRepository $event_repository
* @param IEventRegistrationRequestFactory $factory
Expand All @@ -67,8 +72,6 @@ public function __construct(?IEntityRepository $event_repository,
$this->event_publishing_service = $event_publishing_service;
$this->validator_factory = $validator_factory;
$this->external_event_api = $external_event_api;
if(!is_null($this->external_event_api))
$this->external_event_api->setApiKey(MEETUP_API_KEY);
}

public function toggleSummitEvent($id){
Expand Down
4 changes: 0 additions & 4 deletions events/code/model/IExternalEventsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,4 @@ interface IExternalEventsApi
*/
public function getAllUpcomingEvents(int $pageSize):array;

/**
* @param string $apiKey
*/
public function setApiKey(string $apiKey);
}
6 changes: 6 additions & 0 deletions sample._ss_environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@

define('SURVEY_ANALYTICS_URL', '');

define('MEETUP_OAUTH_CLIENT_ID' ,'');
define('MEETUP_OAUTH_REDIRECT_URL' ,'');
define('MEETUP_OAUTH_CLIENT_SECRET' ,'');
define('MEETUP_USER' ,'');
define('MEETUP_PASSWORD' ,'');

global $_FILE_TO_URL_MAPPING;
$_FILE_TO_URL_MAPPING[''] = '';

0 comments on commit 99f65b5

Please sign in to comment.