Skip to content

Commit b00a850

Browse files
authored
Merge pull request #21 from codeccoop/feat/endpoints-datalist
endpoints datalist and api introspection
2 parents 12adf5e + 2ac8c21 commit b00a850

22 files changed

Lines changed: 717 additions & 104 deletions

forms-bridge/addons/bigin/class-bigin-addon.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,38 @@ public function ping( $backend ) {
103103
return true;
104104
}
105105

106+
/**
107+
* Performs an introspection of the backend API and returns a list of available endpoints.
108+
*
109+
* @param string $backend Target backend name.
110+
* @param string|null $method HTTP method.
111+
*
112+
* @return array|WP_Error
113+
*/
114+
public function get_endpoints( $backend, $method = null ) {
115+
$bridge = new Bigin_Form_Bridge(
116+
array(
117+
'name' => '__bigin-' . time(),
118+
'endpoint' => '/bigin/v2/settings/modules',
119+
'method' => 'GET',
120+
'backend' => $backend,
121+
)
122+
);
123+
124+
$response = $bridge->submit();
125+
126+
if ( is_wp_error( $response ) ) {
127+
return array();
128+
}
129+
130+
return array_map(
131+
function ( $module ) {
132+
return '/bigin/v2/' . $module['api_name'];
133+
},
134+
$response['data']['modules'],
135+
);
136+
}
137+
106138
/**
107139
* Performs an introspection of the backend endpoint and returns API fields.
108140
*

forms-bridge/addons/bigin/hooks.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ function ( $defaults, $addon, $schema ) {
4343
'ref' => '#credential',
4444
'name' => 'scope',
4545
'value' =>
46-
'ZohoBigin.modules.ALL,ZohoBigin.settings.layouts.READ,ZohoBigin.users.READ',
46+
'ZohoBigin.modules.ALL,ZohoBigin.settings.modules.READ,ZohoBigin.settings.layouts.READ,ZohoBigin.users.READ',
4747
),
4848
),
4949
'credential' => array(
5050
'scope' =>
51-
'ZohoBigin.modules.ALL,ZohoBigin.settings.layouts.READ,ZohoBigin.users.READ',
51+
'ZohoBigin.modules.ALL,ZohoBigin.settings.modules.READ,ZohoBigin.settings.layouts.READ,ZohoBigin.users.READ',
5252
),
5353
),
5454
$defaults,

forms-bridge/addons/brevo/class-brevo-addon.php

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class Brevo_Addon extends Addon {
4646
*
4747
* @var string
4848
*/
49-
public const OAS_URL = 'https://developers.brevo.com/reference/get_companies?json=on';
49+
// public const OAS_URL = 'https://developers.brevo.com/reference/get_companies?json=on';
50+
public const OAS_URL = 'https://api.brevo.com/v3/swagger_definition_v3.yml';
5051

5152
/**
5253
* Performs a request against the backend to check the connexion status.
@@ -79,41 +80,51 @@ public function ping( $backend ) {
7980
/**
8081
* Fetch available models from the OAS spec.
8182
*
82-
* @param Backend $backend HTTP backend object.
83+
* @param string $backend Backend name.
84+
* @param string|null $method HTTP method.
8385
*
8486
* @return array
8587
*
8688
* @todo Implementar el endpoint de consulta de endpoints disponibles.
8789
*/
88-
public function get_endpoints( $backend ) {
89-
$response = wp_remote_get(
90-
self::OAS_URL,
91-
array(
92-
'headers' => array(
93-
'Accept' => 'application/json',
94-
'Host' => 'developers.brevo.com',
95-
'Referer' => 'https://developers.brevo.com/reference/get_companies',
96-
'Alt-Used' => 'developers.brevo.com',
97-
'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0',
98-
),
99-
)
100-
);
90+
public function get_endpoints( $backend, $method = null ) {
91+
if ( function_exists( 'yaml_parse' ) ) {
92+
$response = wp_remote_get( self::OAS_URL );
10193

102-
if ( is_wp_error( $response ) ) {
103-
return array();
104-
}
94+
if ( ! is_wp_error( $response ) ) {
95+
$data = yaml_parse( $response['body'] );
10596

106-
$data = json_decode( $response['body'], true );
107-
$oa_explorer = new OpenAPI( $data['oasDefinition'] );
97+
if ( $data ) {
98+
$oa_explorer = new OpenAPI( $data );
10899

109-
$paths = $oa_explorer->paths();
100+
$paths = $oa_explorer->paths();
110101

111-
return array_map(
112-
function ( $path ) {
113-
return '/v3' . $path;
114-
},
115-
$paths,
116-
);
102+
if ( $method ) {
103+
$method = strtolower( $method );
104+
$method_paths = array();
105+
106+
foreach ( $paths as $path ) {
107+
$path_obj = $oa_explorer->path_obj( $path );
108+
109+
if ( $path_obj && isset( $path_obj[ $method ] ) ) {
110+
$method_paths[] = $path;
111+
}
112+
}
113+
114+
$paths = $method_paths;
115+
}
116+
117+
return array_map(
118+
function ( $path ) {
119+
return '/v3' . $path;
120+
},
121+
$paths,
122+
);
123+
}
124+
}
125+
126+
return array( '/v3/contacts' );
127+
}
117128
}
118129

119130
/**

forms-bridge/addons/dolibarr/class-dolibarr-addon.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,62 @@ public function get_endpoint_schema( $endpoint, $backend, $method = null ) {
157157

158158
return $fields;
159159
}
160+
161+
/**
162+
* Performs an introspection of the backend API and returns a list of available endpoints.
163+
*
164+
* @param string $backend Backend name.
165+
* @param string|null $method HTTP method.
166+
*
167+
* @return array|WP_Error
168+
*/
169+
public function get_endpoints( $backend, $method = null ) {
170+
$bridge = new Dolibarr_Form_Bridge(
171+
array(
172+
'name' => '__dolibarr-' . time(),
173+
'endpoint' => self::SWAGGER_ENDPOINT,
174+
'backend' => $backend,
175+
'method' => 'GET',
176+
)
177+
);
178+
179+
$response = $bridge->submit();
180+
181+
if ( is_wp_error( $response ) ) {
182+
return array();
183+
}
184+
185+
$version = $response['data']['swagger'] ?? null;
186+
if ( ! $version ) {
187+
return array();
188+
}
189+
190+
$oa_explorer = new OpenAPI( $response['data'] );
191+
192+
$paths = $oa_explorer->paths();
193+
194+
if ( $method ) {
195+
$method = strtolower( $method );
196+
$method_paths = array();
197+
198+
foreach ( $paths as $path ) {
199+
$path_obj = $oa_explorer->path_obj( $path );
200+
201+
if ( $path_obj && isset( $path_obj[ $method ] ) ) {
202+
$method_paths[] = $path;
203+
}
204+
}
205+
206+
$paths = $method_paths;
207+
}
208+
209+
return array_map(
210+
function ( $path ) {
211+
return '/api/index.php' . $path;
212+
},
213+
$paths,
214+
);
215+
}
160216
}
161217

162218
Dolibarr_Addon::setup();

forms-bridge/addons/financoop/class-financoop-addon.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ public function ping( $backend ) {
6969
return true;
7070
}
7171

72+
/**
73+
* Performs an introspection of the backend API and returns a list of available endpoints.
74+
*
75+
* @param string $backend Target backend name.
76+
* @param string|null $method HTTP method.
77+
*
78+
* @return array|WP_Error
79+
*/
80+
public function get_endpoints( $backend, $method = null ) {
81+
return array(
82+
'/api/campaign/{campaign_id}/subscription_request',
83+
'/api/campaign/{campaign_id}/donation_request',
84+
'/api/campaign/{campaign_id}/loan_request',
85+
);
86+
}
87+
7288
/**
7389
* Performs an introspection of the backend endpoint and returns API fields
7490
* and accepted content type.

forms-bridge/addons/gcalendar/class-gcalendar-addon.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,29 @@ public function fetch( $endpoint, $backend ) {
130130
return $response;
131131
}
132132

133+
/**
134+
* Performs an introspection of the backend API and returns a list of available endpoints.
135+
*
136+
* @param string $backend Target backend name.
137+
* @param string|null $method HTTP method.
138+
*
139+
* @return array|WP_Error
140+
*/
141+
public function get_endpoints( $backend, $method = null ) {
142+
$response = $this->fetch( null, $backend );
143+
144+
if ( is_wp_error( $response ) || empty( $response['data']['items'] ) ) {
145+
return array();
146+
}
147+
148+
return array_map(
149+
function ( $calendar ) {
150+
return '/calendar/v3/calendars/' . $calendar['id'] . '/events';
151+
},
152+
$response['data']['items']
153+
);
154+
}
155+
133156
/**
134157
* Performs an introspection of the backend endpoint and returns API fields
135158
* and accepted content type.

forms-bridge/addons/gsheets/class-gsheets-addon.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,29 @@ public function fetch( $endpoint, $backend ) {
152152
return $response;
153153
}
154154

155+
/**
156+
* Performs an introspection of the backend API and returns a list of available endpoints.
157+
*
158+
* @param string $backend Target backend name.
159+
* @param string|null $method HTTP method.
160+
*
161+
* @return array|WP_Error
162+
*/
163+
public function get_endpoints( $backend, $method = null ) {
164+
$response = $this->fetch( null, $backend );
165+
166+
if ( is_wp_error( $response ) || empty( $response['data']['files'] ) ) {
167+
return array();
168+
}
169+
170+
return array_map(
171+
function ( $file ) {
172+
return '/v4/spreadsheets/' . $file['id'];
173+
},
174+
$response['data']['files']
175+
);
176+
}
177+
155178
/**
156179
* Performs an introspection of the backend endpoint and returns API fields
157180
* and accepted content type.

forms-bridge/addons/holded/class-holded-addon.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,74 @@ public function ping( $backend ) {
9191
return true;
9292
}
9393

94+
/**
95+
* Performs an introspection of the backend API and returns a list of available endpoints.
96+
*
97+
* @param string $backend Target backend name.
98+
* @param string|null $method HTTP method.
99+
*
100+
* @return array|WP_Error
101+
*/
102+
public function get_endpoints( $backend, $method = null ) {
103+
$paths = array();
104+
105+
foreach ( self::OAS_URLS as $module => $oas_path ) {
106+
$oas_url = self::OAS_BASE_URL . $oas_path . '?dereference=false&reduce=false';
107+
108+
$response = wp_remote_get(
109+
$oas_url,
110+
array(
111+
'headers' => array(
112+
'Accept' => 'application/json',
113+
'Host' => 'developers.holded.com',
114+
'Alt-Used' => 'developers.holded.com',
115+
'Referer' => 'https://developers.holded.com/reference/list-contacts-1',
116+
'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0',
117+
),
118+
),
119+
);
120+
121+
if ( is_wp_error( $response ) ) {
122+
continue;
123+
}
124+
125+
$data = json_decode( $response['body'], true );
126+
if ( ! $data ) {
127+
continue;
128+
}
129+
130+
$oa_explorer = new OpenAPI( $data['data']['api']['schema'] );
131+
132+
$module_paths = $oa_explorer->paths();
133+
134+
if ( $method ) {
135+
$method = strtolower( $method );
136+
$method_paths = array();
137+
138+
foreach ( $module_paths as $path ) {
139+
$path_obj = $oa_explorer->path_obj( $path );
140+
141+
if ( $path_obj && isset( $path_obj[ $method ] ) ) {
142+
$method_paths[] = $path;
143+
}
144+
}
145+
146+
$module_paths = $method_paths;
147+
}
148+
149+
$module_paths = array_map(
150+
function ( $path ) use ( $module ) {
151+
return '/api/' . $module . '/v1' . $path;
152+
},
153+
$module_paths,
154+
);
155+
156+
$paths = array_merge( $paths, $module_paths );
157+
}
158+
159+
return $paths;
160+
}
161+
94162
/**
95163
* Performs an introspection of the backend endpoint and returns API fields
96164
* and accepted content type.

0 commit comments

Comments
 (0)