override def formSubmissionsForGivenPage()

in formstack-baton-requests/src/main/scala/com/gu/identity/formstackbatonrequests/services/FormstackService.scala [57:102]


  override def formSubmissionsForGivenPage(
    page: Int,
    formId: String,
    minTimeUTC: LocalDateTime,
    maxTimeUTC: Option[LocalDateTime],
    encryptionPassword: String,
    accountToken: FormstackAccountToken): Either[Throwable, FormSubmissions] = {


    val maybeMaxTimeFilter = maxTimeUTC.map{ m => ("max_time", DateHelpers.toEasternTimeString(m) )}

    val response = http(s"https://www.formstack.com/api/v2/form/$formId/submission.json")
      .headers(
        Seq(
          ("Authorization", accountToken.secret),
          ("X-FS-ENCRYPTION-PASSWORD", encryptionPassword)
        )
      )
      .params(
        Seq(
          ("page", page.toString),
          ("per_page", submissionResultsPerPage.toString),
          ("data", "true"),
          ("expand_data", "true"),
          ("sort", "DESC"),
          // this api call expects eastern time zone, see https://developers.formstack.com/reference/form-id-submission-get
          ("min_time", DateHelpers.toEasternTimeString(minTimeUTC)),
        ) ++ maybeMaxTimeFilter
      ).asString

    if(!response.is2xx) {
      logger.error(response.body)
    }

    /* There are a couple of forms that the Formstack API can't seem to decrypt. There seems to be no way around this
     *  so we capture this specific error and skip these forms. */
    // We also skip responses from Formstack that contain "Form encryption password invalid". These are undocumented, and we do not know why they happen.

    response.body match {
      case message if message.contains("An error occurred while decrypting the submissions") =>
        Left(FormstackDecryptionError(s"${response.body} | form id: ${formId}"))
      case message if message.contains("Form encryption password invalid") =>
        Left(FormstackAuthError(s"${response.body} | form id: ${formId}"))
      case _ => decode[FormSubmissions](response.body)
    }
  }