Skip to content

Commit

Permalink
Add SetCookie to IServiceClient
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jul 6, 2015
1 parent 35f2f0c commit d4b4f57
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 2 deletions.
34 changes: 33 additions & 1 deletion src/ServiceStack.Client/ServiceClientBase.cs
Expand Up @@ -32,7 +32,7 @@ namespace ServiceStack
* Need to provide async request options
* http://msdn.microsoft.com/en-us/library/86wf6409(VS.71).aspx
*/
public abstract class ServiceClientBase : IServiceClient, IMessageProducer
public abstract class ServiceClientBase : IServiceClient, IMessageProducer, IHasCookieContainer
{
private static readonly ILog log = LogManager.GetLogger(typeof(ServiceClientBase));

Expand Down Expand Up @@ -1127,6 +1127,12 @@ public void ClearCookies()
return CookieContainer.ToDictionary(BaseUri);
}

public void SetCookie(string name, string value, TimeSpan? expiresIn = null)
{
this.SetCookie(name, value, new Uri(BaseUri).Host,
expiresIn != null ? DateTime.UtcNow.Add(expiresIn.Value) : (DateTime?)null);
}

public virtual void Get(IReturnVoid requestDto)
{
Get<byte[]>(requestDto.ToUrl(HttpMethods.Get, Format));
Expand Down Expand Up @@ -1622,6 +1628,32 @@ public static HttpWebResponse Head(this IRestClient client, string relativeOrAbs
throw new NotSupportedException();
return c.Head(relativeOrAbsoluteUrl);
}

public static void SetCookie(this IServiceClient client, string name, string value, string domain,
DateTime? expiresAt = null, string path = null,
bool? httpOnly = null, bool? secure = null)
{
var hasCookies = client as IHasCookieContainer;
if (hasCookies == null)
throw new NotSupportedException("Client does not implement IHasCookieContainer");

var cookie = new Cookie(name, value, path ?? "/", domain);
if (expiresAt != null)
cookie.Expires = expiresAt.Value;
if (path != null)
cookie.Path = path;
if (httpOnly != null)
cookie.HttpOnly = httpOnly.Value;
if (secure != null)
cookie.Secure = secure.Value;

hasCookies.CookieContainer.Add(cookie);
}
}

public interface IHasCookieContainer
{
CookieContainer CookieContainer { get; }
}

public delegate object ResultsFilterDelegate(Type responseType, string httpMethod, string requestUri, object request);
Expand Down
5 changes: 5 additions & 0 deletions src/ServiceStack.Client/WcfServiceClient.cs
Expand Up @@ -358,6 +358,11 @@ public void ClearCookies()
return new Dictionary<string, string>();
}

public void SetCookie(string name, string value, TimeSpan? expiresIn = null)
{
throw new NotImplementedException();
}

public void Get(IReturnVoid request)
{
throw new NotImplementedException();
Expand Down
8 changes: 7 additions & 1 deletion src/ServiceStack.HttpClient/JsonHttpClient.cs
Expand Up @@ -19,7 +19,7 @@

namespace ServiceStack
{
public class JsonHttpClient : IServiceClient, IJsonServiceClient
public class JsonHttpClient : IServiceClient, IJsonServiceClient, IHasCookieContainer
{
public static ILog log = LogManager.GetLogger(typeof(JsonHttpClient));

Expand Down Expand Up @@ -612,6 +612,12 @@ public void ClearCookies()
return CookieContainer.ToDictionary(BaseUri);
}

public void SetCookie(string name, string value, TimeSpan? expiresIn = null)
{
this.SetCookie(name, value, new Uri(BaseUri).Host,
expiresIn != null ? DateTime.UtcNow.Add(expiresIn.Value) : (DateTime?)null);
}

public void Get(IReturnVoid request)
{
WaitSyncResponse(GetAsync(request));
Expand Down
2 changes: 2 additions & 0 deletions src/ServiceStack.Interfaces/IRestClient.cs
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;

Expand All @@ -7,6 +8,7 @@ public interface IRestClient
{
void ClearCookies();
Dictionary<string, string> GetCookieValues();
void SetCookie(string name, string value, TimeSpan? expiresIn = null);

void Get(IReturnVoid request);
TResponse Get<TResponse>(IReturn<TResponse> requestDto);
Expand Down
5 changes: 5 additions & 0 deletions tests/ServiceStack.Common.Tests/TestBase.cs
Expand Up @@ -185,6 +185,11 @@ public void ClearCookies()
return new Dictionary<string, string>();
}

public void SetCookie(string name, string value, TimeSpan? expiresIn = null)
{
throw new NotImplementedException();
}

public void Get(IReturnVoid request)
{
throw new NotImplementedException();
Expand Down
Expand Up @@ -171,6 +171,11 @@ public void ClearCookies()
return new Dictionary<string, string>();
}

public void SetCookie(string name, string value, TimeSpan? expiresIn = null)
{
throw new NotImplementedException();
}

public void Get(IReturnVoid request)
{
throw new NotImplementedException();
Expand Down

0 comments on commit d4b4f57

Please sign in to comment.