forked from linguanostra/GoogleMapsAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebRequestUtility.cs
More file actions
115 lines (87 loc) · 2.79 KB
/
Copy pathWebRequestUtility.cs
File metadata and controls
115 lines (87 loc) · 2.79 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
113
114
115
using System;
using System.Net;
using GoogleMapsAPI.NET.API.Client;
using GoogleMapsAPI.NET.Requests.Interfaces;
namespace GoogleMapsAPI.NET.Requests
{
/// <summary>
/// Web request utility
/// </summary>
public class WebRequestUtility : IWebRequestUtility
{
#region Properties
/// <summary>
/// API client
/// </summary>
public MapsAPIClient Client { get; }
#endregion
#region Constructors
/// <summary>
/// Create a new instance
/// </summary>
/// <param name="client">API client</param>
public WebRequestUtility(MapsAPIClient client)
{
Client = client;
}
#endregion
#region Virtual methods
/// <summary>
/// Create web request
/// </summary>
/// <param name="requestUri">Request Uri</param>
/// <returns>Result request</returns>
public virtual HttpWebRequest CreateWebRequest(string requestUri)
{
return (HttpWebRequest)WebRequest.Create(requestUri);
}
/// <summary>
/// Execute GET web request
/// </summary>
/// <param name="requestUri">Request Uri</param>
/// <param name="config">Config</param>
/// <returns>Response result</returns>
public virtual HttpWebResponse Get(string requestUri, RequestConfig config)
{
// Create web request
var webRequest = CreateWebRequest(requestUri);
// Set method
webRequest.Method = "GET";
// Set (non restricted) headers
webRequest.Headers = config.Headers.ToWebHeaderCollection(true);
// Set User-Agent
webRequest.UserAgent = config.Headers.UserAgent;
// Set timeout, if required
if (config.Timeout > 0)
{
webRequest.Timeout = config.Timeout;
}
// Get response
var response = (HttpWebResponse)webRequest.GetResponse();
// Return response
return response;
}
/// <summary>
/// Append credentials from client to given url
/// </summary>
/// <param name="url">Url</param>
/// <returns>Result url</returns>
public string AppendCredentialsTourl(string url)
{
// Check credentials
// API key
if (Client.IsUsingAPIKey)
{
return $"{url}&key={Client.APIKey}";
}
// Enterprise credentials
if (Client.IsUsingEnterpriseCredentials)
{
throw new NotImplementedException();
}
// Nothing to do
return url;
}
#endregion
}
}