forked from chadly/Geocoding.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseRequest.cs
More file actions
143 lines (126 loc) · 2.98 KB
/
BaseRequest.cs
File metadata and controls
143 lines (126 loc) · 2.98 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Text;
using Newtonsoft.Json;
namespace Geocoding.MapQuest
{
/// <summary>
/// Geo-code request object
/// <see cref="http://open.mapquestapi.com/geocoding/"/>
/// </summary>
public abstract class BaseRequest
{
protected BaseRequest(string key) //output only, no need for default ctor
{
Key = key;
}
[JsonIgnore]
string key;
/// <summary>
/// A REQUIRED unique key to authorize use of the Routing Service.
/// <see cref="http://developer.mapquest.com/"/>
/// </summary>
[JsonIgnore]
public virtual string Key
{
get { return key; }
set
{
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException("An application key is required for MapQuest");
key = value;
}
}
/// <summary>
/// Defaults to json
/// </summary>
[JsonIgnore]
public virtual DataFormat InputFormat { get; private set; }
/// <summary>
/// Defaults to json
/// </summary>
[JsonIgnore]
public virtual DataFormat OutputFormat { get; private set; }
[JsonIgnore]
RequestOptions op = new RequestOptions();
/// <summary>
/// Optional settings
/// </summary>
[JsonProperty("options")]
public virtual RequestOptions Options
{
get { return op; }
protected set
{
if (value == null)
throw new ArgumentNullException("Options");
op = value;
}
}
/// <summary>
/// if true, use Open Street Map, else use commercial map
/// </summary>
public virtual bool UseOSM { get; set; }
/// <summary>
/// We are using v1 of MapQuest OSM API
/// </summary>
protected virtual string BaseRequestPath
{
get
{
if (UseOSM)
return @"http://open.mapquestapi.com/geocoding/v1/";
else
return @"http://www.mapquestapi.com/geocoding/v1/";
}
}
/// <summary>
/// The full path for the request
/// </summary>
[JsonIgnore]
public virtual Uri RequestUri
{
get
{
var sb = new StringBuilder(BaseRequestPath);
sb.Append(RequestAction);
sb.Append("?");
//no need to escape this key, it is already escaped by MapQuest at generation
sb.AppendFormat("key={0}&", Key);
if (InputFormat != DataFormat.json)
sb.AppendFormat("inFormat={0}&", InputFormat);
if (OutputFormat != DataFormat.json)
sb.AppendFormat("outFormat={0}&", OutputFormat);
sb.Length--;
return new Uri(sb.ToString());
}
}
[JsonIgnore]
public abstract string RequestAction { get; }
[JsonIgnore]
string _verb = "POST";
/// <summary>
/// Default request verb is POST for security and large batch payloads
/// </summary>
[JsonIgnore]
public virtual string RequestVerb
{
get { return _verb; }
protected set { _verb = string.IsNullOrWhiteSpace(value) ? "POST" : value.Trim().ToUpper(); }
}
/// <summary>
/// Request body if request verb is applicable (POST, PUT, etc)
/// </summary>
[JsonIgnore]
public virtual string RequestBody
{
get
{
return this.ToJSON();
}
}
public override string ToString()
{
return this.RequestBody;
}
}
}