Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * AWS Client factory. Retrieves a client with moodle specific HTTP configuration.
19
 *
20
 * @package    core
21
 * @author     Peter Burnett <peterburnett@catalyst-au.net>
22
 * @copyright  2022 Catalyst IT
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace core\aws;
27
use Aws\AwsClient;
28
 
29
/**
30
 * AWS Client factory. Retrieves a client with moodle specific HTTP configuration.
31
 *
32
 * @copyright  2022 Catalyst IT
33
 * @author     Peter Burnett <peterburnett@catalyst-au.net>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class client_factory {
37
    /**
38
     * Get an AWS client with moodle specific HTTP configuration.
39
     *
40
     * @param string $class Fully qualified AWS classname e.g. \Aws\S3\S3Client
41
     * @param array $opts array of constructor options for AWS Client.
42
     * @return AwsClient
43
     */
44
    public static function get_client(string $class, array $opts): AwsClient {
45
        // Modify the opts to add HTTP timeouts.
46
        if (empty($opts['http'])) {
47
            $opts['http'] = ['connect_timeout' => HOURSECS];
48
        } else if (!array_key_exists('connect_timeout', $opts['http'])) {
49
            // Try not to override existing settings.
50
            $opts['http']['connect_timeout'] = HOURSECS;
51
        }
52
 
53
        // Blindly trust the call here. If it exceptions, the raw message is the most useful.
54
        $client = new $class($opts);
55
        if (!$client instanceof \Aws\AwsClient) {
56
            throw new \moodle_exception('clientnotfound', 'factor_sms');
57
        }
58
 
59
        // Now we can configure the proxy with the routing aware middleware.
60
        return aws_helper::configure_client_proxy($client);
61
    }
62
}