<ConnectionManager Name="Foxy" RuntimeVersion="" Help="http://www.cozyroc.com/ssis/foxy-connection" xmlns="http://www.cozyroc.com/schema/rcm-config-1.0.xsd">
	<Service EndPoint="https://api.foxycart.com" />

	<Authentication Name="OAuth 2">
		<Documentation>https://api.foxycart.com/docs/authentication/thirdparty</Documentation>

		<User>
			<Parameter Name="Scope" Default="store_full_access" />
			<Parameter Name="redirect_uri" Default="http://example.com">
				<Documentation>Required. Specify redirect_uri</Documentation>
			</Parameter>
		</User>

		<Test Url="/" />

		<Token Url="{{=new Uri(connection.serverHost).authority().replace('//api', '//my')+ OAuth2.sign_in_url}}" Result="{{=response.access_token}}">
			<Parameters>
				<Parameter Name="response_type" Value="{{=OAuth2.sign_in_response_type}}" />
				<Parameter Name="client_id" Value="{{=OAuth2.client_id}}" />
				<Parameter Name="redirect_uri" Value="{{=token.redirect_uri}}" />
				<Parameter Name="scope" Value="{{=token.Scope}}" />
				<Parameter Name="state" Value="{{=Math.random()}}" />
			</Parameters>

			<Authorized Id="{{=token.redirect_uri}}" Result="{{=
				connection.execute({
					url: OAuth2.authorization_url,
					method: OAuth2.authorization_method,
					headers: {
						Authorization: 'Basic ' + Base64.encode(OAuth2.client_id + ':' + OAuth2.client_secret)
					},
					parameters: {
						code: response.code,
						grant_type: OAuth2.grant_type,
						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}}">
				<Documentation>https://api.foxycart.com/docs/authentication/refresh_tokens</Documentation>

				<Parameters>
					<Parameter Name="Authorization" Value="Basic {{=Base64.encode(OAuth2.client_id + ':' + OAuth2.client_secret)}}" Type="HttpHeader" />
					<Parameter Name="grant_type" Value="refresh_token" />
					<Parameter Name="refresh_token" Value="{{=token.Refresh}}" />
				</Parameters>
			</Refresh>
		</Token>

		<Parameters>
			<Parameter Name="Authorization" Value="Bearer {{=token.Access}}" Type="HttpHeader" />
			<Parameter Name="FOXY-API-VERSION" Value="1" 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 Result="{{=response}}">
					<Parameters>
						<Parameter Name="offset" Value="{{=parameters.iterator}}" />
						<Parameter Name="limit" Value="{{=parameters.batchSize}}" />
						<Parameter Name="fields" Value="{{=parameters.fields.join()}}" />
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
					</Parameters>

					<Iterator>
						<Next Value="{{=parseInt(parameters.iterator || 0) + parameters.batchSize}}">
							<Documentation>https://api.foxycart.com/docs/cheat-sheet</Documentation>
						</Next>
					</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('Uri');
require('base64');
]]>
		</Module>
		<Module Name="OAuth 2">
			<![CDATA[
// See RFC 6749 (https://tools.ietf.org/html/rfc6749) for familiarizing with OAuth2 authorization flows-related terms and concepts 
var OAuth2 = {
	client_id: "client_AI8yYHIfD4z2DQ1Sl34C", // required. The Client ID.
	client_secret: "HN5hCXwWvfxZSvsG31QAYfwsVmack8EWQD8iwu6R", // required. The Client Secret.
	sign_in_url: "/authorize", // 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: "/token", // 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.
	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: "/token" // required, if the authentication needs to support renewing access tokens
};
]]>
		</Module>
	</Script>
</ConnectionManager>