Skip to content

Commit 536129e

Browse files
author
michael.hallett
committed
added method to rest request for AddFileBytes; closes PR #463
1 parent 444f83f commit 536129e

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

RestSharp.IntegrationTests/AuthenticationTests.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ public class AuthenticationTests
1616
public void Can_Authenticate_With_Basic_Http_Auth()
1717
{
1818
Uri baseUrl = new Uri("http://localhost:8888/");
19+
1920
using(SimpleServer.Create(baseUrl.AbsoluteUri, UsernamePasswordEchoHandler))
2021
{
2122
var client = new RestClient(baseUrl)
22-
{
23-
Authenticator = new HttpBasicAuthenticator("testuser", "testpassword")
24-
};
23+
{
24+
Authenticator = new HttpBasicAuthenticator("testuser", "testpassword")
25+
};
2526
var request = new RestRequest("test");
2627
var response = client.Execute(request);
2728

@@ -78,6 +79,7 @@ public void Can_Authenticate_With_OAuth()
7879
qs = HttpUtility.ParseQueryString(response.Content);
7980
oauthToken = qs["oauth_token"];
8081
oauthTokenSecret = qs["oauth_token_secret"];
82+
8183
Assert.NotNull(oauthToken);
8284
Assert.NotNull(oauthTokenSecret);
8385

@@ -96,7 +98,9 @@ public void Can_Authenticate_With_OAuth()
9698
//{
9799
// var baseUrl = "http://term.ie/oauth/example";
98100
// var client = new RestClient(baseUrl);
101+
99102
// client.Authenticator = new OAuthAuthenticator(baseUrl, "key", "secret");
103+
100104
// var request = new RestRequest("request_token.php");
101105
// var response = client.Execute(request);
102106

@@ -109,7 +113,9 @@ public void Can_Authenticate_With_OAuth()
109113
//{
110114
// var baseUrl = "http://term.ie/oauth/example";
111115
// var client = new RestClient(baseUrl);
116+
112117
// client.Authenticator = new OAuthAuthenticator(baseUrl, "key", "secret", "requestkey", "requestsecret");
118+
113119
// var request = new RestRequest("access_token.php");
114120
// var response = client.Execute(request);
115121

@@ -123,10 +129,14 @@ public void Can_Authenticate_With_OAuth()
123129
//{
124130
// var baseUrl = "http://term.ie/oauth/example";
125131
// var client = new RestClient(baseUrl);
132+
126133
// client.Authenticator = new OAuthAuthenticator(baseUrl, "key", "secret", "accesskey", "accesssecret");
134+
127135
// var request = new RestRequest("echo_api.php");
136+
128137
// request.AddParameter("foo", "bar");
129138
// request.AddParameter("fizz", "pop");
139+
130140
// var response = client.Execute(request);
131141

132142
// Assert.NotNull(response);
@@ -138,7 +148,9 @@ public void Can_Authenticate_With_OAuth()
138148
//{
139149
// var baseUrl = "http://term.ie/oauth/example";
140150
// var client = new RestClient(baseUrl);
151+
141152
// client.Authenticator = new OAuthAuthenticator(baseUrl, "key", "secret", "accesskey", "accesssecret");
153+
142154
// var request = new RestRequest("echo_api.php");
143155
// var response = client.Execute(request);
144156

RestSharp/IRestRequest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ public interface IRestRequest
159159
/// <param name="contentType">The MIME type of the file to upload</param>
160160
/// <returns>This request</returns>
161161
IRestRequest AddFile(string name, Action<Stream> writer, string fileName, string contentType = null);
162+
163+
/// <summary>
164+
/// Add bytes to the Files collection as if it was a file of specific type
165+
/// </summary>
166+
/// <param name="name">A form parameter name</param>
167+
/// <param name="bytes">The file data</param>
168+
/// <param name="filename">The file name to use for the uploaded file</param>
169+
/// <param name="contentType">Specific content type. Es: application/x-gzip </param>
170+
/// <returns></returns>
171+
IRestRequest AddFileBytes(string name, byte[] bytes, string filename, string contentType = "application/x-gzip");
162172
#endif
163173

164174
/// <summary>

RestSharp/RestClient.Async.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
using System.Threading.Tasks;
2121
#endif
2222
using System.Net;
23-
using RestSharp.Extensions;
2423

2524
namespace RestSharp
2625
{
@@ -83,6 +82,7 @@ private RestRequestAsyncHandle ExecuteAsync(IRestRequest request,
8382
Func<IHttp, Action<HttpResponse>, string, HttpWebRequest> getWebRequest)
8483
{
8584
var http = HttpFactory.Create();
85+
8686
AuthenticateIfNeeded(this, request);
8787

8888
ConfigureHttp(request, http);

RestSharp/RestRequest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,34 @@ private IRestRequest AddFile(FileParameter file)
181181
return this;
182182
}
183183

184+
/// <summary>
185+
/// Add bytes to the Files collection as if it was a file of specific type
186+
/// </summary>
187+
/// <param name="name">A form parameter name</param>
188+
/// <param name="bytes">The file data</param>
189+
/// <param name="filename">The file name to use for the uploaded file</param>
190+
/// <param name="contentType">Specific content type. Es: application/x-gzip </param>
191+
/// <returns></returns>
192+
public IRestRequest AddFileBytes(string name, byte[] bytes, string filename, string contentType = "application/x-gzip")
193+
{
194+
long length = bytes.Length;
195+
196+
return AddFile(new FileParameter
197+
{
198+
Name = name,
199+
FileName = filename,
200+
ContentLength = length,
201+
ContentType = contentType,
202+
Writer = s =>
203+
{
204+
using (var file = new StreamReader(new MemoryStream(bytes)))
205+
{
206+
file.BaseStream.CopyTo(s);
207+
}
208+
}
209+
});
210+
}
211+
184212
/// <summary>
185213
/// Serializes obj to format specified by RequestFormat, but passes xmlNamespace if using the default XmlSerializer
186214
/// The default format is XML. Change RequestFormat if you wish to use a different serialization format.

0 commit comments

Comments
 (0)