Link applicant to files in S3.
Accessing secure file uploads from Fountain
This is part 2 of adding files to an applicant's profile. Refer to File Upload to S3 for part 1.
"Key" clarificationThe
keyis Fountain's identifier for the file you're uploading, known in the product as a Data Key. Pass the S3 object key returned by the upload ass3_key.
Use the same key for similar files you're collecting from applicants to prevent duplicate data keys. For example, use resume_file for all applicants' resumes.
Secure data access is requiredIf your call returns with
Secure data access is required, contact [email protected] to enable your account to access secure data.
Gateway authentication and storage requests
The examples use an OAuth access token with the employer scope for Fountain requests at https://services.fountain.com/api/servicehire/v2. Obtain the token through the client-credentials flow at https://services.fountain.com/api/servicesecurity/processes/apikey/oauth/token before running them.
The upload response's url is the S3 upload destination, not the Fountain gateway. Use that URL and its signed form_data unchanged, with the empty content-type field shown below. Do not replace the S3 host or send a Fountain Authorization or X-ACCESS-TOKEN header to S3. After S3 returns the object key, send the link request to Fountain with OAuth.
Optional attribution
Set attribute_to_applicant to true to show the applicant as the actor for the upload in the History panel. Omit this option to attribute the upload to the caller. A Fountain One OAuth token attributes it to the Hire user with the token's email: your own user for a personal key, the integration's user for an integration key. A Hire API key, or a token whose email matches no Hire user, attributes it to the account owner. A request that sends both a Hire API key and a token is attributed by the Hire API key.
import json
import xml.etree.ElementTree
import mimetypes
import os
import requests
BASE_URL="https://services.fountain.com/api/servicehire/v2"
ACCESS_TOKEN=os.environ["FOUNTAIN_ACCESS_TOKEN"]
APPLICANT="6d0d9434-2817-4b96-9ad4-e33a7f2a0adb"
FILENAME="dog.jpg"
UPLOAD_FILE=(FILENAME, open(FILENAME, 'rb'), mimetypes.guess_type(FILENAME)[0])
UPLOAD_KEY="test"
UPLOAD_URL="%s/applicants/%s/secure_documents/upload" % (BASE_URL, APPLICANT)
LINK_UPLOAD_URL="%s/applicants/%s/secure_documents/link_upload" % (BASE_URL, APPLICANT)
HEADERS = { 'Authorization': 'Bearer ' + ACCESS_TOKEN }
upload_response = requests.post(UPLOAD_URL, headers=HEADERS)
upload_response.raise_for_status()
upload_response_json = json.loads(upload_response.text)
form_data = upload_response_json['form_data']
form_data['Content-type'] = ""
s3_upload_response = requests.post(upload_response_json['url'], data=form_data, files={'file': UPLOAD_FILE})
s3_upload_response.raise_for_status()
s3_upload_response_xml = xml.etree.ElementTree.fromstring(s3_upload_response.text)
s3_filename = s3_upload_response_xml.find("Key").text
finish_upload_data = {
'key': UPLOAD_KEY,
's3_key': s3_filename,
'size': os.path.getsize(FILENAME),
}
finish_upload_response = requests.post(LINK_UPLOAD_URL, headers=HEADERS, json=finish_upload_data)
finish_upload_response.raise_for_status()
print(finish_upload_response)using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using System.Xml.XPath;
var localFilePath = @"/Users/userName/Downloads/the_file.png";
var baseUrl = "https://services.fountain.com/api/servicehire/v2";
var accessToken = Environment.GetEnvironmentVariable("FOUNTAIN_ACCESS_TOKEN") ?? throw new InvalidOperationException("Set FOUNTAIN_ACCESS_TOKEN");
var applicantId = "29345e1b-6224-467a-964c-da077ebed994";
// Open file stream to the local file on disk
using var fileStream = new FileStream(localFilePath, FileMode.Open);
// Send the OAuth token only to the Fountain gateway
using var hireClient = new HttpClient();
hireClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
// Get an upload URL so that the file can be uploaded to the applicant
var urlResponse = await hireClient.PostAsync($"{baseUrl}/applicants/{applicantId}/secure_documents/upload", null);
urlResponse.EnsureSuccessStatusCode();
var presignedPayload = await urlResponse.Content.ReadFromJsonAsync<Payload>(new JsonSerializerOptions { PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower });
// Upload the file to the presigned URL with a multipart form
var formData = new MultipartFormDataContent();
// Copy the form data from the presignedPayload to the formData
// This has the x-amz-signature, target s3 key and other fields from a presigned upload payload
foreach (var item in presignedPayload.FormData)
{
formData.Add(new StringContent(item.Value), item.Key);
}
formData.Add(new StringContent(""), "Content-Type"); // required due to a content validation rule
formData.Add(new StreamContent(fileStream), "file", Path.GetFileName(localFilePath));
// POST the file to the presigned URL (S3), without Fountain headers
using var s3Client = new HttpClient();
var uploadResponse = await s3Client.PostAsync(presignedPayload.Url, formData);
// You would want to use ReadAsStreamAsync here and pass it directly to XPathDocument but
// this makes it easy to log the response if there was a problem
var textResponse = await uploadResponse.Content.ReadAsStringAsync();
if (uploadResponse.StatusCode != HttpStatusCode.Created)
{
Console.WriteLine("Error uploading file");
Console.WriteLine(textResponse);
return;
}
var xmlDocument = new XPathDocument(new StringReader(textResponse));
var navigator = xmlDocument.CreateNavigator();
var keyNode = navigator.SelectSingleNode("/PostResponse/Key");
if (keyNode is null) {
Console.WriteLine("Unexpected response from S3");
Console.WriteLine(textResponse);
return;
}
// Now link the uploaded file residing on S3 to the applicant
var linkResponse = await hireClient.PostAsJsonAsync($"{baseUrl}/applicants/{applicantId}/secure_documents/link_upload", new LinkPayload
{
Key = "some_file", // this is the data key you are uploading the file to
S3Key = keyNode.Value,
Size = fileStream.Length.ToString()
}, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower });
if (linkResponse.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine($"Error linking file: {linkResponse.StatusCode}");
return;
}
public readonly struct LinkPayload
{
public string Key { get; init; } // data key
public string S3Key { get; init; } // s3 key
public string Size { get; init; }
}
public readonly struct Payload
{
public string Url { get; init; }
public string Host { get; init; }
public Dictionary<string, string> FormData { get; init; }
public int MinSize { get; init; }
public int MaxSize { get; init; }
public bool OnlyImages { get; init; }
public string Type { get; init; }
}