Skip to content

Instantly share code, notes, and snippets.

@bhubbard
Created November 27, 2024 21:15
Show Gist options
  • Save bhubbard/32a7dd1cd823d15d43c127e7c9124271 to your computer and use it in GitHub Desktop.
Save bhubbard/32a7dd1cd823d15d43c127e7c9124271 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: WP Gemini API
* Plugin URI: https://example.com/wp-gemini-api
* Description: A WordPress plugin that provides an API wrapper for Gemini.
* Version: 1.0.0
* Author: Your Name
* Author URI: https://example.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-gemini-api
* Domain Path: /languages
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WP_Gemini_API' ) ) :
/**
* Main WP_Gemini_API Class.
*
* @since 1.0.0
*/
final class WP_Gemini_API {
/**
* Version of the plugin.
*
* @var string
*/
public $version = '1.0.0';
/**
* Instance of this class.
*
* @var object
*/
protected static $instance = null;
/**
* The Gemini API base URL.
*
* @var string
*/
public $api_base_url = 'https://api.gemini.com/v1/';
/**
* Gemini API credentials.
*
* @var array
*/
private $credentials;
/**
* Initialize the plugin public actions.
*/
private function __construct() {
// Load plugin text domain.
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
// Set the Gemini API credentials.
$this->credentials = array(
'api_key' => 'YOUR_GEMINI_API_KEY', // Replace with your Gemini API key
'api_secret' => 'YOUR_GEMINI_API_SECRET', // Replace with your Gemini API secret
);
// Register any necessary hooks or actions.
}
/**
* Return an instance of this class.
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Load the plugin text domain for translation.
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'wp-gemini-api', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Magic getter for our object. Allows getting but not setting.
*
* @param string $field
* @return mixed
*/
public function __get( $field ) {
return $this->$field;
}
/**
* Generate a Gemini API signature.
*
* @param array $request_payload The request payload.
* @return string The base64 encoded signature.
*/
private function generate_signature( $request_payload ) {
$encoded_payload = base64_encode( json_encode( $request_payload ) );
$signature = hash_hmac( 'sha384', $encoded_payload, $this->credentials['api_secret'] );
return base64_encode( $signature );
}
/**
* Make a request to the Gemini API.
*
* @param string $endpoint The API endpoint.
* @param string $method The HTTP method (GET, POST).
* @param array $params The request parameters.
* @return array The API response.
*/
public function make_request( $endpoint, $method = 'GET', $params = array() ) {
$url = $this->api_base_url . $endpoint;
// Prepare the request payload.
$request_payload = array(
'request' => '/' . $endpoint,
'nonce' => time(),
);
if ( 'POST' === $method ) {
$request_payload = array_merge( $request_payload, $params );
}
// Generate the API signature.
$signature = $this->generate_signature( $request_payload );
$args = array(
'method' => $method,
'headers' => array(
'Content-Type' => 'text/plain',
'X-GEMINI-APIKEY' => $this->credentials['api_key'],
'X-GEMINI-PAYLOAD' => base64_encode( json_encode( $request_payload ) ),
'X-GEMINI-SIGNATURE' => $signature,
),
);
if ( 'GET' === $method ) {
$url = add_query_arg( $params, $url );
}
$response = wp_remote_request( $url, $args );
if ( is_wp_error( $response ) ) {
// Handle WP_Error.
error_log( 'Gemini API Error: ' . $response->get_error_message() );
return $response;
}
$status_code = wp_remote_retrieve_response_code( $response );
if ( $status_code < 200 || $status_code >= 300 ) {
// Handle API errors.
$error_message = wp_remote_retrieve_body( $response );
error_log( 'Gemini API Error (' . $status_code . '): ' . $error_message );
return new WP_Error( 'gemini_api_error', $error_message, array( 'status_code' => $status_code ) );
}
return json_decode( wp_remote_retrieve_body( $response ) );
}
/**
* Get current order book.
*
* @param string $symbol The symbol of the trading pair (e.g., btcusd).
* @return array The order book data.
*/
public function get_order_book( $symbol ) {
return $this->make_request( 'book/' . $symbol );
}
/**
* Get your current balances.
*
* @return array The balances data.
*/
public function get_balances() {
return $this->make_request( 'balances', 'POST' );
}
// Add other helper methods for specific Gemini API functionalities, e.g.,:
// public function get_ticker( $symbol ) { ... }
// public function place_order( $symbol, $amount, $price, $side ) { ... }
// public function get_order_status( $order_id ) { ... }
// ... and so on
}
endif;
/**
* Returns the main instance of WP_Gemini_API.
*
* @return WP_Gemini_API The main instance.
*/
function WP_Gemini_API() {
return WP_Gemini_API::get_instance();
}
// Get WP_Gemini_API Running.
WP_Gemini_API();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment