<?php

/**
 * RootApi
 * PHP version 5
 *
 * @category Class
 * @package  MailchimpMarketing
 * @author   Swagger Codegen team
 * @link     https://github.com/swagger-api/swagger-codegen
 */
/**
 * Mailchimp Marketing API
 *
 * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
 *
 * OpenAPI spec version: 3.0.80
 * Contact: apihelp@mailchimp.com
 * Generated by: https://github.com/swagger-api/swagger-codegen.git
 * Swagger Codegen version: 2.4.12
 */
/**
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen
 * Do not edit the class manually.
 */
namespace FormGent\MailchimpMarketing\Api;

use FormGent\GuzzleHttp\Client;
use FormGent\GuzzleHttp\ClientInterface;
use FormGent\GuzzleHttp\Exception\RequestException;
use FormGent\GuzzleHttp\Psr7\MultipartStream;
use FormGent\GuzzleHttp\Psr7\Query;
use FormGent\GuzzleHttp\Psr7\Request;
use FormGent\GuzzleHttp\RequestOptions;
use FormGent\MailchimpMarketing\ApiException;
use FormGent\MailchimpMarketing\Configuration;
use FormGent\MailchimpMarketing\HeaderSelector;
use FormGent\MailchimpMarketing\ObjectSerializer;
class RootApi
{
    protected $client;
    protected $config;
    protected $headerSelector;
    public function __construct(Configuration $config = null)
    {
        $this->client = new Client(['defaults' => ['timeout' => 120.0]]);
        $this->headerSelector = new HeaderSelector();
        $this->config = $config ?: new Configuration();
    }
    public function getConfig()
    {
        return $this->config;
    }
    public function getRoot($fields = null, $exclude_fields = null)
    {
        $response = $this->getRootWithHttpInfo($fields, $exclude_fields);
        return $response;
    }
    public function getRootWithHttpInfo($fields = null, $exclude_fields = null)
    {
        $request = $this->getRootRequest($fields, $exclude_fields);
        try {
            $options = $this->createHttpClientOption();
            try {
                $response = $this->client->send($request, $options);
            } catch (RequestException $e) {
                throw $e;
            }
            $statusCode = $response->getStatusCode();
            if ($statusCode < 200 || $statusCode > 299) {
                throw new ApiException(\sprintf('[%d] Error connecting to the API (%s)', $statusCode, $request->getUri()), $statusCode, $response->getHeaders(), $response->getBody());
            }
            $responseBody = $response->getBody();
            $content = $responseBody->getContents();
            $content = \json_decode($content);
            return $content;
        } catch (ApiException $e) {
            throw $e->getResponseBody();
        }
    }
    protected function getRootRequest($fields = null, $exclude_fields = null)
    {
        $resourcePath = '/';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = \false;
        // query params
        if (\is_array($fields)) {
            $queryParams['fields'] = ObjectSerializer::serializeCollection($fields, 'csv');
        } else {
            if ($fields !== null) {
                $queryParams['fields'] = ObjectSerializer::toQueryValue($fields);
            }
        }
        // query params
        if (\is_array($exclude_fields)) {
            $queryParams['exclude_fields'] = ObjectSerializer::serializeCollection($exclude_fields, 'csv');
        } else {
            if ($exclude_fields !== null) {
                $queryParams['exclude_fields'] = ObjectSerializer::toQueryValue($exclude_fields);
            }
        }
        // body params
        $_tempBody = null;
        if ($multipart) {
            $headers = $this->headerSelector->selectHeadersForMultipart(['application/json', 'application/problem+json']);
        } else {
            $headers = $this->headerSelector->selectHeaders(['application/json', 'application/problem+json'], ['application/json']);
        }
        // for model (json/xml)
        if (isset($_tempBody)) {
            $httpBody = $_tempBody;
            if ($headers['Content-Type'] === 'application/json') {
                if ($httpBody instanceof \stdClass) {
                    $httpBody = \FormGent\GuzzleHttp\json_encode($httpBody);
                }
                if (\is_array($httpBody)) {
                    $httpBody = \FormGent\GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($httpBody));
                }
            }
        } elseif (\count($formParams) > 0) {
            if ($multipart) {
                $multipartContents = [];
                foreach ($formParams as $formParamName => $formParamValue) {
                    $multipartContents[] = ['name' => $formParamName, 'contents' => $formParamValue];
                }
                $httpBody = new MultipartStream($multipartContents);
            } elseif ($headers['Content-Type'] === 'application/json') {
                $httpBody = \FormGent\GuzzleHttp\json_encode($formParams);
            } else {
                $httpBody = Query::build($formParams);
            }
        }
        // Basic Authentication
        if (!empty($this->config->getUsername()) && !empty($this->config->getPassword())) {
            $headers['Authorization'] = 'Basic ' . \base64_encode($this->config->getUsername() . ":" . $this->config->getPassword());
        }
        // OAuth Authentication
        if (!empty($this->config->getAccessToken())) {
            $headers['Authorization'] = 'Bearer ' . $this->config->getAccessToken();
        }
        $defaultHeaders = [];
        if ($this->config->getUserAgent()) {
            $defaultHeaders['User-Agent'] = $this->config->getUserAgent();
        }
        $headers = \array_merge($defaultHeaders, $headerParams, $headers);
        $query = Query::build($queryParams);
        return new Request('GET', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), $headers, $httpBody);
    }
    protected function createHttpClientOption()
    {
        $options = [];
        if ($this->config->getDebug()) {
            $options[RequestOptions::DEBUG] = \fopen($this->config->getDebugFile(), 'a');
            if (!$options[RequestOptions::DEBUG]) {
                throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile());
            }
        }
        if ($this->config->getTimeout()) {
            $options[RequestOptions::TIMEOUT] = $this->config->getTimeout();
        }
        return $options;
    }
}
