<ConnectionManager Name="Bullhorn" RuntimeVersion="" Help="http://www.cozyroc.com/ssis/bullhorn-connection" xmlns="http://www.cozyroc.com/schema/rcm-config-1.0.xsd">
	<Service EndPoint="https://rest.bullhornstaffing.com/rest-services/[corpToken]" />

	<Authentication Name="OAuth 2">
		<Documentation>http://bullhorn.github.io/Getting-Started-with-REST/</Documentation>

		<User>
			<Parameter Name="client_id">
				<Documentation>Required. Specify OAuth client id.</Documentation>
			</Parameter>
			<Parameter Name="client_secret">
				<Documentation>Required. Specify OAuth client secret.</Documentation>
			</Parameter>
			<Parameter Name="redirect_uri">
				<Documentation>Required. Specify the redirect uri you have configured.</Documentation>
			</Parameter>
		</User>

		<Test Url="/ping" />

		<Token Url="{{=OAuth2.sign_in_url}}" Result="{{=response.access_token}}">
			<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="{{=OAuth2.redirect_uri}}" Result="{{=
				connection.execute({
					url: OAuth2.authorization_url,
					method: OAuth2.authorization_method,
					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="client_id" Value="{{=token.client_id}}" />
					<Parameter Name="client_id" Value="{{=token.client_secret}}" />
					<Parameter Name="refresh_token" Value="{{=token.Refresh}}" />
					<Parameter Name="grant_type" Value="refresh_token" />
				</Parameters>
			</Refresh>
		</Token>

		<Parameters>
			<Parameter Name="BhRestToken" Value="{{=Bullhorn.getSessionToken(token.Access)}}" 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="start" Value="{{=parameters.iterator}}" />
						<Parameter Name="count" Value="{{=parameters.batchSize}}" />
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
					</Parameters>

					<Iterator>
						<Next Value="{{=parseInt(parameters.iterator || 0) + parameters.batchSize}}" />
					</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");

var Bullhorn = {};


///////////////////////////////////////////////////////////////////////////////
Bullhorn.getSessionToken = function(token) {
	var result = Bullhorn.sessionToken;
	if (result) {
		return result;
	}
	
	var response = connection.execute({
		url: 'https://rest.bullhornstaffing.com/rest-services/login',
		method: 'POST',
		parameters: { version: '*', access_token: token }
	});
	result = Bullhorn.sessionToken = response.BhRestToken;

	return result;
}]]>
		</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 = {
	sign_in_url: "https://auth.bullhornstaffing.com/oauth/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: "https://auth.bullhornstaffing.com/oauth/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: "https://auth.bullhornstaffing.com/oauth/token" // required, if the authentication needs to support renewing access tokens
};

		]]>
		</Module>
	</Script>
</ConnectionManager>