Skip to content

Commit

Permalink
Client generation
Browse files Browse the repository at this point in the history
  • Loading branch information
zio-mitch committed Feb 3, 2025
1 parent abc625f commit 7a6452d
Show file tree
Hide file tree
Showing 43 changed files with 6,980 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Dockerfile
docker-compose.yml
README.md
open-api.yml
openapi.yaml
.dockerignore
.gitignore
data
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
data/input
data/maildir
!data/input/.gitkeep
!data/maildir/outbox/.gitkeep
!data/maildir/outbox/.gitkeep
client
!client/.gitkeep
178 changes: 20 additions & 158 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,163 +68,25 @@ To build the image:
docker build -t mailculators-prod --target mailculators-prod .
```

### Example clients

```php
<?php

function validateEmailData(stdClass $payload): bool
{
// Validate that the 'data' property exists and is an array
if (empty($payload->data) || !is_array($payload->data)) {
throw new InvalidArgumentException("Invalid or missing 'data'. It must be an array of email objects.");
}

// Validate each email in the 'data' array
foreach ($payload->data as $email) {
if (empty($email->id) || !is_string($email->id)) {
throw new InvalidArgumentException("Invalid or missing 'id'. It must be a string.");
}
if (empty($email->type) || $email->type !== "email") {
throw new InvalidArgumentException("Invalid or missing 'type'. It must be 'email'.");
}
if (empty($email->attributes) || !is_object($email->attributes)) {
throw new InvalidArgumentException("Invalid or missing 'attributes'. It must be an object.");
}

$attributes = $email->attributes;

// Validate email fields
if (empty($attributes->from) || !filter_var($attributes->from, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException("Invalid or missing 'from' email address.");
}
if (!empty($attributes->replyTo) && !filter_var($attributes->replyTo, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException("Invalid 'replyTo' email address.");
}
if (empty($attributes->to) || !filter_var($attributes->to, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException("Invalid or missing 'to' email address.");
}
if (empty($attributes->subject) || !is_string($attributes->subject)) {
throw new InvalidArgumentException("Invalid or missing 'subject'. It must be a string.");
}
if (empty($attributes->bodyHTML) && empty($attributes->bodyText)) {
throw new InvalidArgumentException("At least one of 'bodyHTML' or 'bodyText' must be provided.");
}

// Attachments validation (if provided)
if (!empty($attributes->attachments) && !is_array($attributes->attachments)) {
throw new InvalidArgumentException("'attachments' must be an array of file paths.");
}

// Custom headers validation (if provided)
if (!empty($attributes->customHeaders) && !is_object($attributes->customHeaders)) {
throw new InvalidArgumentException("'customHeaders' must be an object.");
}
}

return true; // Validation passed for all emails
}

function createEmailQueue(string $apiUrl, stdClass $payload): array
{
// Validate payload
try {
validateEmailData($payload);
} catch (InvalidArgumentException $e) {
throw $e; // Rethrow invalid argument exceptions
}

// Initialize cURL
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $apiUrl . "/email-queues");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));

// Execute the request and fetch response
$response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
throw new RuntimeException("cURL error: " . curl_error($ch));
}

// Get the HTTP response code
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// Close cURL
curl_close($ch);

// Handle response
if ($httpCode === 201) {
return json_decode($response, true); // Success
} elseif ($httpCode === 400) {
throw new InvalidArgumentException("Invalid request: " . $response);
} elseif ($httpCode === 405) {
throw new RuntimeException("Invalid HTTP method");
} elseif ($httpCode === 500) {
throw new RuntimeException("Internal server error");
} else {
throw new RuntimeException("Unexpected response code: $httpCode, response: $response");
}
}

// Example usage
try {
$apiUrl = "https://api.mailculator.com"; // Replace with your actual API URL

// Create payload as an object with 'data' as an array of stdClass objects
$payload = (object)[
"data" => [
(object)[
"id" => "user123:queue456:message789",
"type" => "email",
"attributes" => (object)[
"from" => "[email protected]",
"replyTo" => "[email protected]",
"to" => "[email protected]",
"subject" => "Test Email 1",
"bodyHTML" => "<p>This is a test email 1.</p>",
"bodyText" => "This is a test email 1.",
"attachments" => ["/path/to/attachment1.txt", "/path/to/attachment2.jpg"],
"customHeaders" => (object)[
"X-Custom-Header" => "CustomValue1"
]
]
],
(object)[
"id" => "user123:queue456:message790",
"type" => "email",
"attributes" => (object)[
"from" => "[email protected]",
"replyTo" => "[email protected]",
"to" => "[email protected]",
"subject" => "Test Email 2",
"bodyHTML" => "<p>This is a test email 2.</p>",
"bodyText" => "This is a test email 2.",
"attachments" => ["/path/to/attachment3.pdf"],
"customHeaders" => (object)[
"X-Custom-Header" => "CustomValue2"
]
]
]
]
];

$result = createEmailQueue($apiUrl, $payload);
echo "Email queue created successfully:\n";
print_r($result);
} catch (InvalidArgumentException $e) {
echo "Validation Error: " . $e->getMessage();
} catch (RuntimeException $e) {
echo "Runtime Error: " . $e->getMessage();
} catch (Exception $e) {
echo "General Error: " . $e->getMessage();
}
### API clients

Generate clients from open api:

```bash
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
-i /local/openapi.yaml \
-g <language> \
-o /local/client/<language>
```

For example for php:

```bash
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
-i /local/openapi.yaml \
-g php \
-o /local/client/php
```

Then you will find the generated client in the directory client/php in the root path of this repository.

File renamed without changes.
15 changes: 15 additions & 0 deletions php-client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ref: https://github.com/github/gitignore/blob/master/Composer.gitignore

composer.phar
/vendor/

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock

# php-cs-fixer cache
.php_cs.cache
.php-cs-fixer.cache

# PHPUnit cache
.phpunit.result.cache
23 changes: 23 additions & 0 deletions php-client/.openapi-generator-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
28 changes: 28 additions & 0 deletions php-client/.openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.gitignore
.php-cs-fixer.dist.php
.travis.yml
README.md
composer.json
docs/Api/DefaultApi.md
docs/Model/CreateEmailQueue201Response.md
docs/Model/CreateEmailQueue201ResponseData.md
docs/Model/CreateEmailQueue201ResponseDataLinks.md
docs/Model/CreateEmailQueueRequest.md
docs/Model/CreateEmailQueueRequestData.md
docs/Model/CreateEmailQueueRequestDataAttributes.md
docs/Model/Email.md
git_push.sh
lib/Api/DefaultApi.php
lib/ApiException.php
lib/Configuration.php
lib/HeaderSelector.php
lib/Model/CreateEmailQueue201Response.php
lib/Model/CreateEmailQueue201ResponseData.php
lib/Model/CreateEmailQueue201ResponseDataLinks.php
lib/Model/CreateEmailQueueRequest.php
lib/Model/CreateEmailQueueRequestData.php
lib/Model/CreateEmailQueueRequestDataAttributes.php
lib/Model/Email.php
lib/Model/ModelInterface.php
lib/ObjectSerializer.php
phpunit.xml.dist
1 change: 1 addition & 0 deletions php-client/.openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.12.0-SNAPSHOT
29 changes: 29 additions & 0 deletions php-client/.php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* @generated
* @link https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/HEAD/doc/config.rst
*/
$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('vendor')
->exclude('test')
->exclude('tests')
;

$config = new PhpCsFixer\Config();
return $config->setRules([
'@PSR12' => true,
'phpdoc_order' => true,
'array_syntax' => [ 'syntax' => 'short' ],
'strict_comparison' => true,
'strict_param' => true,
'no_trailing_whitespace' => false,
'no_trailing_whitespace_in_comment' => false,
'braces' => false,
'single_blank_line_at_eof' => false,
'blank_line_after_namespace' => false,
'no_leading_import_slash' => false,
])
->setFinder($finder)
;
8 changes: 8 additions & 0 deletions php-client/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: php
# Bionic environment has preinstalled PHP from 7.1 to 7.4
# https://docs.travis-ci.com/user/reference/bionic/#php-support
dist: bionic
php:
- 7.4
before_install: "composer install"
script: "vendor/bin/phpunit"
Loading

0 comments on commit 7a6452d

Please sign in to comment.