File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ namespace Helper \WPUnit ;
3+
4+ /**
5+ * Helper methods and actions related to the ConvertKit API,
6+ * which are then available using $I->{yourFunctionName}.
7+ */
8+ class API extends \Codeception \Module
9+ {
10+ /**
11+ * Sends a request to the ConvertKit API, typically used to read an endpoint to confirm
12+ * that data in an Acceptance Test was added/edited/deleted successfully.
13+ *
14+ * @param string $endpoint Endpoint.
15+ * @param string $method Method (GET|POST|PUT).
16+ * @param array $params Endpoint Parameters.
17+ * @return array
18+ */
19+ public function apiRequest ($ endpoint , $ method = 'GET ' , $ params = array ())
20+ {
21+ // Build query parameters.
22+ $ params = array_merge (
23+ $ params ,
24+ [
25+ 'api_key ' => $ _ENV ['CONVERTKIT_API_KEY ' ],
26+ 'api_secret ' => $ _ENV ['CONVERTKIT_API_SECRET ' ],
27+ ]
28+ );
29+
30+ // Send request.
31+ try {
32+ $ client = new \GuzzleHttp \Client ();
33+ $ result = $ client ->request (
34+ $ method ,
35+ 'https://api.convertkit.com/v3/ ' . $ endpoint . '? ' . http_build_query ($ params ),
36+ [
37+ 'headers ' => [
38+ 'Accept-Encoding ' => 'gzip ' ,
39+ 'timeout ' => 5 ,
40+ ],
41+ ]
42+ );
43+
44+ // Return JSON decoded response.
45+ return json_decode ($ result ->getBody ()->getContents (), true );
46+ } catch (\GuzzleHttp \Exception \ClientException $ e ) {
47+ return [];
48+ }
49+ }
50+ }
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ actor: WpunitTester
66modules :
77 enabled :
88 - WPLoader
9+ - \Helper\WPUnit\API
910
1011 # Codeception supplied classes, which provide assertions and filesystem functions
1112 - \Codeception\Module\Asserts
You can’t perform that action at this time.
0 commit comments