Skip to content

Commit

Permalink
Merge pull request #10 from Multidialogo/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
zio-mitch authored Feb 3, 2025
2 parents f1ed3d7 + 341365b commit 562a5eb
Show file tree
Hide file tree
Showing 4 changed files with 24 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.

0 comments on commit 562a5eb

Please sign in to comment.