Skip to content

Instantly share code, notes, and snippets.

@IMSoP
Forked from mikeschinkel/Apis.php
Last active May 14, 2020 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IMSoP/363eaf54ec1894106f83d5e3f2851507 to your computer and use it in GitHub Desktop.
Save IMSoP/363eaf54ec1894106f83d5e3f2851507 to your computer and use it in GitHub Desktop.
Examples reworked to use possible try-catch extensions
<?php
class Apis {
static function JsonGET( string $api_url, array $args = array() ) {
try {
$wp_error = null;
$args = wp_parse_args( $args, array(
'response_type' => ARRAY_A,
) );
$response = null;
$message = sprintf( "with request URL='%s'", $api_url );
$response = wp_remote_get( $api_url );
if ( is_wp_error( $response ) ) {
/**
* @var WP_Error $response
*/
$wp_error = $response;
$messages = implode( '; ', $response->get_error_messages() );
throw sprintf( "%s: %s", $message, $messages );
}
if ( ! $body = wp_remote_retrieve_body( $response ) ) {
throw "response body is empty";
}
$response = json_decode( $body, true, 512, JSON_UNESCAPED_SLASHES );
if ( is_null( $response ) ) {
$body = @Strings::strip_newlines( $body );
throw sprintf( "invalid JSON returned in body: %s", $body );
}
if ( $args[ 'response_type' ] === ARRAY_A && ! is_array( $response ) ) {
$body = @Strings::strip_newlines( $body );
throw sprintf( "JSON returned in body was not an array: %s", $body );
}
if ( $args[ 'response_type' ] === OBJECT && ! is_object( $response ) ) {
$body = @Strings::strip_newlines( $body );
throw sprintf( "JSON returned in body was not an object: %s", $body );
}
} catch ( string $message ) ) {
Errors::log( "API request failed for URL '%s: %s",
$api_url,
$message
);
$response = is_null($wp_error)
? new WP_Error( -1, $message )
: $wp_error;
}
return $response;
}
}
<?php
Class Objects {
/**
* @param WP_Post $post
* @param array $args
* @return |null
*/
static function load_post( $post, array $args = array() ) {
try {
$object = $reason = null;
$post = get_post( $post );
if ( empty( $post ) ) {
throw 'post is empty';
}
$class_name = PostTypes::get_post_type_class( $post->post_type );
if ( empty( $post ) ) {
throw sprintf( "post type '%s' does not have an associated PHP class",
$post->post_type
);
}
$factory_class = Reflect::get_class_constant( $class_name, WellKnown::FACTORY_CLASS );
if ( empty( $post ) ) {
throw sprintf( "class '%s' for post type '%s' does not have a FACTORY_CLASS constant",
$class_name,
$post->post_type
);
}
if ( ! method_exists( $factory_class, Methods::make_new_instance ) ) {
throw sprintf( "factory class '%s' for class '%s' for post type '%s' does not have a %s() method",
$factory_class,
$class_name,
$post->post_type,
Methods::make_new_instance
);
}
$args[ Instances::post] = $post;
$object = $factory_class::make_new_instance( $args );
} catch ( $reason ) {
Errors::notify( "failed call to %s() called from %s because %s: %s",
Methods::make_new_instance,
Caller::trace( 1 ),
$reason,
$post
);
}
return $object;
}
}
<?php
class OfferingCard
function expiration_date():?string {
$value = $should_show = null;
try {
if ( ! $offering = $this->contained_offering() ) {
throw;
}
$value = $offering->expiration_date();
/**
* @todo Shouldn't this be in the the_approval_number() method?
* As is there is no want to get the approval number if needed
* for some other reason that showing it. - Mike
*/
$should_show = $offering->should_show_expiration_date();
}
return ( $value && $should_show )
? date_format( $value, 'M j, Y | h:i a e' )
: null;
}
}
<?php
class Php {
static function call_context( CallContext $call_context, bool $trigger_error = true ) {
try {
$result = null;
if ( ! $call_context->can_call() ) {
throw;
}
$result = $call_context->call();
}
catch(void) {
if ( $trigger_error ) {
Errors::notify( 'invalid callable %s() when called by %s()',
$call_context->representation(),
Caller::trace(1)
);
}
}
return $result;
}
}
<?php
class YouTube implements Serializable {
use Traits\Serializable;
const KEY_PREFIX = 'YT'; // Should be short
/**
* @param string $video_id
*
* @return Listing\Item
*/
/**
* @const int — 24 hours
*/
const CACHE_DURATION = 24 * 60 * 60;
static function fetch_item( string $video_id ): ?Videos\Listing\Item {
try {
$response = null;
$api_url = Videos\Api::get_url( [ 'id' => $video_id ] );
$transient_key = Transients\TransientKey::make_new( self::KEY_PREFIX, $api_url );
/*if ( $video = Transients\Transients::get( $transient_key ) ) {
break;
}*/
$video = null;
$response = Apis::JsonGET( $api_url );
if ( is_wp_error( $response ) ) {
throw;
}
if ( isset( $response[ 'error' ] ) ) {
throw;
}
$video_list = new Videos\Listing\Response( $response );
if ( ! $video_list->has_items() ) {
throw;
}
$video = $video_list->get_item( 0 );
if ( false === $video ) {
$video = null;
throw;
}
Transients\Transients::set( $transient_key, $video, self::CACHE_DURATION );
}
catch(void) {
Errors::log( "error when calling YouTube API '%s': %s",
$api_url,
$response
);
}
return $video;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment