LEFT | RIGHT |
1 /* | 1 /* |
2 Copyright 2012 Google Inc | 2 Copyright 2012 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0(the "License"); | 4 Licensed under the Apache License, Version 2.0(the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
11 distributed under the License is distributed on an "AS IS" BASIS, | 11 distributed under the License is distributed on an "AS IS" BASIS, |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 See the License for the specific language governing permissions and | 13 See the License for the specific language governing permissions and |
14 limitations under the License. | 14 limitations under the License. |
15 */ | 15 */ |
16 | 16 |
17 using System; | 17 using System; |
18 using System.Collections.Generic; | 18 using System.Collections.Generic; |
19 using System.Linq; | 19 using System.Linq; |
20 using System.Text; | 20 using System.Text; |
21 using System.Net.Http; | 21 using System.Net.Http; |
22 | 22 |
23 using Google.Apis.Util; | 23 using Google.Apis.Util; |
24 using Google.Apis.Http; | 24 using Google.Apis.Http; |
25 | 25 |
26 namespace Google.Apis.Requests | 26 namespace Google.Apis.Requests |
27 { | 27 { |
28 /// <summary> Utility class used to build a request from the various parts o
f a REST Method call. </summary> | 28 /// <summary>Utility class for building a URI using <see cref="BuildUri"/> o
r a HTTP request using· |
| 29 /// <see cref="CreateRequest"/> from the query and path parameters of a REST
call.</summary> |
29 public class RequestBuilder | 30 public class RequestBuilder |
30 { | 31 { |
31 /// <summary> Supported Http methods. </summary> | 32 /// <summary>Supported HTTP methods.</summary> |
32 private static IEnumerable<string> SupportedMethods = new List<string>· | 33 private static IEnumerable<string> SupportedMethods = new List<string>· |
33 {· | 34 {· |
34 HttpConsts.Get, HttpConsts.Post, HttpConsts.Put, HttpConsts.Dele
te, HttpConsts.Patch· | 35 HttpConsts.Get, HttpConsts.Post, HttpConsts.Put, HttpConsts.Dele
te, HttpConsts.Patch· |
35 }; | 36 }; |
36 | 37 |
37 /// <summary> | 38 /// <summary> |
38 /// A dictionary containing the parameters which will be inserted into t
he path | 39 /// A dictionary containing the parameters which will be inserted into t
he path |
39 /// of the URI. These parameters will be substituted into the URI path w
here the· | 40 /// of the URI. These parameters will be substituted into the URI path w
here the· |
40 /// path contains "{key}" that portion of the path will be replaced by t
he value· | 41 /// path contains "{key}" that portion of the path will be replaced by t
he value· |
41 /// for the specified key in this dictionary. | 42 /// for the specified key in this dictionary. |
(...skipping 10 matching lines...) Expand all Loading... |
52 /// The base uri for this request (usually applies to the service itself
). | 53 /// The base uri for this request (usually applies to the service itself
). |
53 /// </summary> | 54 /// </summary> |
54 public Uri BaseUri { get; set; } | 55 public Uri BaseUri { get; set; } |
55 | 56 |
56 /// <summary> | 57 /// <summary> |
57 /// The path portion of this request. Appended to the <see cref="BaseUri
"/> and | 58 /// The path portion of this request. Appended to the <see cref="BaseUri
"/> and |
58 /// the parameters are substituted from the <see cref="PathParameters"/>
dictionary. | 59 /// the parameters are substituted from the <see cref="PathParameters"/>
dictionary. |
59 /// </summary> | 60 /// </summary> |
60 public string Path { get; set; } | 61 public string Path { get; set; } |
61 | 62 |
62 /// <summary> The Http method used for this request. </summary> | 63 /// <summary>The HTTP method used for this request.</summary> |
63 private string method; | 64 private string method; |
64 | 65 |
65 /// <summary> The Http method used for this request (such as GET, PUT, P
OST, etc...) </summary> | 66 /// <summary>The HTTP method used for this request (such as GET, PUT, PO
ST, etc...) </summary> |
66 /// <remarks> Default Value is <see cref="Google.Apis.Http.HttpConsts.Ge
t"/>. </remarks> | 67 /// <remarks> Default Value is <see cref="Google.Apis.Http.HttpConsts.Ge
t"/>. </remarks> |
67 public string Method | 68 public string Method |
68 { | 69 { |
69 get { return method; } | 70 get { return method; } |
70 set | 71 set |
71 { | 72 { |
72 if (!SupportedMethods.Contains(value)) | 73 if (!SupportedMethods.Contains(value)) |
73 throw new ArgumentOutOfRangeException("Method"); | 74 throw new ArgumentOutOfRangeException("Method"); |
74 method = value; | 75 method = value; |
75 } | 76 } |
76 } | 77 } |
77 | 78 |
78 /// <summary> Construct a new request builder. </summary> | 79 /// <summary>Construct a new request builder.</summary> |
79 public RequestBuilder() | 80 public RequestBuilder() |
80 { | 81 { |
81 this.PathParameters = new Dictionary<string, string>(); | 82 this.PathParameters = new Dictionary<string, string>(); |
82 this.QueryParameters = new List<KeyValuePair<string, string>>(); | 83 this.QueryParameters = new List<KeyValuePair<string, string>>(); |
83 this.Method = HttpConsts.Get; | 84 this.Method = HttpConsts.Get; |
84 } | 85 } |
85 | 86 |
86 /// <summary> Constructs a Uri as defined by the parts of this request b
uilder. </summary> | 87 /// <summary>Constructs a Uri as defined by the parts of this request bu
ilder.</summary> |
87 public Uri BuildUri() | 88 public Uri BuildUri() |
88 { | 89 { |
89 var restPath = new StringBuilder(PathParameters | 90 var restPath = new StringBuilder(PathParameters |
90 .Select(param => new { Token = "{" param.Key "}", Value = Ur
i.EscapeDataString(param.Value) }) | 91 .Select(param => new { Token = "{" param.Key "}", Value = Ur
i.EscapeDataString(param.Value) }) |
91 .Aggregate(this.Path, (path, param) => path.Replace(param.Token,
param.Value))); | 92 .Aggregate(this.Path, (path, param) => path.Replace(param.Token,
param.Value))); |
92 | 93 |
93 if (QueryParameters.Count > 0) | 94 if (QueryParameters.Count > 0) |
94 { | 95 { |
95 restPath.Append("?"); | 96 restPath.Append("?"); |
96 // If parameter value is empty - just add the "name", otherwise
"name=value" | 97 // If parameter value is empty - just add the "name", otherwise
"name=value" |
97 restPath.Append(String.Join("&", QueryParameters.Select( | 98 restPath.Append(String.Join("&", QueryParameters.Select( |
98 x => string.IsNullOrEmpty(x.Value) ? | 99 x => string.IsNullOrEmpty(x.Value) ? |
99 Uri.EscapeDataString(x.Key) : | 100 Uri.EscapeDataString(x.Key) : |
100 String.Format("{0}={1}", Uri.EscapeDataString(x.Key), Ur
i.EscapeDataString(x.Value))) | 101 String.Format("{0}={1}", Uri.EscapeDataString(x.Key), Ur
i.EscapeDataString(x.Value))) |
101 .ToArray())); | 102 .ToArray())); |
102 } | 103 } |
103 | 104 |
104 return new Uri(this.BaseUri, restPath.ToString()); | 105 return new Uri(this.BaseUri, restPath.ToString()); |
105 } | 106 } |
106 | 107 |
107 /// <summary> Adds a parameter value. </summary> | 108 /// <summary>Adds a parameter value.</summary> |
108 /// <param name="type">Type of the parameter (must be Path or Query).</p
aram> | 109 /// <param name="type">Type of the parameter (must be Path or Query).</p
aram> |
109 /// <param name="name">Parameter name.</param> | 110 /// <param name="name">Parameter name.</param> |
110 /// <param name="value">Parameter value.</param> | 111 /// <param name="value">Parameter value.</param> |
111 public void AddParameter(RequestParameterType type, string name, string
value) | 112 public void AddParameter(RequestParameterType type, string name, string
value) |
112 { | 113 { |
113 switch (type) | 114 switch (type) |
114 { | 115 { |
115 case RequestParameterType.Path: | 116 case RequestParameterType.Path: |
116 if (string.IsNullOrEmpty(value)) | 117 if (string.IsNullOrEmpty(value)) |
117 { | 118 { |
118 throw new ArgumentException("Path parameters cannot be n
ull or empty."); | 119 throw new ArgumentException("Path parameters cannot be n
ull or empty."); |
119 } | 120 } |
120 PathParameters.Add(name, value); | 121 PathParameters.Add(name, value); |
121 break; | 122 break; |
122 case RequestParameterType.Query: | 123 case RequestParameterType.Query: |
123 if (value == null) // don't allow null values on query (empt
y value is valid) | 124 if (value == null) // don't allow null values on query (empt
y value is valid) |
124 { | 125 { |
125 break; | 126 break; |
126 } | 127 } |
127 QueryParameters.Add(new KeyValuePair<string, string>(name, v
alue)); | 128 QueryParameters.Add(new KeyValuePair<string, string>(name, v
alue)); |
128 break; | 129 break; |
129 default: | 130 default: |
130 throw new ArgumentOutOfRangeException("type"); | 131 throw new ArgumentOutOfRangeException("type"); |
131 } | 132 } |
132 } | 133 } |
133 | 134 |
134 /// <summary> Creates a new HTTP request message. </summary> | 135 /// <summary>Creates a new HTTP request message.</summary> |
135 public HttpRequestMessage CreateRequest() | 136 public HttpRequestMessage CreateRequest() |
136 { | 137 { |
137 return new HttpRequestMessage(new HttpMethod(Method), BuildUri()); | 138 return new HttpRequestMessage(new HttpMethod(Method), BuildUri()); |
138 } | 139 } |
139 } | 140 } |
140 } | 141 } |
LEFT | RIGHT |