kinlane
2/24/2016 - 11:33 PM

Generate OpenAPI Spec From Clinical Trials MySQL Database

Generate OpenAPI Spec From Clinical Trials MySQL Database

<?php
$host = "[your database server";]
$database = "[your database name]";
$user = "[your user]";
$pass = "[your password]";

// database connection
mysql_connect($host, $user, $pass) or die ('cannot connect to the database: ' . mysql_error());
mysql_select_db($database) or die ('cannot select database: ' . mysql_error());

// function for creating default 200 OK
function getOK($table)
	{
		
	$R = array();
	$R['description'] = $table . " response";		
	$R['schema'] = array();
	
	$S = array();
	$S['type'] = "array";
	
	$I = array();
	$I['$ref'] = "#/definitions/" . $table;
	
	$S['items'] = $I;
	
	$R['schema'] =  $S;			
	
	$ok = new stdClass;
	$response = "200";
	$ok->$response = $R;		
		
	return $ok;	
	}

$Spec_Name = str_replace("_"," ",$database);
$Spec_Name = ucwords($Spec_Name);

// loop through table + fields
$loop = mysql_query("SHOW tables FROM " . $database) or die ('cannot select tables');

// OpenAPI Spec Base
$OpenAPISpec = array();
$OpenAPISpec['swagger'] = "2.0";
$OpenAPISpec['info'] = array();
$Info = array();
$Info['title'] = $Spec_Name;
$Info['description'] = 'An auto generated OpenAPISpec from ' . $database . '.';
$Info['termsOfService'] = 'Going to need a TOS!';
$Info['version'] = 'v1';
$OpenAPISpec['info'] = $Info;
$OpenAPISpec['host'] = 'example.host.com';
$OpenAPISpec['basePath'] = '/';
$OpenAPISpec['schemes'] = array('http');
$OpenAPISpec['produces'] = array('application/json');
$OpenAPISpec['paths'] = new stdClass;
$OpenAPISpec['definitions'] = array();
$definition = new stdClass;

while($table = mysql_fetch_array($loop))
{
	
	// We will need to identif
	$this_key = "";
	
	// The table name
    $table = $table[0];
	$tag_name = str_replace("_"," ",$table);
	$tag_name = ucwords($tag_name);		
    
	// Get the fields for the table
    $row = mysql_query("SHOW columns FROM `" . $table . "`") or die ('cannot select table fields');
	
	// Lets just build an array for resue
	$Fields = array();	
    while ($col = mysql_fetch_array($row))
    	{
        $field_name = $col[0];
        $field_type = $col[1];
		$field_key = $col[2];

		$F = array();
		$F['name'] = $field_name;
		$F['type'] = $field_type;
		$F['key'] = $field_key;
		
		// If its the key then set
		if($field_key == 'NO')
			{
			$this_key = strtolower($field_name);
			}
		
		  array_push($Fields,$F);
    	}	
    
	// Use table name to build some of the values we will need
	$path1 = "/" . $table . "/";
	$path2 = "/" . $table . "/{" . $this_key . "}/";
	
	// Base operation name
	$operation = str_replace('_',' ', $table);
	$operation = ucwords($operation);
	$operation = str_replace(' ','', $operation);
	
	// One for each method
	$getoperation = 'get' . $operation;
	$get2operation = 'getIndividual' . $operation;
	$postoperation = 'add' . $operation;
	$putoperation = 'update' . $operation;
	$deleteoperation = 'delete' . $operation;
	
	////
	// GET
	////	
	
	// Build the first path with GET & POST
	$OpenAPISpec['paths']->$path1 = new stdClass;
	$thispath = new stdClass;
	
	// Build base GET API settings
	$path = array();	
	$path['summary'] = "get " . $table;
	$path['description'] = "Needs a description.";
	$path['operationId'] = $getoperation;
	$path['parameters'] = array();
	
	// Add one Query for pulling
	$F = array();
	$F['name'] = 'query';	
	$F['in'] = "query";
	$F['description'] = "A query parameter to search with.";
	$F['required'] = false;
	$F['type'] = 'string';	
	
	// Add to the path parameters
	array_push($path['parameters'],$F);	
	
	// Create a 200 response
	$path['responses'] = array();
	$path['responses'] = getOK($table);
	
	// Create a default tag
	$path['tags'] = array($tag_name);
	
	// Create the path class	
	$thispath->get = $path;	
	
	////
	// POST
	////	
	
	// Build base POST API settings
	$path = array();	
	$path['summary'] = "add " . $table;
	$path['description'] = "Needs a description.";
	$path['operationId'] = $postoperation;
	$path['parameters'] = array();
	
	// Loop through and build the parameters
	foreach($Fields as $Field)
		{		
		$field_name = $Field['name'];
		$field_type = $Field['type'];
		$field_key = $Field['key'];
		
		// set the type of field (need to do date, but....)
		$pos = strpos($field_type, 'INT');
		if ($pos === false) { $type = "string"; } else {  $type = "integer"; }
			
		// Local Array
		$F = array();
		$F['name'] = $field_name;	
		$F['in'] = "query";
		$F['description'] = "Needs a description.";
		$F['required'] = false;
		$F['type'] = $type;	
		
		if(strtolower($field_name) != $this_key)
			{
			// Add to the path parameters
			array_push($path['parameters'],$F);
			}	
		}
	
	// Create a 200 response
	$path['responses'] = array();
	$path['responses'] = getOK($table);
	
	// Create a default tag
	$path['tags'] = array($tag_name);
	
	// Create the path class
	$thispath->post = $path;		
	
	// Set the First Base Path
	$OpenAPISpec['paths']->$path1 = $thispath;
	
	// Start the next set of paths w/ id
	
	////
	// GET INDIVIDUAL
	////	
	
	// Build the first path with GET & PUT & DELETE
	$OpenAPISpec['paths']->$path2 = new stdClass;
	$thispath = new stdClass;
	
	// Build base GET API settings
	$path = array();	
	$path['summary'] = "get " . $table;
	$path['description'] = "Needs a description.";
	$path['operationId'] = $get2operation;
	$path['parameters'] = array();
	
	// Add the id path field
	$F = array();
	$F['name'] = 'id';	
	$F['in'] = "path";
	$F['description'] = "the id for the " . $table;
	$F['required'] = true;
	$F['type'] = 'integer';	
	
	// Add to the path parameters
	array_push($path['parameters'],$F);	
	
	// Create a 200 response
	$path['responses'] = array();
	$path['responses'] = getOK($table);
	
	// Create a default tag
	$path['tags'] = array($tag_name);
	
	// Create the path class	
	$thispath->get = $path;	
	
	////
	// PUT
	////	
	
	// Build base PUT API settings
	$path = array();	
	$path['summary'] = "update " . $table;
	$path['description'] = "Needs a description.";
	$path['operationId'] = $putoperation;
	$path['parameters'] = array();
	
	// Add the id path field
	$F = array();
	$F['name'] = 'id';	
	$F['in'] = "path";
	$F['description'] = "the id for the " . $table;
	$F['required'] = true;
	$F['type'] = 'integer';		
	array_push($path['parameters'],$F);
	
	// Loop through and build the parameters
	foreach($Fields as $Field)
		{		
		$field_name = $Field['name'];
		$field_type = $Field['type'];
		$field_key = $Field['key'];
		
		// set the type of field (need to do date, but....)
		$pos = strpos($field_type, 'INT');
		if ($pos === false) { $type = "string"; } else {  $type = "integer"; }
			
		// Local Array
		$F = array();
		$F['name'] = $field_name;	
		$F['in'] = "query";
		$F['description'] = "Needs a description.";
		$F['required'] = false;
		$F['type'] = $type;	
		
		if(strtolower($field_name) != $this_key)
			{
			// Add to the path parameters
			array_push($path['parameters'],$F);
			}	
		}
	
	// Create a 200 response
	$path['responses'] = array();
	$path['responses'] = getOK($table);
	
	// Create a default tag
	$path['tags'] = array($tag_name);
	
	// Create the path class
	$thispath->put = $path;	
	
	////
	// DELETE
	////	
	
	// Build base DELETE API settings
	$path = array();	
	$path['summary'] = "delete " . $table;
	$path['description'] = "Needs a description.";
	$path['operationId'] = $deleteoperation;
	$path['parameters'] = array();
	
	// Add the id path field
	$F = array();
	$F['name'] = 'id';	
	$F['in'] = "path";
	$F['description'] = "the id for the " . $table;
	$F['required'] = true;
	$F['type'] = 'integer';	
	
	// Add to the path parameters
	array_push($path['parameters'],$F);	
	
	// Create a 200 response
	$path['responses'] = array();
	$path['responses'] = getOK($table);
	
	// Create a default tag
	$path['tags'] = array($tag_name);
	
	// Create the path class	
	$thispath->delete = $path;			
	
	// Set the First Base Path
	$OpenAPISpec['paths']->$path2 = $thispath;	
	
	////
	// Definition(s)
	////
	
	// Build the definition we reference above		
	$definition->$table = new stdClass;
	$definition->$table->properties = new stdClass;
	
	// Loop through and build the parameters
	foreach($Fields as $Field)
		{
					
		$field_name = $Field['name'];
		$field_type = $Field['type'];
		$field_key = $Field['key'];
		
		// set the type of field (need to do date, but....)
		$pos = strpos($field_type, 'INT');
		if ($pos === false) { $type = "string"; } else {  $type = "integer"; }

		$D = array();
		$D['description'] = $field_name . " for " . $table;
		$D['type'] = $type;
		$D['format'] = $type;
		
		$definition->$table->properties->$field_name = $D;
	
		}	
	
	} 

$OpenAPISpec['definitions'] = $definition; 

$ReturnSpec = json_encode($OpenAPISpec);
echo $ReturnSpec;
?>