Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 Copyright 2013 Google Inc | 2 Copyright 2013 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.Net.Http; | 19 using System.Net.Http; |
20 using System.Linq; | 20 using System.Linq; |
21 using System.Reflection; | 21 using System.Reflection; |
22 | 22 |
23 using Google.Apis.Util; | 23 using Google.Apis.Util; |
24 | 24 |
25 namespace Google.Apis.Requests.Parameters | 25 namespace Google.Apis.Requests.Parameters |
26 { | 26 { |
27 /// <summary> | |
28 /// Utility class for iterating on <seealso cref="RequestParameterAttribute" /> properties in a request object. | |
29 /// </summary> | |
27 public static class ParameterUtils | 30 public static class ParameterUtils |
peleyal
2013/09/10 01:14:44
add comment
peleyal
2013/09/10 12:32:22
Done.
| |
28 { | 31 { |
29 /// <summary> | 32 /// <summary> |
30 /// Creates a <seealso cref="System.Net.Http.FormUrlEncodedContent"/> wi th all the specified parameters in· | 33 /// Creates a <seealso cref="System.Net.Http.FormUrlEncodedContent"/> wi th all the specified parameters in· |
31 /// input request. It uses reflection to look at all properties marked w ith | 34 /// input request. It uses reflection to iterate over all properties wit h |
peleyal
2013/09/10 01:14:44
to get all properties with the ... attribute
peleyal
2013/09/10 12:32:22
Done.
| |
32 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/>. | 35 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu te. |
33 /// </summary> | 36 /// </summary> |
37 /// <param name="request"> | |
38 /// A request object which contains properties with· | |
39 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu te. Those properties will be serialized | |
40 /// to the returned <seealso cref="System.Net.Http.FormUrlEncodedContent "/>. | |
41 /// </param> | |
42 /// <returns> | |
43 /// A <seealso cref="System.Net.Http.FormUrlEncodedContent"/> which cont ains the all the given object required· | |
44 /// values | |
45 /// </returns> | |
34 public static FormUrlEncodedContent CreateFormUrlEncodedContent(object r equest) | 46 public static FormUrlEncodedContent CreateFormUrlEncodedContent(object r equest) |
35 { | 47 { |
36 IList<KeyValuePair<string, string>> list = new List<KeyValuePair<str ing, string>>(); | 48 IList<KeyValuePair<string, string>> list = new List<KeyValuePair<str ing, string>>(); |
37 IterateParameters(request, (type, name, value) => | 49 IterateParameters(request, (type, name, value) => |
38 { | 50 { |
39 list.Add(new KeyValuePair<string, string>(name, value.ToStri ng())); | 51 list.Add(new KeyValuePair<string, string>(name, value.ToStri ng())); |
40 }); | 52 }); |
41 return new FormUrlEncodedContent(list); | 53 return new FormUrlEncodedContent(list); |
42 } | 54 } |
43 | 55 |
44 /// <summary> | 56 /// <summary> |
45 /// Creates a parameter dictionary by using reflection to look at all pr operties marked with | 57 /// Creates a parameter dictionary by using reflection to iterate over a ll properties with |
peleyal
2013/09/10 01:14:44
look --> get all
peleyal
2013/09/10 12:32:22
Done.
| |
46 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/>. | 58 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu te. |
47 /// </summary> | 59 /// </summary> |
60 /// <param name="request"> | |
61 /// A request object which contains properties with· | |
62 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu te. Those properties will be set | |
63 /// in the output dictionary | |
64 /// </param> | |
48 public static IDictionary<string, object> CreateParameterDictionary(obje ct request) | 65 public static IDictionary<string, object> CreateParameterDictionary(obje ct request) |
49 { | 66 { |
50 var dict = new Dictionary<string, object>(); | 67 var dict = new Dictionary<string, object>(); |
51 IterateParameters(request, (type, name, value) => | 68 IterateParameters(request, (type, name, value) => |
52 { | 69 { |
53 dict.Add(name, value); | 70 dict.Add(name, value); |
54 }); | 71 }); |
55 return dict; | 72 return dict; |
56 } | 73 } |
57 | 74 |
58 /// <summary> | 75 /// <summary> |
59 /// Creates a parameter dictionary by using reflection to look at all pr operties marked with | 76 /// Sets query parameters in the given builder with all all properties w ith the |
peleyal
2013/09/10 01:14:44
ditto
peleyal
2013/09/10 12:32:22
Done.
| |
60 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/>. | 77 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu te. |
61 /// </summary> | 78 /// </summary> |
79 /// <param name="builder">The request builder</param> | |
80 /// <param name="request"> | |
81 /// A request object which contains properties with· | |
82 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu te. Those properties will be set in the· | |
83 /// given request builder object | |
84 /// </param> | |
62 public static void InitParameters(RequestBuilder builder, object request ) | 85 public static void InitParameters(RequestBuilder builder, object request ) |
63 { | 86 { |
64 IterateParameters(request, (type, name, value) => | 87 IterateParameters(request, (type, name, value) => |
65 { | 88 { |
66 builder.AddParameter(type, name, value.ToString()); | 89 builder.AddParameter(type, name, value.ToString()); |
67 }); | 90 }); |
68 } | 91 } |
69 | 92 |
93 /// <summary> | |
94 /// Iterates over all <seealso cref="RequestParameterAttribute"/> proper ties in the request object and invokes | |
95 /// the specified action for each of them. | |
96 /// </summary> | |
97 /// <param name="request">A request object</param> | |
98 /// <param name="action">An action to invoke which gets the parameter ty pe, name and its value</param> | |
70 private static void IterateParameters(object request, Action<RequestPara meterType, string, object> action) | 99 private static void IterateParameters(object request, Action<RequestPara meterType, string, object> action) |
peleyal
2013/09/10 01:14:44
add comment
peleyal
2013/09/10 12:32:22
Done.
| |
71 { | 100 { |
72 // use reflection to build the parameter dictionary. | 101 // Use reflection to build the parameter dictionary. |
73 foreach (PropertyInfo property in request.GetType().GetProperties(Bi ndingFlags.Instance | | 102 foreach (PropertyInfo property in request.GetType().GetProperties(Bi ndingFlags.Instance | |
74 BindingFlags.Public)) | 103 BindingFlags.Public)) |
75 { | 104 { |
76 // retrieve the RequestParameterAttribute | 105 // Retrieve the RequestParameterAttribute. |
77 RequestParameterAttribute attribute = | 106 RequestParameterAttribute attribute = |
78 property.GetCustomAttributes(typeof(RequestParameterAttribut e), false).FirstOrDefault() as | 107 property.GetCustomAttributes(typeof(RequestParameterAttribut e), false).FirstOrDefault() as |
79 RequestParameterAttribute; | 108 RequestParameterAttribute; |
80 if (attribute == null) | 109 if (attribute == null) |
81 { | 110 { |
82 continue; | 111 continue; |
83 } | 112 } |
84 | 113 |
85 // get the name of this parameter from the attribute, if it does n't exist take a lower-case variant of· | 114 // Get the name of this parameter from the attribute, if it does n't exist take a lower-case variant of· |
86 // property name | 115 // property name. |
87 string name = attribute.Name ?? property.Name.ToLower(); | 116 string name = attribute.Name ?? property.Name.ToLower(); |
88 | 117 |
89 var propertyType = property.PropertyType; | 118 var propertyType = property.PropertyType; |
90 var value = property.GetValue(request, null); | 119 var value = property.GetValue(request, null); |
91 | 120 |
92 // call action with the type name and value | 121 // Call action with the type name and value. |
93 if (propertyType.IsValueType || value != null) | 122 if (propertyType.IsValueType || value != null) |
94 { | 123 { |
95 action(attribute.Type, name, value); | 124 action(attribute.Type, name, value); |
96 } | 125 } |
97 } | 126 } |
98 } | 127 } |
99 } | 128 } |
100 } | 129 } |
LEFT | RIGHT |