<commit_before><?php

/**
 * PHP Gumroad API Wrapper
 *
 * @package Gumroad
 * @author  <NAME> <<EMAIL>>
 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
 */

define( 'GUMROAD_API_URL', 'https://gumroad.com/api/v1' );
define( 'GUMROAD_API_TIMEOUT', 3000 );

class Gumroad_Client
{
	public $requester;
	public $sessions;
	public $links;

	public function __construct() {
		$this->requester = new Gumroad_Requester();
		$this->sessions = new Gumroad_Sessions_Endpoint($this->requester);
		$this->links = new Gumroad_Links_Endpoint($this->requester);
	}

	//shortcut for auth
	public function auth( $email, $password ) {
		$params = array('email' => $email, 'password' => $password);
		return $this->sessions->authenticate($params);
	}

	//shortcut for deauth
	public function deauth() {
		return $this->sessions->deauthenticate();
	}

	public function __destruct() {
		if( $this->requester->token ) {
			$this->deauth();
		}
	}
}

class Gumroad_Requester 
{
	public $timeout;
	public $token;

	public function __construct( $timeout=GUMROAD_API_TIMEOUT ) {
		$this->timeout = $timeout;
		$this->token = null;
	}

	public function request( $method, $url, $params = array() ) {
		//Start request
        $ch   = curl_init();
        $params['token'] = $this->token;
        $query = http_build_query($params);

        switch ( strtoupper($method) ) {
        	case 'HEAD': 
        		curl_setopt($ch, CURLOPT_NOBODY, true);
        		break;
            case 'GET':
                if ( !empty($query) ) {
                    $url = $url . '?' . $query;
                }
                curl_setopt($ch, CURLOPT_HTTPGET, true);
                break;
            case 'POST':
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
                break;
            default:
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
                break;
        }

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout);
        # End of build request

        $response = curl_exec($ch);
        
        # Check response for errors
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ( $httpCode >= 300 ) {
            throw new Gumroad_Exception($httpCode);
        } 

        $curlError = curl_error($ch);
        if ( $curlError ) {
            throw new Gumroad_Exception($curlError);
        }

        $response = json_decode($response);
        if ( !$response->success ) {
            throw new Gumroad_Exception($response->error->message);
        }

        return $response;
    }
}

abstract class Gumroad_EndpointAbstract
{
	public $url;
	public $requester;

	public function __construct( $requester, $url=GUMROAD_API_URL ) {
		$this->requester = $requester;
		$this->url = $url;
	}
}

class Gumroad_Sessions_Endpoint extends Gumroad_EndpointAbstract
{
	public function __construct( $requester, $path='/sessions' ) {
		parent::__construct($requester);
		$this->url = $this->url . $path;
	}

	public function authenticate( $params ) {
		$response = $this->requester->request('POST', $this->url, $params);
		$this->requester->token = $response->token;
		return $response;

	}

	public function deauthenticate() {
		$response = $this->requester->request('DELETE', $this->url);
		$this->requester->token = null;
		return $response;
	}
}

class Gumroad_Links_Endpoint extends Gumroad_EndpointAbstract
{
	public function __construct( $requester, $path='/links' ) {
		parent::__construct($requester);
		$this->url = $this->url . $path;
	}

	public function createLink( $params ) {
		return $this->requester->request('POST', $this->url, $params);
	}

	public function editLink( $id, $params ) {
		return $this->requester->request('PUT', $this->url . '/' . $id, $params);
	}

	public function deleteLink( $id ) {
		return $this->requester->request('DELETE', $this->url . '/' . $id);
	}

	public function enableLink( $id ) {
		return $this->requester->request('PUT', $this->url . '/' . $id . '/enable');
	}

	public function disableLink( $id ) {
		return $this->requester->request('PUT', $this->url . '/' . $id . '/disable');
	}

	public function getLink( $id ) {
		return $this->requester->request('GET', $this->url . '/' . $id);
	}

	public function getLinks() {
		return $this->requester->request('GET', $this->url);
	}
}

class Gumroad_Exception extends Exception
{

}

?><commit_msg>phpdoc documentation<commit_after><?php
/**
 * Gumroad PHP Client Library
<?php
/**
 * Gumroad PHP Client Library
 *
 * PHP wrapper for Gumroad API. Originally developed for Ultiworld.
 * 
 * @package Gumroad
 * @author  <NAME> <<EMAIL>>
 * @license http://opensource.org/licenses/mit-license.php The MIT License
 */

/** base API URL */
define( 'GUMROAD_API_URL', 'https://gumroad.com/api/v1' );
/** default timeout to use with cURL */
define( 'GUMROAD_API_TIMEOUT', 5000 );

/**
 * Gumroad Client Class
 * 
 * Core class used to access all Gumroad functionality
 */
class Gumroad_Client
{
	/**
	 * A requester object to pass between the various endpoint objects
	 * 
	 * @var Gumroad_Requester
	 */
	public $requester;

	/**
	 * The API endpoint for /sessions
	 * 
	 * @var Gumroad_Sessions_Endpoint
	 */
	public $sessions;

	/**
	 * The API endpoint for /links
	 * 
	 * @var Gumroad_Links_Endpoint
	 */
	public $links;

	/**
	 * Initializes a Gumroad_Client object
	 * 
	 * Creates a new requester object for the client to use and passes it to the endpoint objects
	 */
	public function __construct() {
		$this->requester = new Gumroad_Requester();
		$this->sessions = new Gumroad_Sessions_Endpoint($this->requester);
		$this->links = new Gumroad_Links_Endpoint($this->requester);
	}

	/**
	 * Shortcut function for authentication
	 * 
	 * @param string $email
	 * @param string $password
	 * @return stdClass $response
	 */
	public function auth( $email, $password ) {
		$params = array('email' => $email, 'password' => $password);
		return $this->sessions->authenticate($params);
	}

	/**
	 * Shortcut function for deauthentication
	 * 
	 * @return stdClass $response
	 */
	public function deauth() {
		return $this->sessions->deauthenticate();
	}

	/**
	 * Ensures authenticated sessions are deauthenticated on destruction
	 */
	public function __destruct() {
		if( $this->requester->token ) {
			$this->deauth();
		}
	}
}

/**
 * Gumroad Requester Class
 * 
 * Executes HTTP requests with cURL
 * Uses session token (if one is stored)
 */
class Gumroad_Requester 
{
	/**
	 * Timeout (milliseconds) to use with cURL requests
	 * 
	 * @var int
	 */
	public $timeout;

	/**
	 * Stored session token
	 * 
	 * @var mixed
	 */
	public $token;

	/**
	 * Initializes Gumroad_Requester object
	 * 
	 * Sets $timeout value and empty $token
	 * @param int $timeout defaults to GUMROAD_API_TIMEOUT
	 */
	public function __construct( $timeout=GUMROAD_API_TIMEOUT ) {
		$this->timeout = $timeout;
		$this->token = null;
	}

	/**
	 * Build and execute HTTP request
	 * 
	 * @param string $method
	 * @param string $url
	 * @param array $params
	 * @return stdClass $response
	 */
	public function request( $method, $url, $params = array() ) {
		//Start request
		$ch   = curl_init();
		$params['token'] = $this->token;
		$query = http_build_query($params);

		switch ( strtoupper($method) ) {
			case 'HEAD': 
				curl_setopt($ch, CURLOPT_NOBODY, true);
				break;
			case 'GET':
				if ( !empty($query) ) {
					$url = $url . '?' . $query;
				}
				curl_setopt($ch, CURLOPT_HTTPGET, true);
				break;
			case 'POST':
				curl_setopt($ch, CURLOPT_POST, true);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
				break;
			default:
				curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
				break;
		}

		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout);
		# End of build request

		$response = curl_exec($ch);
		
		# Check response for errors
		$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		if ( $httpCode >= 300 ) {
			throw new Gumroad_Exception($httpCode);
		} 

		$curlError = curl_error($ch);
		if ( $curlError ) {
			throw new Gumroad_Exception($curlError);
		}

		$response = json_decode($response);
		if ( !$response->success ) {
			throw new Gumroad_Exception($response->error->message);
		}

		return $response;
	}
}

/**
 * Gumroad Endpoint Abstract Class
 * 
 * Stores the endpoint URL and a requester object
 */
abstract class Gumroad_EndpointAbstract
{
	/**
	 * Endpoint URL
	 * 
	 * @var string
	 */
	public $url;

	/**
	 * Requester object
	 * 
	 * @var Gumroad_Requester
	 */
	public $requester;

	/**
	 * Initializes Gumroad_EndpointAbstract object
	 * 
	 * Should be called by child classes to set the requester object and base API URL
	 * @param Gumroad_Requester $requester
	 * @param string $url defaults to GUMROAD_API_URL
	 */
	public function __construct( $requester, $url=GUMROAD_API_URL ) {
		$this->requester = $requester;
		$this->url = $url;
	}
}

/**
 * Gumroad Sessions Endpoint Class
 * 
 * Used in conjunction with a Gumroad_Requester object to access the /sessions endpoint
 */
class Gumroad_Sessions_Endpoint extends Gumroad_EndpointAbstract
{
	/**
	 * Initializes a Gumroad_Sessions_Endpoint object
	 * 
	 * Calls parent constructor and adds on endpoint path to base API URL
	 * @param Gumroad_Requester $requester
	 * @param string $path defaults to '/sessions'
	 */
	public function __construct( $requester, $path='/sessions' ) {
		parent::__construct($requester);
		$this->url = $this->url . $path;
	}

	/**
	 * Sends request to authenticate a new session 
	 * 
	 * @param array $params
	 * @return stdClass $response
	 */
	public function authenticate( $params ) {
		$response = $this->requester->request('POST', $this->url, $params);
		$this->requester->token = $response->token;
		return $response;

	}

	/**
	 * Sends request to deautheticate the active session
	 * 
	 * @return stdClass $response
	 */
	public function deauthenticate() {
		$response = $this->requester->request('DELETE', $this->url);
		$this->requester->token = null;
		return $response;
	}
}

/**
 * Gumroad Links Endpoint Class
 * 
 * Used in conjunction with a Gumroad_Requester object to access the /links endpoint
 */
class Gumroad_Links_Endpoint extends Gumroad_EndpointAbstract
{
	/**
	 * Initializes a Gumroad_Links_Endpoint object
	 * 
	 * Calls parent constructor and adds on endpoint path to base API URL
	 * @param Gumroad_Requester $requester
	 * @param string $path defaults to '/links'
	 */
	public function __construct( $requester, $path='/links' ) {
		parent::__construct($requester);
		$this->url = $this->url . $path;
	}

	/**
	 * Sends request to get all links
	 * 
	 * @return stdClass $response
	 */
	public function getLinks() {
		return $this->requester->request('GET', $this->url);
	}

	/**
	 * Sends request to get a link
	 * 
	 * @param string $id
	 * @return stdClass $response
	 */
	public function getLink( $id ) {
		return $this->requester->request('GET', $this->url . '/' . $id);
	}

	/**
	 * Sends request to create a link
	 * 
	 * @param array $params
	 * @return stdClass $response
	 */
	public function createLink( $params ) {
		return $this->requester->request('POST', $this->url, $params);
	}

	/**
	 * Sends request to edit a link
	 * 
	 * @param string $id
	 * @param array $params
	 * @return stdClass $response
	 */
	public function editLink( $id, $params ) {
		return $this->requester->request('PUT', $this->url . '/' . $id, $params);
	}

	/**
	 * Sends request to enable (aka publish) a link
	 * 
	 * @param string $id
	 * @return stdClass $response
	 */
	public function enableLink( $id ) {
		return $this->requester->request('PUT', $this->url . '/' . $id . '/enable');
	}

	/**
	 * Sends request to disable (aka unpublish) a link
	 * 
	 * @param string $id
	 * @return stdClass $response
	 */
	public function disableLink( $id ) {
		return $this->requester->request('PUT', $this->url . '/' . $id . '/disable');
	}

	/**
	 * Sends request to delete a link
	 * 
	 * @param string $id
	 * @return stdClass $response
	 */
	public function deleteLink( $id ) {
		return $this->requester->request('DELETE', $this->url . '/' . $id);
	}
}

/**
 * Gumroad Exception Class
 * 
 * Simple extension of the Exception class
 */
class Gumroad_Exception extends Exception
{

}

?>
{'fraction_non_alphanumeric': 0.14294825799250577, 'fraction_numerical': 0.0012756118950809216, 'mean_word_length': 3.21505376344086, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 131, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5062744', 'n_tokens_mistral': 4470, 'n_tokens_neox': 4049, 'n_words': 1023}