Skip to content

Commit ca0abd5

Browse files
authored
Merge pull request #5 from codeccoop/feat/openapi
OpenAPI
2 parents 42d3ddb + 8fb3d49 commit ca0abd5

23 files changed

+746
-8424
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,17 @@ public function ping( $backend ) {
101101
/**
102102
* Performs an introspection of the backend endpoint and returns API fields.
103103
*
104-
* @param string $endpoint API endpoint.
105-
* @param string $backend Backend name.
104+
* @param string $endpoint API endpoint.
105+
* @param string $backend Backend name.
106+
* @param string|null $method HTTP method.
106107
*
107108
* @return array List of fields and content type of the endpoint.
108109
*/
109-
public function get_endpoint_schema( $endpoint, $backend ) {
110+
public function get_endpoint_schema( $endpoint, $backend, $method = null ) {
111+
if ( in_array( $method, array( 'POST', 'PUT' ), true ) ) {
112+
return array();
113+
}
114+
110115
if (
111116
! preg_match(
112117
'/\/(([A-Z][a-z]+(_[A-Z][a-z])?)(?:\/upsert)?$)/',

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

Lines changed: 86 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,28 @@ class Brevo_Addon extends Addon {
2525
*
2626
* @var string
2727
*/
28-
const TITLE = 'Brevo';
28+
public const TITLE = 'Brevo';
2929

3030
/**
3131
* Handles the addon's name.
3232
*
3333
* @var string
3434
*/
35-
const NAME = 'brevo';
35+
public const NAME = 'brevo';
3636

3737
/**
3838
* Handles the addom's custom bridge class.
3939
*
4040
* @var string
4141
*/
42-
const BRIDGE = '\FORMS_BRIDGE\Brevo_Form_Bridge';
42+
public const BRIDGE = '\FORMS_BRIDGE\Brevo_Form_Bridge';
43+
44+
/**
45+
* Holds the OAS URL.
46+
*
47+
* @var string
48+
*/
49+
public const OAS_URL = 'https://developers.brevo.com/reference/get_companies?json=on';
4350

4451
/**
4552
* Performs a request against the backend to check the connexion status.
@@ -91,241 +98,99 @@ public function fetch( $endpoint, $backend ) {
9198
}
9299

93100
/**
94-
* Performs an introspection of the backend endpoint and returns API fields
95-
* and accepted content type.
101+
* Fetch available models from the OAS spec.
96102
*
97-
* @param string $endpoint API endpoint.
98-
* @param string $backend Backend name.
103+
* @param Backend $backend HTTP backend object.
99104
*
100105
* @return array
106+
*
107+
* @todo Implementar el endpoint de consulta de endpoints disponibles.
101108
*/
102-
public function get_endpoint_schema( $endpoint, $backend ) {
103-
$bridge = new Brevo_Form_Bridge(
109+
public function get_endpoints( $backend ) {
110+
$response = wp_remote_get(
111+
self::OAS_URL,
104112
array(
105-
'name' => '__brevo-' . time(),
106-
'endpoint' => $endpoint,
107-
'backend' => $backend,
108-
'method' => 'GET',
113+
'headers' => array(
114+
'Accept' => 'application/json',
115+
'Host' => 'developers.brevo.com',
116+
'Referer' => 'https://developers.brevo.com/reference/get_companies',
117+
'Alt-Used' => 'developers.brevo.com',
118+
'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0',
119+
),
109120
)
110121
);
111122

112-
if ( strstr( $bridge->endpoint, 'contacts' ) ) {
113-
$response = $bridge
114-
->patch(
115-
array(
116-
'name' => 'brevo-contacts-attributes',
117-
'endpoint' => '/v3/contacts/attributes',
118-
'method' => 'GET',
119-
)
120-
)
121-
->submit();
122-
123-
if ( is_wp_error( $response ) ) {
124-
return array();
125-
}
123+
if ( is_wp_error( $response ) ) {
124+
return array();
125+
}
126126

127-
if ( $bridge->endpoint === '/v3/contacts/doubleOptinConfirmation' ) {
128-
$fields = array(
129-
array(
130-
'name' => 'email',
131-
'schema' => array( 'type' => 'string' ),
132-
'required' => true,
133-
),
134-
array(
135-
'name' => 'includeListIds',
136-
'schema' => array(
137-
'type' => 'array',
138-
'items' => array( 'type' => 'integer' ),
139-
),
140-
'required' => true,
141-
),
142-
array(
143-
'name' => 'excludeListIds',
144-
'schema' => array(
145-
'type' => 'array',
146-
'items' => array( 'type' => 'integer' ),
147-
),
148-
),
149-
array(
150-
'name' => 'templateId',
151-
'schema' => array( 'type' => 'integer' ),
152-
'required' => true,
153-
),
154-
array(
155-
'name' => 'redirectionUrl',
156-
'schema' => array( 'type' => 'string' ),
157-
'required' => true,
158-
),
159-
array(
160-
'name' => 'attributes',
161-
'schema' => array(
162-
'type' => 'object',
163-
'properties' => array(),
164-
),
165-
),
166-
);
167-
} else {
168-
$fields = array(
169-
array(
170-
'name' => 'email',
171-
'schema' => array( 'type' => 'string' ),
172-
'required' => true,
173-
),
174-
array(
175-
'name' => 'ext_id',
176-
'schema' => array( 'type' => 'string' ),
177-
),
178-
array(
179-
'name' => 'emailBlacklisted',
180-
'schema' => array( 'type' => 'boolean' ),
181-
),
182-
array(
183-
'name' => 'smsBlacklisted',
184-
'schema' => array( 'type' => 'boolean' ),
185-
),
186-
array(
187-
'name' => 'listIds',
188-
'schema' => array(
189-
'type' => 'array',
190-
'items' => array( 'type' => 'integer' ),
191-
),
192-
),
193-
array(
194-
'name' => 'updateEnabled',
195-
'schema' => array( 'type' => 'boolean' ),
196-
),
197-
array(
198-
'name' => 'smtpBlacklistSender',
199-
'schema' => array( 'type' => 'boolean' ),
200-
),
201-
array(
202-
'name' => 'attributes',
203-
'schema' => array(
204-
'type' => 'object',
205-
'properties' => array(),
206-
),
207-
),
208-
);
209-
}
127+
$data = json_decode( $response['body'], true );
128+
$oa_explorer = new OpenAPI( $data['oasDefinition'] );
210129

211-
foreach ( $response['data']['attributes'] as $attribute ) {
212-
$fields[] = array(
213-
'name' => 'attributes.' . $attribute['name'],
214-
'schema' => array( 'type' => 'string' ),
215-
);
216-
}
130+
$paths = $oa_explorer->paths();
217131

218-
return $fields;
219-
} else {
220-
if ( ! preg_match( '/\/([a-z]+)$/', $bridge->endpoint, $matches ) ) {
221-
return array();
222-
}
132+
return array_map(
133+
function ( $path ) {
134+
return '/v3' . $path;
135+
},
136+
$paths,
137+
);
138+
}
223139

224-
$module = $matches[1];
225-
$response = $bridge
226-
->patch(
227-
array(
228-
'name' => "brevo-{$module}-attributes",
229-
'endpoint' => "/v3/crm/attributes/{$module}",
230-
'method' => 'GET',
231-
)
232-
)
233-
->submit();
140+
/**
141+
* Performs an introspection of the backend endpoint and returns API fields
142+
* and accepted content type.
143+
*
144+
* @param string $endpoint API endpoint.
145+
* @param string $backend Backend name.
146+
* @param string|null $method HTTP method.
147+
*
148+
* @return array
149+
*/
150+
public function get_endpoint_schema( $endpoint, $backend, $method = null ) {
151+
$bytes = random_bytes( 16 );
152+
$bytes[6] = chr( ord( $bytes[6] ) & 0x0f | 0x40 ); // set version to 0100
153+
$bytes[8] = chr( ord( $bytes[8] ) & 0x3f | 0x80 ); // set bits 6-7 to 10
154+
$uuid = vsprintf( '%s%s-%s-%s-%s-%s%s%s', str_split( bin2hex( $bytes ), 4 ) );
155+
156+
$response = wp_remote_get(
157+
self::OAS_URL,
158+
array(
159+
'headers' => array(
160+
'Accept' => 'application/json',
161+
'Host' => 'developers.brevo.com',
162+
'Referer' => 'https://developers.brevo.com/reference/get_companies',
163+
'Alt-Used' => 'developers.brevo.com',
164+
'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0',
165+
'X-Requested-With' => 'XMLHttpRequest',
166+
),
167+
'cookies' => array(
168+
'anonymous_id' => $uuid,
169+
'first_referrer' => 'https://app.brevo.com/',
170+
'pscd' => 'get.brevo.com',
171+
'readme_language' => 'shell',
172+
'readme_library' => '{%22shell%22:%22curl%22}',
173+
),
174+
)
175+
);
234176

235-
if ( is_wp_error( $response ) ) {
236-
return array();
237-
}
177+
if ( is_wp_error( $response ) ) {
178+
return array();
179+
}
238180

239-
if ( 'companies' === $module ) {
240-
$fields = array(
241-
array(
242-
'name' => 'name',
243-
'schema' => array( 'type' => 'string' ),
244-
'required' => true,
245-
),
246-
array(
247-
'name' => 'countryCode',
248-
'schema' => array( 'type' => 'integer' ),
249-
),
250-
array(
251-
'name' => 'linkedContactsIds',
252-
'schema' => array(
253-
'type' => 'array',
254-
'items' => array( 'type' => 'integer' ),
255-
),
256-
),
257-
array(
258-
'name' => 'linkedDealsIds',
259-
'schema' => array(
260-
'type' => 'array',
261-
'items' => array( 'type' => 'integer' ),
262-
),
263-
),
264-
array(
265-
'name' => 'attributes',
266-
'schema' => array(
267-
'type' => 'object',
268-
'properties' => array(),
269-
),
270-
),
271-
);
272-
} elseif ( 'deals' === $module ) {
273-
$fields = array(
274-
array(
275-
'name' => 'name',
276-
'schema' => array( 'type' => 'string' ),
277-
'required' => true,
278-
),
279-
array(
280-
'name' => 'linkedDealsIds',
281-
'schema' => array(
282-
'type' => 'array',
283-
'items' => array( 'type' => 'integer' ),
284-
),
285-
),
286-
array(
287-
'name' => 'linkedCompaniesIds',
288-
'schema' => array(
289-
'type' => 'array',
290-
'items' => array( 'type' => 'integer' ),
291-
),
292-
),
293-
array(
294-
'name' => 'attributes',
295-
'schema' => array(
296-
'type' => 'object',
297-
'properties' => array(),
298-
),
299-
),
300-
);
301-
}
181+
$data = json_decode( $response['body'], true );
182+
if ( ! $data ) {
183+
return array();
184+
}
302185

303-
foreach ( $response['data'] as $attribute ) {
304-
switch ( $attribute['attributeTypeName'] ) {
305-
case 'number':
306-
$type = 'number';
307-
break;
308-
case 'text':
309-
$type = 'string';
310-
break;
311-
case 'user':
312-
$type = 'email';
313-
break;
314-
case 'date':
315-
$type = 'date';
316-
break;
317-
default:
318-
$type = 'string';
319-
}
186+
$oa_explorer = new OpenAPI( $data['oasDefinition'] );
320187

321-
$fields[] = array(
322-
'name' => 'attributes.' . $attribute['internalName'],
323-
'schema' => array( 'type' => $type ),
324-
);
325-
}
188+
$method = strtolower( $method ?? 'post' );
189+
$path = preg_replace( '/^\/v\d+/', '', $endpoint );
190+
$source = in_array( $method, array( 'post', 'put', 'patch' ), true ) ? 'body' : 'query';
191+
$params = $oa_explorer->params( $path, $method, $source );
326192

327-
return $fields;
328-
}
193+
return $params ?: array();
329194
}
330195
}
331196

0 commit comments

Comments
 (0)