Skip to content

Commit fee58a1

Browse files
committed
feat: holded addon endpoints introspection
1 parent 577ea2e commit fee58a1

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

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)