<ConnectionManager Name="HSD Metrics" RuntimeVersion="" Help="http://www.cozyroc.com/ssis/hsd-metrics-connection" xmlns="http://www.cozyroc.com/schema/rcm-config-1.0.xsd">
	<Service EndPoint="https://app.hsdmetrics.com/HsdServices/api/v1" />

	<Authentication>
		<User>
			<Parameter Name="Username">
				<Documentation>Required. Specify user name.</Documentation>
			</Parameter>
			<Parameter Name="Password" Type="password">
				<Documentation>Required. Specify password.</Documentation>
			</Parameter>
		</User>

		<Token Url="/auth/login" Method="POST" Result="{{=response.access_token}}">
			<Parameters>
				<Parameter Name="username" Value="{{=connection.user.Username}}" />
				<Parameter Name="password" Value="{{=connection.user.Password}}" />
			</Parameters>

			<Refresh Url="/auth/token" Method="POST" Result="{{=response.refresh_token}}" Expiration="{{=Date.now() + (response.expires_in - 300) * 1000}}">
				<Parameters>
					<Parameter Name="grant_type" Value="refresh_token" />
					<Parameter Name="refresh_token" Value="{{=token.Refresh}}" />
					<Parameter Name="client_key" Value="{{=token.client_key}}" />
				</Parameters>
			</Refresh>
		</Token>

		<Parameters>
			<Parameter Name="Authorization" Value="Bearer {{=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="page" Value="{{=parameters.iterator}}" />
						<Parameter Name="limit" Value="{{=parameters.batchSize}}" />
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
					</Parameters>

					<Iterator>
						<Next Value="{{=parseInt(parameters.iterator || 1) + 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>

		<Resource Name="RespondentResponses" Template="Base">
			<Fields>
				<Field Name="pcode" Template="ShortText" />
				<Field Name="firstName" Template="ShortText" />
				<Field Name="lastName" Template="ShortText" />
				<Field Name="primaryPhone" Template="ShortText" />
				<Field Name="alternatePhone" Template="ShortText" />
				<Field Name="demographicResponses">
					<Component IsArray="true">
						<Component Name="demographic" Template="ShortText" />
						<Component Name="value" Template="ShortText" />
					</Component>
				</Field>
				<Field Name="surveyResponses">
					<Component IsArray="true">
						<Component Name="question" Template="ShortText" />
						<Component Name="response" Template="ShortText" />
						<Component Name="comments" Template="ShortText" />
					</Component>
				</Field>
				<Field Name="method" Template="ShortText" />
				<Field Name="completed" Template="ShortText" />
				<Field Name="listDate" Template="ShortText" />
			</Fields>

			<Read Url="/survey/responses" Result="{{=response.respondentResponses}}">
				<Parameters>
					<Parameter Name="survey_abbr" Value="{{=parameters.survey_abbr}}" />
				</Parameters>

				<User>
					<Parameter Name="survey_abbr">
						<Documentation>Required. Specify survey.</Documentation>
					</Parameter>
				</User>
			</Read>
		</Resource>
	</Resources>

	<Script>
		<Module Name="Main">
			<![CDATA[
require('underscore');
]]>
		</Module>
	</Script>
</ConnectionManager>