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 |
(...skipping 16 matching lines...) Expand all Loading... |
27 /// <summary> | 27 /// <summary> |
28 /// Utility class for iterating on <seealso cref="RequestParameterAttribute"
/> properties in a request object. | 28 /// Utility class for iterating on <seealso cref="RequestParameterAttribute"
/> properties in a request object. |
29 /// </summary> | 29 /// </summary> |
30 public static class ParameterUtils | 30 public static class ParameterUtils |
31 { | 31 { |
32 /// <summary> | 32 /// <summary> |
33 /// 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· |
34 /// input request. It uses reflection to iterate over all properties wit
h | 34 /// input request. It uses reflection to iterate over all properties wit
h |
35 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu
te. | 35 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu
te. |
36 /// </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> |
37 public static FormUrlEncodedContent CreateFormUrlEncodedContent(object r
equest) | 46 public static FormUrlEncodedContent CreateFormUrlEncodedContent(object r
equest) |
38 { | 47 { |
39 IList<KeyValuePair<string, string>> list = new List<KeyValuePair<str
ing, string>>(); | 48 IList<KeyValuePair<string, string>> list = new List<KeyValuePair<str
ing, string>>(); |
40 IterateParameters(request, (type, name, value) => | 49 IterateParameters(request, (type, name, value) => |
41 { | 50 { |
42 list.Add(new KeyValuePair<string, string>(name, value.ToStri
ng())); | 51 list.Add(new KeyValuePair<string, string>(name, value.ToStri
ng())); |
43 }); | 52 }); |
44 return new FormUrlEncodedContent(list); | 53 return new FormUrlEncodedContent(list); |
45 } | 54 } |
46 | 55 |
47 /// <summary> | 56 /// <summary> |
48 /// Creates a parameter dictionary by using reflection to iterate over a
ll properties with | 57 /// Creates a parameter dictionary by using reflection to iterate over a
ll properties with |
49 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu
te. | 58 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu
te. |
50 /// </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> |
51 public static IDictionary<string, object> CreateParameterDictionary(obje
ct request) | 65 public static IDictionary<string, object> CreateParameterDictionary(obje
ct request) |
52 { | 66 { |
53 var dict = new Dictionary<string, object>(); | 67 var dict = new Dictionary<string, object>(); |
54 IterateParameters(request, (type, name, value) => | 68 IterateParameters(request, (type, name, value) => |
55 { | 69 { |
56 dict.Add(name, value); | 70 dict.Add(name, value); |
57 }); | 71 }); |
58 return dict; | 72 return dict; |
59 } | 73 } |
60 | 74 |
61 /// <summary> | 75 /// <summary> |
62 /// Creates a parameter dictionary by using reflection to iterate over a
ll properties with | 76 /// Sets query parameters in the given builder with all all properties w
ith the |
63 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu
te. | 77 /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribu
te. |
64 /// </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> |
65 public static void InitParameters(RequestBuilder builder, object request
) | 85 public static void InitParameters(RequestBuilder builder, object request
) |
66 { | 86 { |
67 IterateParameters(request, (type, name, value) => | 87 IterateParameters(request, (type, name, value) => |
68 { | 88 { |
69 builder.AddParameter(type, name, value.ToString()); | 89 builder.AddParameter(type, name, value.ToString()); |
70 }); | 90 }); |
71 } | 91 } |
72 | 92 |
73 /// <summary> | 93 /// <summary> |
74 /// Iterates over all <seealso cref="RequestParameterAttribute"/> proper
ties in the request object and invoke | 94 /// Iterates over all <seealso cref="RequestParameterAttribute"/> proper
ties in the request object and invokes |
75 /// the specified action for each of them. | 95 /// the specified action for each of them. |
76 /// </summary> | 96 /// </summary> |
77 /// <param name="request">A request object</param> | 97 /// <param name="request">A request object</param> |
78 /// <param name="action">An action to invoke which gets the parameter ty
pe, name and it value</param> | 98 /// <param name="action">An action to invoke which gets the parameter ty
pe, name and its value</param> |
79 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) |
80 { | 100 { |
81 // use reflection to build the parameter dictionary. | 101 // Use reflection to build the parameter dictionary. |
82 foreach (PropertyInfo property in request.GetType().GetProperties(Bi
ndingFlags.Instance | | 102 foreach (PropertyInfo property in request.GetType().GetProperties(Bi
ndingFlags.Instance | |
83 BindingFlags.Public)) | 103 BindingFlags.Public)) |
84 { | 104 { |
85 // retrieve the RequestParameterAttribute | 105 // Retrieve the RequestParameterAttribute. |
86 RequestParameterAttribute attribute = | 106 RequestParameterAttribute attribute = |
87 property.GetCustomAttributes(typeof(RequestParameterAttribut
e), false).FirstOrDefault() as | 107 property.GetCustomAttributes(typeof(RequestParameterAttribut
e), false).FirstOrDefault() as |
88 RequestParameterAttribute; | 108 RequestParameterAttribute; |
89 if (attribute == null) | 109 if (attribute == null) |
90 { | 110 { |
91 continue; | 111 continue; |
92 } | 112 } |
93 | 113 |
94 // 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· |
95 // property name | 115 // property name. |
96 string name = attribute.Name ?? property.Name.ToLower(); | 116 string name = attribute.Name ?? property.Name.ToLower(); |
97 | 117 |
98 var propertyType = property.PropertyType; | 118 var propertyType = property.PropertyType; |
99 var value = property.GetValue(request, null); | 119 var value = property.GetValue(request, null); |
100 | 120 |
101 // call action with the type name and value | 121 // Call action with the type name and value. |
102 if (propertyType.IsValueType || value != null) | 122 if (propertyType.IsValueType || value != null) |
103 { | 123 { |
104 action(attribute.Type, name, value); | 124 action(attribute.Type, name, value); |
105 } | 125 } |
106 } | 126 } |
107 } | 127 } |
108 } | 128 } |
109 } | 129 } |
LEFT | RIGHT |