@@ -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 /**
0 commit comments