-
Notifications
You must be signed in to change notification settings - Fork 6
/
smartdocs_service.inc
168 lines (151 loc) · 4.65 KB
/
smartdocs_service.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
use Apigee\ManagementAPI\APIProxy;
use Apigee\SmartDocs\Model;
use Apigee\SmartDocs\Revision;
require_once drupal_get_path('module', 'smartdocs') . "/smartdocs.admin.inc";
require_once drupal_get_path('module', 'smartdocs') . "/batch/smartdocs.render.inc";
/**
* SmartDocs Import Callback
*
* @param $model_name
* @param $fields
* @return mixed|string
*/
function _smartdocs_service_swagger_import( $model_name, $fields ) {
// Manipulate $_FILES to match form version.
$_FILES['files'] = $_FILES['api_definition'];
foreach ($_FILES['files'] as $key => $item) {
$_FILES['files'][$key] = array('file' => $_FILES['api_definition'][$key]);
}
// Acceptable formats.
$formats = array('yaml' => 'swagger', 'json' => 'swagger', 'xml' => 'wadl');
$tmp = explode(".", $_FILES['api_definition']['name']);
$ext = array_pop($tmp);
if (!isset($formats[$ext])) {
return services_error(t('unknown format'), 500);
}
// Create form_state for smartdocs_import_model_submit function.
$form_state = array();
// Pass in file type.
$form_state['values']['type'] = 'swagger_' . $ext;
// Pass in model name
$form_state['values']['model_uuid'] = $model_name;
//Pass in upload type
$form_state['values']['upload_type'] = 'file';
// Pass in file, not sure if this is neccessary.
$form_state['values']['file'] = file_get_contents($_FILES['api_definition']['tmp_name']);
// Call smartdoc_import function. This will now trigger smartdoc_import hooks.
smartdocs_import_model_submit(array(), $form_state);
return "Import Successful";
}
/**
* SmartDocs Render Callback
* @param $name
* @return string
*/
function _smartdocs_service_swagger_render( $name ) {
drupal_set_time_limit(0);
$config = devconnect_default_org_config();
$model = new Model($config);
$model->load($name);
$revision = new Revision($config, $model->getUuid());
$revision->load($model->getLatestRevisionNumber());
$selected = array();
foreach($revision->getResources() as $resource){
foreach($resource->getMethods() as $method) {
$selected[$method->getUuid()] = $method->getUuid();
}
}
$batch = smartdocs_render($model, $revision, $selected, array('publish' => TRUE), FALSE);
$queue = DrupalQueue::get('smartdocs_service_queue');
foreach($batch['operations'] as $op){
$queue->createItem($op);
}
return 'Items have been added to the queue for rendering';
}
/**
* Retrieve a SmartDocs model
*
* @param $model_name
* @return array
*/
function _smartdocs_model_resource_retrieve($model_name) {
$model = new Model(devconnect_default_org_config());
$model->load($model_name);
return $model->toArray();
}
/**
* Create a SmartDocs model
*
* @param $model_name
* @param $display_name
* @param $model_description
* @return string
*/
function _smartdocs_model_resource_create($model_name, $display_name, $model_description){
if(smartdocs_model_exist($model_name)){
return services_error(t('Model with the same name exists'), 500);
}
if (empty($display_name)) {
$display_name = $model_name;
}
$form_state = array();
$form_state['values']['model_name'] = $model_name;
$form_state['values']['display_name'] = $display_name;
$form_state['values']['model_description'] = $model_description;
smartdocs_add_model_submit(array(), $form_state);
return "Model added";
}
/**
* Update a SmartDocs model
*
*
* @param $model_name
* @param $display_name
* @param $model_description
* @return string
*/
function _smartdocs_model_resource_update($model_name, $display_name, $model_description){
$model = new Model(devconnect_default_org_config());
$model->load($model_name);
if (empty($display_name)) {
$model->setDisplayName($display_name);
}
$model->setDescription($model_description);
$model->save(TRUE);
return "Model updated";
}
/**
* Delete a Smartdocs model
*
* @param $model_name
* @return string
*/
function _smartdocs_model_resource_delete($model_name){
$model = new Model(devconnect_default_org_config());
$model->load($model_name);
$form_state = array();
$form_state['values']['model'] = $model->toArray();
smartdocs_model_delete_submit(array(), $form_state);
return "Delete successful";
}
/**
* List all the SmartDocs Model
*
* @return array
*/
function _smartdocs_model_resource_index(){
$model = new Model(devconnect_default_org_config());
$return = array();
foreach($model->listModels() as $model){
$return[$model->getName()] = $model->toArray();
}
return $return;
}
/**
* Count of the pending items in smartdocs_service_queue
*/
function _smartdocs_service_queue_status(){
$queue = DrupalQueue::get('smartdocs_service_queue');
return array('pending_items' => $queue->numberOfItems());
}