Skip to content

Commit

Permalink
add error logging to auphonic curling
Browse files Browse the repository at this point in the history
  • Loading branch information
eteubert committed Aug 11, 2013
1 parent 0114b2e commit 25aa60c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
22 changes: 22 additions & 0 deletions lib/http/curl.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Podlove\Http;

use Podlove\Log;

/**
* Wrapper for WordPress' WP_Http_Curl class.
*/
Expand Down Expand Up @@ -30,6 +32,26 @@ public function request( $url, $params ) {
$params = wp_parse_args( $params, $defaults );

$this->response = $this->curl->request( $url, $params );

if ( is_wp_error($this->response) ) {
Log::get()->addError( 'Curl error', array(
'url' => $url,
'error' => $this->response->get_error_message()
) );
} elseif (substr($this->response['response']['code'], 0, 1) >= 4) {
Log::get()->addError( 'Curl error', array(
'url' => $url,
'response code' => $this->response['response']['code']
) );
}
}

public function isSuccessful()
{
return
$this->response && // request has been made
!is_wp_error($this->response) && // there was no error
substr($this->response['response']['code'], 0, 1) < 4; // 1xx 2xx or 3xx
}

public function get_response() {
Expand Down
60 changes: 34 additions & 26 deletions lib/modules/auphonic/auphonic.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Podlove\Modules\Auphonic;
use \Podlove\Model;
use \Podlove\Http;

class Auphonic extends \Podlove\Modules\Base {

Expand Down Expand Up @@ -99,19 +100,23 @@ public function fetch_authorized_user() {
if ( ! ( $token = $this->get_module_option('auphonic_api_key') ) )
return "";

$ch = curl_init( 'https://auphonic.com/api/user.json' );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "GET" );
curl_setopt( $ch, CURLOPT_USERAGENT, \Podlove\Http\Curl::user_agent() );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Authorization: Bearer ' . $this->get_module_option('auphonic_api_key') )
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$decoded_user = json_decode( curl_exec( $ch ) );

$user = $decoded_user ? $decoded_user : FALSE;
set_transient( $cache_key, $user, 60*60*24*365 ); // 1 year, we devalidate manually
return $user;
$curl = new Http\Curl();
$curl->request( 'https://auphonic.com/api/user.json', array(
'headers' => array(
'Content-type' => 'application/json',
'Authorization' => 'Bearer ' . $this->get_module_option('auphonic_api_key')
)
) );
$response = $curl->get_response();

if ($curl->isSuccessful()) {
$decoded_user = json_decode( $response['body'] );
$user = $decoded_user ? $decoded_user : FALSE;
set_transient( $cache_key, $user, 60*60*24*365 ); // 1 year, we devalidate manually
return $user;
} else {
return false;
}
}
}

Expand All @@ -131,20 +136,23 @@ public function fetch_presets() {
if ( ! ( $token = $this->get_module_option('auphonic_api_key') ) )
return "";

$ch = curl_init( 'https://auphonic.com/api/presets.json' );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "GET" );
curl_setopt( $ch, CURLOPT_USERAGENT, \Podlove\Http\Curl::user_agent() );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Authorization: Bearer ' . $this->get_module_option('auphonic_api_key') )
);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

$decoded_presets = json_decode( curl_exec( $ch ) );
$curl = new Http\Curl();
$curl->request( 'https://auphonic.com/api/presets.json', array(
'headers' => array(
'Content-type' => 'application/json',
'Authorization' => 'Bearer ' . $this->get_module_option('auphonic_api_key')
)
) );
$response = $curl->get_response();

if ($curl->isSuccessful()) {
$presets = json_decode( $response['body'] );
set_transient( $cache_key, $presets, 60*60*24*365 ); // 1 year, we devalidate manually
return $presets;
} else {
return array();
}

$presets = $decoded_presets ? $decoded_presets : FALSE;
set_transient( $cache_key, $presets, 60*60*24*365 ); // 1 year, we devalidate manually
return $presets;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/modules/podlove_web_player/printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function render() {
$sorted_files = array();
$preferred_order = array( 'audio/mp4', 'audio/aac', 'audio/opus', 'audio/ogg', 'audio/vorbis' );
foreach ( $preferred_order as $order_key ) {
if ( $media_files[ $order_key ] ) {
if ( isset($media_files[ $order_key ]) && $media_files[ $order_key ] ) {
$sorted_files[] = $media_files[ $order_key ];
unset($media_files[ $order_key ]);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
namespace Podlove;
use \Podlove\Model;

define( __NAMESPACE__ . '\DATABASE_VERSION', 44 );
define( __NAMESPACE__ . '\DATABASE_VERSION', 45 );

add_action( 'init', function () {

Expand Down Expand Up @@ -348,6 +348,10 @@ function run_migrations_for_version( $version ) {
'DELETE FROM `' . $wpdb->postmeta . '` WHERE meta_key = "last_validated_at"'
);
break;
case 45:
delete_transient('podlove_auphonic_user');
delete_transient('podlove_auphonic_presets');
break;
}

}

0 comments on commit 25aa60c

Please sign in to comment.