Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(104)

Delta Between Two Patch Sets: Src/GoogleApis/Apis/Requests/Parameters/ParameterUtils.cs

Issue 13412046: Reimplement OAuth2 library - Step 1 (Closed) Base URL: https://google-api-dotnet-client.googlecode.com/hg/
Left Patch Set: minor Created 10 years, 10 months ago
Right Patch Set: minor Created 10 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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 14 matching lines...) Expand all
25 namespace Google.Apis.Requests.Parameters 25 namespace Google.Apis.Requests.Parameters
26 { 26 {
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.
class 2013/09/16 23:47:29 <param> missing
peleyal 2013/09/17 15:53:40 Done.
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>
class 2013/09/16 23:47:29 <param missing>
peleyal 2013/09/17 15:53:40 Done.
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
class 2013/09/16 23:47:29 <param> missing
peleyal 2013/09/17 15:53:40 Done.
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
class 2013/09/16 23:47:29 Iterates over all <seealso cref="RequestParameterA
peleyal 2013/09/17 15:53:40 Done.
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>
class 2013/09/16 23:47:29 Fix plural and add period at end of action param
peleyal 2013/09/17 15:53:40 Done.
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.
class 2013/09/16 23:47:29 Capitalize use
peleyal 2013/09/17 15:53:40 Done.
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.
class 2013/09/16 23:47:29 Capitalize Retrieve and add . at end of comment
peleyal 2013/09/17 15:53:40 Done.
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·
class 2013/09/16 23:47:29 Add period and capitol to comment
peleyal 2013/09/17 15:53:40 Done.
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.
class 2013/09/16 23:47:29 Add period / cap to comment
peleyal 2013/09/17 15:53:40 Done.
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 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b