Response FileClient::Flush()

in sdk/storage/azure-storage-files-datalake/src/rest_client.cpp [612:736]


    Response<Models::FlushFileResult> FileClient::Flush(
        Core::Http::_internal::HttpPipeline& pipeline,
        const Core::Url& url,
        const FlushFileOptions& options,
        const Core::Context& context)
    {
      auto request = Core::Http::Request(Core::Http::HttpMethod::Patch, url);
      request.GetUrl().AppendQueryParameter("action", "flush");
      if (options.Position.HasValue())
      {
        request.GetUrl().AppendQueryParameter("position", std::to_string(options.Position.Value()));
      }
      if (options.RetainUncommittedData.HasValue())
      {
        request.GetUrl().AppendQueryParameter(
            "retainUncommittedData", options.RetainUncommittedData.Value() ? "true" : "false");
      }
      if (options.Close.HasValue())
      {
        request.GetUrl().AppendQueryParameter("close", options.Close.Value() ? "true" : "false");
      }
      request.SetHeader("Content-Length", "0");
      if (options.ContentMD5.HasValue()
          && !Core::Convert::Base64Encode(options.ContentMD5.Value()).empty())
      {
        request.SetHeader(
            "x-ms-content-md5", Core::Convert::Base64Encode(options.ContentMD5.Value()));
      }
      if (options.LeaseId.HasValue() && !options.LeaseId.Value().empty())
      {
        request.SetHeader("x-ms-lease-id", options.LeaseId.Value());
      }
      if (options.LeaseAction.HasValue() && !options.LeaseAction.Value().ToString().empty())
      {
        request.SetHeader("x-ms-lease-action", options.LeaseAction.Value().ToString());
      }
      if (options.LeaseDuration.HasValue())
      {
        request.SetHeader("x-ms-lease-duration", std::to_string(options.LeaseDuration.Value()));
      }
      if (options.ProposedLeaseId.HasValue() && !options.ProposedLeaseId.Value().empty())
      {
        request.SetHeader("x-ms-proposed-lease-id", options.ProposedLeaseId.Value());
      }
      if (options.CacheControl.HasValue() && !options.CacheControl.Value().empty())
      {
        request.SetHeader("x-ms-cache-control", options.CacheControl.Value());
      }
      if (options.ContentType.HasValue() && !options.ContentType.Value().empty())
      {
        request.SetHeader("x-ms-content-type", options.ContentType.Value());
      }
      if (options.ContentDisposition.HasValue() && !options.ContentDisposition.Value().empty())
      {
        request.SetHeader("x-ms-content-disposition", options.ContentDisposition.Value());
      }
      if (options.ContentEncoding.HasValue() && !options.ContentEncoding.Value().empty())
      {
        request.SetHeader("x-ms-content-encoding", options.ContentEncoding.Value());
      }
      if (options.ContentLanguage.HasValue() && !options.ContentLanguage.Value().empty())
      {
        request.SetHeader("x-ms-content-language", options.ContentLanguage.Value());
      }
      if (options.IfMatch.HasValue() && !options.IfMatch.ToString().empty())
      {
        request.SetHeader("If-Match", options.IfMatch.ToString());
      }
      if (options.IfNoneMatch.HasValue() && !options.IfNoneMatch.ToString().empty())
      {
        request.SetHeader("If-None-Match", options.IfNoneMatch.ToString());
      }
      if (options.IfModifiedSince.HasValue())
      {
        request.SetHeader(
            "If-Modified-Since",
            options.IfModifiedSince.Value().ToString(Azure::DateTime::DateFormat::Rfc1123));
      }
      if (options.IfUnmodifiedSince.HasValue())
      {
        request.SetHeader(
            "If-Unmodified-Since",
            options.IfUnmodifiedSince.Value().ToString(Azure::DateTime::DateFormat::Rfc1123));
      }
      request.SetHeader("x-ms-version", "2024-08-04");
      if (options.EncryptionKey.HasValue() && !options.EncryptionKey.Value().empty())
      {
        request.SetHeader("x-ms-encryption-key", options.EncryptionKey.Value());
      }
      if (options.EncryptionKeySha256.HasValue()
          && !Core::Convert::Base64Encode(options.EncryptionKeySha256.Value()).empty())
      {
        request.SetHeader(
            "x-ms-encryption-key-sha256",
            Core::Convert::Base64Encode(options.EncryptionKeySha256.Value()));
      }
      if (options.EncryptionAlgorithm.HasValue() && !options.EncryptionAlgorithm.Value().empty())
      {
        request.SetHeader("x-ms-encryption-algorithm", options.EncryptionAlgorithm.Value());
      }
      auto pRawResponse = pipeline.Send(request, context);
      auto httpStatusCode = pRawResponse->GetStatusCode();
      if (httpStatusCode != Core::Http::HttpStatusCode::Ok)
      {
        throw StorageException::CreateFromResponse(std::move(pRawResponse));
      }
      Models::FlushFileResult response;
      response.ETag = ETag(pRawResponse->GetHeaders().at("ETag"));
      response.LastModified = DateTime::Parse(
          pRawResponse->GetHeaders().at("Last-Modified"), Azure::DateTime::DateFormat::Rfc1123);
      response.FileSize = std::stoll(pRawResponse->GetHeaders().at("Content-Length"));
      response.IsServerEncrypted
          = pRawResponse->GetHeaders().at("x-ms-request-server-encrypted") == std::string("true");
      if (pRawResponse->GetHeaders().count("x-ms-encryption-key-sha256") != 0)
      {
        response.EncryptionKeySha256 = Core::Convert::Base64Decode(
            pRawResponse->GetHeaders().at("x-ms-encryption-key-sha256"));
      }
      if (pRawResponse->GetHeaders().count("x-ms-lease-renewed") != 0)
      {
        response.IsLeaseRenewed
            = pRawResponse->GetHeaders().at("x-ms-lease-renewed") == std::string("true");
      }
      return Response<Models::FlushFileResult>(std::move(response), std::move(pRawResponse));
    }