-
Notifications
You must be signed in to change notification settings - Fork 263
/
DurableHttpResponse.cs
85 lines (76 loc) · 3.28 KB
/
DurableHttpResponse.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
namespace Microsoft.Azure.WebJobs.Extensions.DurableTask
{
/// <summary>
/// Response received from the HTTP request made by the Durable Function.
/// </summary>
public class DurableHttpResponse
{
/// <summary>
/// Initializes a new instance of the <see cref="DurableHttpResponse"/> class.
/// </summary>
/// <param name="statusCode">HTTP Status code returned from the HTTP call.</param>
/// <param name="headers">Headers returned from the HTTP call.</param>
/// <param name="content">Content returned from the HTTP call.</param>
[JsonConstructor]
public DurableHttpResponse(
HttpStatusCode statusCode,
IDictionary<string, StringValues> headers = null,
string content = null)
{
this.StatusCode = statusCode;
this.Headers = HttpHeadersConverter.CreateCopy(headers);
this.Content = content;
}
/// <summary>
/// Status code returned from an HTTP request.
/// </summary>
[JsonProperty("statusCode")]
public HttpStatusCode StatusCode { get; }
/// <summary>
/// Headers in the response from an HTTP request.
/// </summary>
[JsonProperty("headers")]
[JsonConverter(typeof(HttpHeadersConverter))]
public IDictionary<string, StringValues> Headers { get; }
/// <summary>
/// Content returned from an HTTP request.
/// </summary>
[JsonProperty("content")]
public string Content { get; }
/// <summary>
/// Creates a DurableHttpResponse from an HttpResponseMessage.
/// </summary>
/// <param name="httpResponseMessage">HttpResponseMessage returned from the HTTP call.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
public static async Task<DurableHttpResponse> CreateDurableHttpResponseWithHttpResponseMessage(HttpResponseMessage httpResponseMessage)
{
DurableHttpResponse durableHttpResponse = new DurableHttpResponse(
statusCode: httpResponseMessage.StatusCode,
headers: CreateStringValuesHeaderDictionary(httpResponseMessage.Headers),
content: await httpResponseMessage.Content.ReadAsStringAsync());
return durableHttpResponse;
}
private static IDictionary<string, StringValues> CreateStringValuesHeaderDictionary(IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers)
{
IDictionary<string, StringValues> newHeaders = new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase);
if (headers != null)
{
foreach (var header in headers)
{
newHeaders[header.Key] = new StringValues(header.Value.ToArray());
}
}
return newHeaders;
}
}
}