OLD | NEW |
(Empty) | |
| 1 /* |
| 2 Copyright 2013 Google Inc |
| 3 |
| 4 Licensed under the Apache License, Version 2.0 (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 |
| 7 |
| 8 http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
| 10 Unless required by applicable law or agreed to in writing, software |
| 11 distributed under the License is distributed on an "AS IS" BASIS, |
| 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 See the License for the specific language governing permissions and |
| 14 limitations under the License. |
| 15 */ |
| 16 |
| 17 using System; |
| 18 using System.Collections.Generic; |
| 19 using System.Net.Http; |
| 20 using System.Linq; |
| 21 using System.Reflection; |
| 22 |
| 23 using Google.Apis.Util; |
| 24 |
| 25 namespace Google.Apis.Requests.Parameters |
| 26 { |
| 27 /// <summary> |
| 28 /// Utility class for iterating on <seealso cref="RequestParameterAttribute"
/> properties in a request object. |
| 29 /// </summary> |
| 30 public static class ParameterUtils |
| 31 { |
| 32 /// <summary> |
| 33 /// Creates a <seealso cref="System.Net.Http.FormUrlEncodedContent"/> wi
th all the specified parameters in· |
| 34 /// input request. It uses reflection to iterate over all properties wit
h |
| 35 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu
te. |
| 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> |
| 46 public static FormUrlEncodedContent CreateFormUrlEncodedContent(object r
equest) |
| 47 { |
| 48 IList<KeyValuePair<string, string>> list = new List<KeyValuePair<str
ing, string>>(); |
| 49 IterateParameters(request, (type, name, value) => |
| 50 { |
| 51 list.Add(new KeyValuePair<string, string>(name, value.ToStri
ng())); |
| 52 }); |
| 53 return new FormUrlEncodedContent(list); |
| 54 } |
| 55 |
| 56 /// <summary> |
| 57 /// Creates a parameter dictionary by using reflection to iterate over a
ll properties with |
| 58 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu
te. |
| 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> |
| 65 public static IDictionary<string, object> CreateParameterDictionary(obje
ct request) |
| 66 { |
| 67 var dict = new Dictionary<string, object>(); |
| 68 IterateParameters(request, (type, name, value) => |
| 69 { |
| 70 dict.Add(name, value); |
| 71 }); |
| 72 return dict; |
| 73 } |
| 74 |
| 75 /// <summary> |
| 76 /// Sets query parameters in the given builder with all all properties w
ith the |
| 77 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu
te. |
| 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> |
| 85 public static void InitParameters(RequestBuilder builder, object request
) |
| 86 { |
| 87 IterateParameters(request, (type, name, value) => |
| 88 { |
| 89 builder.AddParameter(type, name, value.ToString()); |
| 90 }); |
| 91 } |
| 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> |
| 99 private static void IterateParameters(object request, Action<RequestPara
meterType, string, object> action) |
| 100 { |
| 101 // Use reflection to build the parameter dictionary. |
| 102 foreach (PropertyInfo property in request.GetType().GetProperties(Bi
ndingFlags.Instance | |
| 103 BindingFlags.Public)) |
| 104 { |
| 105 // Retrieve the RequestParameterAttribute. |
| 106 RequestParameterAttribute attribute = |
| 107 property.GetCustomAttributes(typeof(RequestParameterAttribut
e), false).FirstOrDefault() as |
| 108 RequestParameterAttribute; |
| 109 if (attribute == null) |
| 110 { |
| 111 continue; |
| 112 } |
| 113 |
| 114 // Get the name of this parameter from the attribute, if it does
n't exist take a lower-case variant of· |
| 115 // property name. |
| 116 string name = attribute.Name ?? property.Name.ToLower(); |
| 117 |
| 118 var propertyType = property.PropertyType; |
| 119 var value = property.GetValue(request, null); |
| 120 |
| 121 // Call action with the type name and value. |
| 122 if (propertyType.IsValueType || value != null) |
| 123 { |
| 124 action(attribute.Type, name, value); |
| 125 } |
| 126 } |
| 127 } |
| 128 } |
| 129 } |
OLD | NEW |