|
| 1 | +<?php namespace App\Services\Apis; |
| 2 | +/** |
| 3 | + * Copyright 2023 OpenStack Foundation |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + **/ |
| 14 | +use GuzzleHttp\Exception\RequestException; |
| 15 | +use models\exceptions\ValidationException; |
| 16 | +use libs\utils\ICacheService; |
| 17 | +use GuzzleHttp\Client; |
| 18 | +use GuzzleHttp\HandlerStack; |
| 19 | +use GuzzleRetry\GuzzleRetryMiddleware; |
| 20 | +use GuzzleHttp\RequestOptions; |
| 21 | +use Illuminate\Support\Facades\Config; |
| 22 | +use Illuminate\Support\Facades\Log; |
| 23 | +use Exception; |
| 24 | +/** |
| 25 | + * Class EmailTemplatesApi |
| 26 | + * @package App\Services |
| 27 | + */ |
| 28 | +final class EmailTemplatesApi extends AbstractOAuth2Api |
| 29 | + implements IEmailTemplatesApi |
| 30 | +{ |
| 31 | + const AppName = 'EMAIL_TEMPLATES_SERVICE'; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var Client |
| 35 | + */ |
| 36 | + private $client; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var string |
| 40 | + */ |
| 41 | + private $scopes; |
| 42 | + |
| 43 | + /** |
| 44 | + * ExternalUserApi constructor. |
| 45 | + * @param ICacheService $cacheService |
| 46 | + */ |
| 47 | + public function __construct(ICacheService $cacheService) |
| 48 | + { |
| 49 | + parent::__construct($cacheService); |
| 50 | + $stack = HandlerStack::create(); |
| 51 | + $stack->push(GuzzleRetryMiddleware::factory()); |
| 52 | + |
| 53 | + $this->client = new Client([ |
| 54 | + 'handler' => $stack, |
| 55 | + 'base_uri' => Config::get('mail.service_base_url') ?? '', |
| 56 | + 'timeout' => Config::get('curl.timeout', 60), |
| 57 | + 'allow_redirects' => Config::get('curl.allow_redirects', false), |
| 58 | + 'verify' => Config::get('curl.verify_ssl_cert', true), |
| 59 | + ]); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @return string |
| 64 | + */ |
| 65 | + public function getAppName(): string |
| 66 | + { |
| 67 | + return self::AppName; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return array |
| 72 | + */ |
| 73 | + public function getAppConfig(): array |
| 74 | + { |
| 75 | + return [ |
| 76 | + 'client_id' => Config::get("mail.service_client_id"), |
| 77 | + 'client_secret' => Config::get("mail.service_client_secret"), |
| 78 | + 'scopes' => Config::get("mail.service_client_scopes") |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param string $template_id |
| 84 | + * @return mixed |
| 85 | + * @throws \GuzzleHttp\Exception\GuzzleException |
| 86 | + * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException |
| 87 | + */ |
| 88 | + public function getEmailTemplate(string $template_id) { |
| 89 | + Log::debug("EmailTemplatesApi::getEmailTemplate"); |
| 90 | + |
| 91 | + try { |
| 92 | + $query = [ |
| 93 | + 'access_token' => $this->getAccessToken() |
| 94 | + ]; |
| 95 | + |
| 96 | + $response = $this->client->get("/api/v1/mail-templates/{$template_id}", [ |
| 97 | + 'query' => $query, |
| 98 | + ] |
| 99 | + ); |
| 100 | + return json_decode($response->getBody()->getContents(), true); |
| 101 | + } |
| 102 | + catch (Exception $ex) { |
| 103 | + $this->cleanAccessToken(); |
| 104 | + Log::error($ex); |
| 105 | + throw $ex; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @param array $payload |
| 111 | + * @param string $html_template |
| 112 | + * @return mixed |
| 113 | + * @throws \GuzzleHttp\Exception\GuzzleException|\League\OAuth2\Client\Provider\Exception\IdentityProviderException |
| 114 | + */ |
| 115 | + public function getEmailPreview(array $payload, string $html_template) |
| 116 | + { |
| 117 | + Log::debug("EmailTemplatesApi::getEmailPreview"); |
| 118 | + |
| 119 | + try { |
| 120 | + $query = [ |
| 121 | + 'access_token' => $this->getAccessToken() |
| 122 | + ]; |
| 123 | + |
| 124 | + $response = $this->client->put('/api/v1/mail-templates/all/render', [ |
| 125 | + 'query' => $query, |
| 126 | + RequestOptions::JSON => [ |
| 127 | + "html" => $html_template, |
| 128 | + "payload" => $payload |
| 129 | + ] |
| 130 | + ] |
| 131 | + ); |
| 132 | + return json_decode($response->getBody()->getContents(), true); |
| 133 | + } |
| 134 | + catch (Exception $ex) { |
| 135 | + $this->cleanAccessToken(); |
| 136 | + Log::error($ex); |
| 137 | + throw $ex; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
0 commit comments