-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHTTPRequestHandler.cs
More file actions
112 lines (102 loc) · 3.97 KB
/
Copy pathHTTPRequestHandler.cs
File metadata and controls
112 lines (102 loc) · 3.97 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Text.Json;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using contentstack.CMA.Http;
namespace contentstack.CMA
{
internal class HttpRequestHandler
{
private readonly HttpClient _httpClient;
public HttpRequestHandler()
{
_httpClient = new HttpClient();
}
public async Task<string> ProcessRequest(string Url, Dictionary<string, object> Headers, Dictionary<string, object> BodyJson, string FileName = null)
{
// Build query parameters
String queryParam = String.Join("&", BodyJson.Select(kvp => {
var value = "";
if (kvp.Value is string[])
{
string[] vals = (string[])kvp.Value;
value = String.Join("&", vals.Select(item =>
{
return String.Format("{0}={1}", kvp.Key, item);
}));
return value;
}
else if (kvp.Value is Dictionary<string, object>)
value = JsonSerializer.Serialize(kvp.Value);
else
return String.Format("{0}={1}", kvp.Key, kvp.Value);
return String.Format("{0}={1}", kvp.Key, value);
}));
var uri = new Uri(Url + "?" + queryParam);
// Create HTTP request
var request = new HttpRequestMessage(HttpMethod.Get, uri);
request.Headers.Add("x-user-agent", "contentstack-model-generator/0.4.2");
if (Headers != null)
{
foreach (var header in Headers)
{
try
{
// Handle OAuth Bearer token
if (header.Key.Equals("Authorization", StringComparison.OrdinalIgnoreCase))
{
// Check if the value already contains "Bearer" prefix
string authValue = header.Value.ToString();
if (authValue.StartsWith("Bearer "))
{
string token = authValue.Substring(7); // Remove "Bearer " prefix
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
}
else
{
// Value doesn't have Bearer prefix, add it
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authValue);
}
}
else
{
request.Headers.Add(header.Key, header.Value.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
try
{
var response = await _httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
return responseContent;
}
else
{
var errorContent = await response.Content.ReadAsStringAsync();
throw new HttpRequestException($"HTTP request failed with status code: {response.StatusCode} - {errorContent}");
}
}
catch (Exception)
{
throw;
}
}
public void Dispose()
{
_httpClient?.Dispose();
}
}
}