Response PageBlobClient::Create()

in sdk/storage/azure-storage-blobs/src/rest_client.cpp [5842:5991]


    Response<Models::CreatePageBlobResult> PageBlobClient::Create(
        Core::Http::_internal::HttpPipeline& pipeline,
        const Core::Url& url,
        const CreatePageBlobOptions& options,
        const Core::Context& context)
    {
      auto request = Core::Http::Request(Core::Http::HttpMethod::Put, url);
      request.SetHeader("x-ms-blob-type", "PageBlob");
      request.SetHeader("Content-Length", "0");
      if (options.Tier.HasValue() && !options.Tier.Value().ToString().empty())
      {
        request.SetHeader("x-ms-access-tier", options.Tier.Value().ToString());
      }
      if (!options.BlobContentType.empty())
      {
        request.SetHeader("x-ms-blob-content-type", options.BlobContentType);
      }
      if (!options.BlobContentEncoding.empty())
      {
        request.SetHeader("x-ms-blob-content-encoding", options.BlobContentEncoding);
      }
      if (!options.BlobContentLanguage.empty())
      {
        request.SetHeader("x-ms-blob-content-language", options.BlobContentLanguage);
      }
      if (!Core::Convert::Base64Encode(options.BlobContentMD5).empty())
      {
        request.SetHeader(
            "x-ms-blob-content-md5", Core::Convert::Base64Encode(options.BlobContentMD5));
      }
      if (!options.BlobCacheControl.empty())
      {
        request.SetHeader("x-ms-blob-cache-control", options.BlobCacheControl);
      }
      for (const auto& p : options.Metadata)
      {
        request.SetHeader("x-ms-meta-" + p.first, p.second);
      }
      if (options.LeaseId.HasValue() && !options.LeaseId.Value().empty())
      {
        request.SetHeader("x-ms-lease-id", options.LeaseId.Value());
      }
      if (!options.BlobContentDisposition.empty())
      {
        request.SetHeader("x-ms-blob-content-disposition", options.BlobContentDisposition);
      }
      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());
      }
      if (options.EncryptionScope.HasValue() && !options.EncryptionScope.Value().empty())
      {
        request.SetHeader("x-ms-encryption-scope", options.EncryptionScope.Value());
      }
      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));
      }
      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.IfTags.HasValue() && !options.IfTags.Value().empty())
      {
        request.SetHeader("x-ms-if-tags", options.IfTags.Value());
      }
      request.SetHeader("x-ms-blob-content-length", std::to_string(options.BlobContentLength));
      if (options.BlobSequenceNumber.HasValue())
      {
        request.SetHeader(
            "x-ms-blob-sequence-number", std::to_string(options.BlobSequenceNumber.Value()));
      }
      request.SetHeader("x-ms-version", "2024-08-04");
      if (options.BlobTagsString.HasValue() && !options.BlobTagsString.Value().empty())
      {
        request.SetHeader("x-ms-tags", options.BlobTagsString.Value());
      }
      if (options.ImmutabilityPolicyExpiry.HasValue())
      {
        request.SetHeader(
            "x-ms-immutability-policy-until-date",
            options.ImmutabilityPolicyExpiry.Value().ToString(
                Azure::DateTime::DateFormat::Rfc1123));
      }
      if (options.ImmutabilityPolicyMode.HasValue()
          && !options.ImmutabilityPolicyMode.Value().ToString().empty())
      {
        request.SetHeader(
            "x-ms-immutability-policy-mode", options.ImmutabilityPolicyMode.Value().ToString());
      }
      if (options.LegalHold.HasValue())
      {
        request.SetHeader("x-ms-legal-hold", options.LegalHold.Value() ? "true" : "false");
      }
      auto pRawResponse = pipeline.Send(request, context);
      auto httpStatusCode = pRawResponse->GetStatusCode();
      if (httpStatusCode != Core::Http::HttpStatusCode::Created)
      {
        throw StorageException::CreateFromResponse(std::move(pRawResponse));
      }
      Models::CreatePageBlobResult response;
      if (pRawResponse->GetHeaders().count("ETag") != 0)
      {
        response.ETag = ETag(pRawResponse->GetHeaders().at("ETag"));
      }
      if (pRawResponse->GetHeaders().count("Last-Modified") != 0)
      {
        response.LastModified = DateTime::Parse(
            pRawResponse->GetHeaders().at("Last-Modified"), Azure::DateTime::DateFormat::Rfc1123);
      }
      if (pRawResponse->GetHeaders().count("x-ms-version-id") != 0)
      {
        response.VersionId = pRawResponse->GetHeaders().at("x-ms-version-id");
      }
      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-encryption-scope") != 0)
      {
        response.EncryptionScope = pRawResponse->GetHeaders().at("x-ms-encryption-scope");
      }
      return Response<Models::CreatePageBlobResult>(std::move(response), std::move(pRawResponse));
    }