Skip to content

Instantly share code, notes, and snippets.

@esod
Last active August 29, 2015 14:16
Show Gist options
  • Save esod/d3dc951785caceab362a to your computer and use it in GitHub Desktop.
Save esod/d3dc951785caceab362a to your computer and use it in GitHub Desktop.
Guzzle and Symfony's finder in Drupal 7 blocks
{
"name": "esod/d7.local",
"require": {
"guzzlehttp/guzzle": "~5.2",
"symfony/finder": "2.6.6"
},
"authors": [
{
"name": "Eric Sod",
"email": "esod124@gmail.com"
}
]
}
name = Guzzle Services
description = A <a href="https://github.com/guzzle/guzzle" target="_blank">guzzle</a> integration module for Drupal 7.
core = 7.x
package = Custom
version = 7.x-1.0
configure = admin/structure/block
<?php
/**
* @file
* A guzzle integration module for Drupal 7.
*/
require DRUPAL_ROOT . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Post\PostFile;
/**
* Implements hook_help().
*/
function guzzle_services_help($path, $arg) {
switch ($path) {
case "admin/help#guzzle_services":
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('A <a href="@composer" target="_blank">Composer</a> integration module featuring <a href="@guzzle" target="_blank">guzzle</a> and <a href="@finder" target="_blank">symfony/finder</a> for Drupal 7.', array(
'@composer' => 'https://getcomposer.org/',
'@guzzle' => 'https://github.com/guzzle/guzzle',
'@finder' => 'http://symfony.com/doc/current/components/finder.html',
)) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<p>' . t('The Guzzle block arbitrarily guzzles <a href="@github" target="_blank">github\'s api</a>.', array("@github" => "https://api.github.com")) . '</p>';
$output .= '<p>' . t('The Finder block arbitrarily prints a list of .gif files uploaded in the last day.').'</p>';
return $output;
break;
}
}
/**
* Implements hook_block_info().
*/
function guzzle_services_block_info() {
$blocks['get_client'] = array(
'info' => t('Get Client'),
'cache' => DRUPAL_CACHE_PER_ROLE,
);
$blocks['put_client'] = array(
'info' => t('Put Client'),
'cache' => DRUPAL_CACHE_PER_ROLE,
);
$blocks['post_client'] = array(
'info' => t('Post Client'),
'cache' => DRUPAL_CACHE_PER_ROLE,
);
$blocks['current_files'] = array(
'info' => t('Current files'),
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*
* Prepares the contents of the block.
*
* @param $delta
* @return Array
*/
function guzzle_services_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'get_client':
$block['subject'] = t('Get Client');
$results = get_client();
foreach ($results as $key => $value) {
$items[] = array(
'data' => l(t($key), $value, array('attributes' => array('class' => 'about-link another-class'))),
);
}
if (empty($items)) {
$block['content'] = t('Nothing to get.');
}
else {
// Pass data through theme function.
$block['content'] = theme('item_list', array(
'items' => $items,
));
}
break;
case 'put_client':
$block['subject'] = t('Put Client');
$results = put_client();
$json = json_decode($results['data'], true);
foreach ($json as $key => $value) {
$items[] = array(
'data' => l(t($key), $value, array('attributes' => array('class' => 'about-link another-class'))),
);
}
if (empty($items)) {
$block['content'] = t('Nothing to put.');
}
else {
// Pass data through theme function.
$block['content'] = theme('item_list', array(
'items' => $items,
));
}
break;
case 'post_client':
$block['subject'] = t('Post Client');
$results = post_client();
$json = json_decode($results['data'], true);
foreach ($json as $key => $value) {
$items[] = array(
'data' => l(t($key), $value, array('attributes' => array('class' => 'about-link another-class'))),
);
}
if (empty($items)) {
$block['content'] = t('Nothing to post.');
}
else {
// Pass data through theme function.
$block['content'] = theme('item_list', array(
'items' => $items,
));
}
break;
case 'current_files':
$block['subject'] = t('Current Files');
$results = get_current_files();
foreach ($results as $key => $value) {
$items[] = array(
'data' => l(t($value), 'sites/default/files/field/image/'.$value, array('attributes' => array('class' => 'about-link another-class'))),
);
}
if (empty($items)) {
$block['content'] = t('No Current files for you.');
}
else {
// Pass data through theme function.
$block['content'] = theme('item_list', array(
'items' => $items,
));
}
break;
}
return $block;
}
/**
* A utility function to get github's api in a json array.
*/
function get_client() {
$client = new Client();
$files = $client->get('https://api.github.com');
$files = $files->json();
return $files;
}
/**
* A utility function for put requests to httpbin.org/put api in a json array.
*/
function put_client() {
$client = new Client();
// $response = $client->post('http://httpbin.org/post', ['body' => 'raw data']);
$response = $client->put('http://httpbin.org/put', ['json' => ['foo' => 'bar']]);
$json = $response->json();
// var_dump($json);die;
return $json;
}
/**
* A utility function for post requests to httpbin.org/put api in a json array.
*/
function post_client() {
$client = new Client();
$body['grant_type'] = "client_credentials";
$body['client_id'] = '1234';
$body['client_secret'] = 'super secret';
$body['file_filed'] = '/sites/all/modules/custom/guzzle_services/sample_users.json';
$request = $client->post('http://httpbin.org/post', ['body' => json_encode($body) ]);
$code = $request->getStatusCode();
if ($code == 200) {
$result = $request->json();
}
return $result;
}
/**
* A utility function to return the array of current SPLFileInfo objects
*/
function get_current_files() {
// the "files directory
$dir = drupal_realpath(file_default_scheme() . '://');
$finder = new \Symfony\Component\Finder\Finder();
$finder->in($dir)
->name('*.gif')
->date('since 1 day ago')
;
$files = array();
foreach ($finder as $file) {
$files[] = $file->getFilename();
}
return $files;
// return array('foo');
}
Copy link

ghost commented May 27, 2015

What's the name of the first file and where do you put it in your Drupal project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment