<?xml version="1.0" encoding="utf-8"?>
<ConnectionManager Name="Trustpilot" RuntimeVersion="" Help="http://www.cozyroc.com/ssis/trustpilot-connection" xmlns="http://www.cozyroc.com/schema/rcm-config-1.0.xsd">
	<Service EndPoint="https://api.trustpilot.com/v1" />

	<Authentication Name="OAuth">
		<Documentation>https://developers.trustpilot.com/authentication</Documentation>

		<User>
			<Parameter Name="client_id">
				<Documentation>Required. Identifier of application that is associated with the authentication token.</Documentation>
			</Parameter>
			<Parameter Name="client_secret">
				<Documentation>Specify application client secret. Optional.</Documentation>
			</Parameter>
			<Parameter Name="redirect_uri" Default="https://www.cozyroc.com/oauth_callback">
				<Documentation>Required. Specify the redirect uri you have set during creation of the app.</Documentation>
			</Parameter>
		</User>

		<Test Url="/business-units/all">
			<Parameters>
				<Parameter Name="perPage" Value="1" Type="QueryString" />
			</Parameters>
		</Test>

		<Token Url="{{=OAuth2.sign_in_url}}" Result="{{=response.access_token}}">
			<Documentation>https://developers.trustpilot.com/authentication#auth-code</Documentation>

			<Parameters>
				<Parameter Name="response_type" Value="{{=OAuth2.sign_in_response_type}}" />
				<Parameter Name="client_id" Value="{{=token.client_id}}" />
				<Parameter Name="redirect_uri" Value="{{=token.redirect_uri}}" />
			</Parameters>

			<Authorized Id="{{=token.redirect_uri}}" Result="{{=
				connection.execute({
					url: OAuth2.authorization_url,
					method: OAuth2.authorization_method,
					headers: {
						Authorization: 'Basic ' + Base64.encode(token.client_id + ':' + token.client_secret)
					},
					parameters: {
						code: response.code,
						grant_type: OAuth2.grant_type,
						client_id: token.client_id,
						client_secret: token.client_secret,
						redirect_uri: token.redirect_uri
				} })
			}}" />

			<Refresh Url="{{=OAuth2.refresh_token_url}}" Method="POST" Result="{{=response.refresh_token}}" Expiration="{{=Date.now() + (response.expires_in - 300) * 1000}}">
				<Parameters>
					<Parameter Name="grant_type" Value="refresh_token" />
					<Parameter Name="Authorization" Value="Basic {{=Base64.encode(token.client_id + ':' + token.client_secret)}}" Type="HttpHeader" />
					<Parameter Name="refresh_token" Value="{{=token.Refresh}}" />
				</Parameters>
			</Refresh>
		</Token>

		<Parameters>
			<Parameter Name="Authorization" Value="Bearer {{=token.Access}}" Type="HttpHeader" />
			<Parameter Name="apikey" Value="{{=token.client_id}}" Type="HttpHeader" />
		</Parameters>
	</Authentication>

	<Authentication Name="Password">
		<Documentation>https://developers.trustpilot.com/authentication</Documentation>

		<User>
			<Parameter Name="APIKey">
				<Documentation>Required. Specify API key.</Documentation>
			</Parameter>
			<Parameter Name="APISecret">
				<Documentation>Required. Specify API secret.</Documentation>
			</Parameter>
			<Parameter Name="Username">
				<Documentation>Required. Specify the user name.</Documentation>
			</Parameter>
			<Parameter Name="Password">
				<Documentation>Required. Specify the password.</Documentation>
			</Parameter>
		</User>

		<Test Url="/business-units/all">
			<Parameters>
				<Parameter Name="perPage" Value="1" Type="QueryString" />
			</Parameters>
		</Test>

		<Token Url="https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken"  Result="{{=response.access_token}}">
			<Parameters>
				<Parameter Name="grant_type" Value="password" />
				<Parameter Name="Authorization" Value="Basic {{=Base64.encode(connection.user.APIKey + ':' + connection.user.APISecret)}}" Type="HttpHeader" />
				<Parameter Name="username" Value="{{=connection.user.Username}}" />
				<Parameter Name="password" Value="{{=connection.user.Password}}" />
			</Parameters>

			<Refresh Url="https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/refresh" Method="POST" Result="{{=response.refresh_token}}" Expiration="{{=Date.now() + (response.expires_in - 300) * 1000}}">
				<Parameters>
					<Parameter Name="grant_type" Value="refresh_token" />
					<Parameter Name="Authorization" Value="Basic {{=Base64.encode(connection.user.APIKey + ':' + connection.user.APISecret)}}" Type="HttpHeader" />
					<Parameter Name="refresh_token" Value="{{=token.Refresh}}" />
				</Parameters>
			</Refresh>
		</Token>

		<Parameters>
			<Parameter Name="Authorization" Value="Bearer {{=token.Access}}" Type="HttpHeader" />
			<Parameter Name="apikey" Value="{{=connection.user.APIKey}}" Type="HttpHeader" />
		</Parameters>
	</Authentication>

	<Resources>
		<Template>
			<Field Name="ShortText" DataType="DT_WSTR" Length="255" />
			<Field Name="LongText" DataType="DT_WSTR" Length="1000" />
			<Field Name="DateTime" DataType="DT_DBTIMESTAMP" />
			<Field Name="Date" DataType="DT_DBDATE" />

			<Resource Name="Base">
				<Read>
					<Parameters>
						<Parameter Name="page" Value="{{=parameters.iterator}}" />
						<Parameter Name="perPage" Value="{{=parameters.batchSize}}" />
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
					</Parameters>

					<Iterator>
						<Next Value="{{=parseInt(parameters.iterator || 0) + 1}}" />
					</Iterator>
				</Read>

				<Create Method="POST">
					<Parameters>
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
						<Parameter Name="application/json" Value="{{=item}}" Type="Body" />
					</Parameters>
				</Create>

				<Update Method="PUT">
					<Parameters>
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
						<Parameter Name="application/json" Value="{{=item}}" Type="Body" />
					</Parameters>
				</Update>

				<Delete Method="DELETE" />
			</Resource>
		</Template>
	</Resources>
	<Script>
		<Module Name="Main">
			<![CDATA[
require('underscore');
require('base64');


]]>
		</Module>
		<Module Name="Code Based">
			<![CDATA[
// See RFC 6749 (https://tools.ietf.org/html/rfc6749) for familiarizing with OAuth2 authorization flows-related terms and concepts
var OAuth2 = {
	sign_in_url: 'https://authenticate.trustpilot.com', // required. The starting endpoint in the authorization server.
	sign_in_response_type: 'code', // optional. The value for the "response_type" parameter in the initiating call. Usually "code", but can also be "token", "id_token".
	authorization_url: 'https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken', // required. The endpoint for exchanging authorization code for an access token.
	authorization_method: 'POST', // required. Usually the authorization URL is accessed via a POST request.
	redirect_uri: 'https://www.cozyroc.com/oauth_callback', // required. The callback URL to be called after autherization. If the default value doesn't meet requirements, need to setup "Token.Authorized.LocalListenUrl".
	grant_type: 'authorization_code', // optional. Can also be "client_credentials" or "password" (see https://auth0.com/docs/applications/reference/grant-types-available )
	refresh_token_url: 'https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/refresh' // required, if the authentication needs to supprot renewing access tokens
};
]]>
		</Module>
	</Script>
</ConnectionManager>