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

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

Issue 13412046: Reimplement OAuth2 library - Step 1 (Closed) Base URL: https://google-api-dotnet-client.googlecode.com/hg/
Left Patch Set: self review Created 10 years, 6 months ago
Right Patch Set: minor Created 10 years, 6 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 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
(...skipping 11 matching lines...) Expand all
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 for building a URI using <see cref="BuildUri"/> o r a HTTP request using· 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 /// <see cref="CreateRequest"/> from the query and path parameters of a REST call.</summary>
30 public class RequestBuilder 30 public class RequestBuilder
31 { 31 {
32 /// <summary> Supported HTTP methods. </summary> 32 /// <summary>Supported HTTP methods.</summary>
33 private static IEnumerable<string> SupportedMethods = new List<string>· 33 private static IEnumerable<string> SupportedMethods = new List<string>·
34 {· 34 {·
35 HttpConsts.Get, HttpConsts.Post, HttpConsts.Put, HttpConsts.Dele te, HttpConsts.Patch· 35 HttpConsts.Get, HttpConsts.Post, HttpConsts.Put, HttpConsts.Dele te, HttpConsts.Patch·
36 }; 36 };
37 37
38 /// <summary> 38 /// <summary>
39 /// 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
40 /// 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·
41 /// 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·
42 /// for the specified key in this dictionary. 42 /// for the specified key in this dictionary.
(...skipping 10 matching lines...) Expand all
53 /// 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 ).
54 /// </summary> 54 /// </summary>
55 public Uri BaseUri { get; set; } 55 public Uri BaseUri { get; set; }
56 56
57 /// <summary> 57 /// <summary>
58 /// 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
59 /// the parameters are substituted from the <see cref="PathParameters"/> dictionary. 59 /// the parameters are substituted from the <see cref="PathParameters"/> dictionary.
60 /// </summary> 60 /// </summary>
61 public string Path { get; set; } 61 public string Path { get; set; }
62 62
63 /// <summary> The HTTP method used for this request. </summary> 63 /// <summary>The HTTP method used for this request.</summary>
64 private string method; 64 private string method;
65 65
66 /// <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>
67 /// <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>
68 public string Method 68 public string Method
69 { 69 {
70 get { return method; } 70 get { return method; }
71 set 71 set
72 { 72 {
73 if (!SupportedMethods.Contains(value)) 73 if (!SupportedMethods.Contains(value))
74 throw new ArgumentOutOfRangeException("Method"); 74 throw new ArgumentOutOfRangeException("Method");
75 method = value; 75 method = value;
76 } 76 }
77 } 77 }
78 78
79 /// <summary> Construct a new request builder. </summary> 79 /// <summary>Construct a new request builder.</summary>
80 public RequestBuilder() 80 public RequestBuilder()
81 { 81 {
82 this.PathParameters = new Dictionary<string, string>(); 82 this.PathParameters = new Dictionary<string, string>();
83 this.QueryParameters = new List<KeyValuePair<string, string>>(); 83 this.QueryParameters = new List<KeyValuePair<string, string>>();
84 this.Method = HttpConsts.Get; 84 this.Method = HttpConsts.Get;
85 } 85 }
86 86
87 /// <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>
88 public Uri BuildUri() 88 public Uri BuildUri()
89 { 89 {
90 var restPath = new StringBuilder(PathParameters 90 var restPath = new StringBuilder(PathParameters
91 .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) })
92 .Aggregate(this.Path, (path, param) => path.Replace(param.Token, param.Value))); 92 .Aggregate(this.Path, (path, param) => path.Replace(param.Token, param.Value)));
93 93
94 if (QueryParameters.Count > 0) 94 if (QueryParameters.Count > 0)
95 { 95 {
96 restPath.Append("?"); 96 restPath.Append("?");
97 // 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"
98 restPath.Append(String.Join("&", QueryParameters.Select( 98 restPath.Append(String.Join("&", QueryParameters.Select(
99 x => string.IsNullOrEmpty(x.Value) ? 99 x => string.IsNullOrEmpty(x.Value) ?
100 Uri.EscapeDataString(x.Key) : 100 Uri.EscapeDataString(x.Key) :
101 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)))
102 .ToArray())); 102 .ToArray()));
103 } 103 }
104 104
105 return new Uri(this.BaseUri, restPath.ToString()); 105 return new Uri(this.BaseUri, restPath.ToString());
106 } 106 }
107 107
108 /// <summary> Adds a parameter value. </summary> 108 /// <summary>Adds a parameter value.</summary>
109 /// <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>
110 /// <param name="name">Parameter name.</param> 110 /// <param name="name">Parameter name.</param>
111 /// <param name="value">Parameter value.</param> 111 /// <param name="value">Parameter value.</param>
112 public void AddParameter(RequestParameterType type, string name, string value) 112 public void AddParameter(RequestParameterType type, string name, string value)
113 { 113 {
114 switch (type) 114 switch (type)
115 { 115 {
116 case RequestParameterType.Path: 116 case RequestParameterType.Path:
117 if (string.IsNullOrEmpty(value)) 117 if (string.IsNullOrEmpty(value))
118 { 118 {
119 throw new ArgumentException("Path parameters cannot be n ull or empty."); 119 throw new ArgumentException("Path parameters cannot be n ull or empty.");
120 } 120 }
121 PathParameters.Add(name, value); 121 PathParameters.Add(name, value);
122 break; 122 break;
123 case RequestParameterType.Query: 123 case RequestParameterType.Query:
124 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)
125 { 125 {
126 break; 126 break;
127 } 127 }
128 QueryParameters.Add(new KeyValuePair<string, string>(name, v alue)); 128 QueryParameters.Add(new KeyValuePair<string, string>(name, v alue));
129 break; 129 break;
130 default: 130 default:
131 throw new ArgumentOutOfRangeException("type"); 131 throw new ArgumentOutOfRangeException("type");
132 } 132 }
133 } 133 }
134 134
135 /// <summary> Creates a new HTTP request message. </summary> 135 /// <summary>Creates a new HTTP request message.</summary>
136 public HttpRequestMessage CreateRequest() 136 public HttpRequestMessage CreateRequest()
137 { 137 {
138 return new HttpRequestMessage(new HttpMethod(Method), BuildUri()); 138 return new HttpRequestMessage(new HttpMethod(Method), BuildUri());
139 } 139 }
140 } 140 }
141 } 141 }
LEFTRIGHT

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