Skip to content

Commit 20d1747

Browse files
committed
feat: mailchimp backend endpoints introspection
1 parent fee58a1 commit 20d1747

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,62 @@ public function ping( $backend ) {
7676
return true;
7777
}
7878

79+
/**
80+
* Performs an introspection of the backend API and returns a list of available endpoints.
81+
*
82+
* @param string $backend Target backend name.
83+
* @param string|null $method HTTP method.
84+
*
85+
* @return array|WP_Error
86+
*/
87+
public function get_endpoints( $backend, $method = null ) {
88+
$response = wp_remote_get(
89+
self::SWAGGER_URL,
90+
array(
91+
'headers' => array(
92+
'Accept' => 'application/json',
93+
'Host' => 'mailchimp.com',
94+
'Referer' => 'https://mailchimp.com/developer/marketing/api/',
95+
'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0',
96+
),
97+
),
98+
);
99+
100+
if ( ! is_wp_error( $response ) ) {
101+
$data = json_decode( $response['body'], true );
102+
103+
if ( $data ) {
104+
$oa_explorer = new OpenAPI( $data );
105+
106+
$paths = $oa_explorer->paths();
107+
108+
if ( $method ) {
109+
$method = strtolower( $method );
110+
$method_paths = array();
111+
112+
foreach ( $paths as $path ) {
113+
$path_obj = $oa_explorer->path_obj( $path );
114+
115+
if ( $path_obj && isset( $path_obj[ $method ] ) ) {
116+
$method_paths[] = $path;
117+
}
118+
}
119+
120+
$paths = $method_paths;
121+
}
122+
123+
return array_map(
124+
function ( $path ) {
125+
return '/3.0' . $path;
126+
},
127+
$paths,
128+
);
129+
}
130+
}
131+
132+
return array( '/3.0/lists/{list_id}/members' );
133+
}
134+
79135
/**
80136
* Performs an introspection of the backend endpoint and returns API fields
81137
* and accepted content type.

0 commit comments

Comments
 (0)