Skip to content

Commit

Permalink
add sample.
Browse files Browse the repository at this point in the history
add method listeners
  • Loading branch information
Chris Martin committed Nov 20, 2012
1 parent e0f2d78 commit 81854fa
Show file tree
Hide file tree
Showing 26 changed files with 304 additions and 60 deletions.
12 changes: 12 additions & 0 deletions infusionsoft-net.sln
Expand Up @@ -18,6 +18,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{571B1E
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample", "src\Sample\Sample.csproj", "{A2E1413A-5DE1-4C60-861F-A3B26FF596A7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -48,6 +50,16 @@ Global
{E25F14C1-1A03-4CA1-BA77-B8ADCFF02EBF}.Release|Mixed Platforms.Build.0 = Release|x86
{E25F14C1-1A03-4CA1-BA77-B8ADCFF02EBF}.Release|x86.ActiveCfg = Release|x86
{E25F14C1-1A03-4CA1-BA77-B8ADCFF02EBF}.Release|x86.Build.0 = Release|x86
{A2E1413A-5DE1-4C60-861F-A3B26FF596A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A2E1413A-5DE1-4C60-861F-A3B26FF596A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A2E1413A-5DE1-4C60-861F-A3B26FF596A7}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{A2E1413A-5DE1-4C60-861F-A3B26FF596A7}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{A2E1413A-5DE1-4C60-861F-A3B26FF596A7}.Debug|x86.ActiveCfg = Debug|Any CPU
{A2E1413A-5DE1-4C60-861F-A3B26FF596A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A2E1413A-5DE1-4C60-861F-A3B26FF596A7}.Release|Any CPU.Build.0 = Release|Any CPU
{A2E1413A-5DE1-4C60-861F-A3B26FF596A7}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{A2E1413A-5DE1-4C60-861F-A3B26FF596A7}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{A2E1413A-5DE1-4C60-861F-A3B26FF596A7}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 3 additions & 0 deletions src/Generator/ServiceWrapperGenerator.cs
Expand Up @@ -97,8 +97,11 @@ private void AddConstructor(CodeTypeDeclaration target)

// Add parameters.
constructor.Parameters.Add(new CodeParameterDeclarationExpression(typeof(IInfusionSoftConfiguration), "configuration"));
constructor.Parameters.Add(new CodeParameterDeclarationExpression(typeof (IMethodListenerProvider),
"listenerProvider"));

constructor.BaseConstructorArgs.Add(new CodeArgumentReferenceExpression("configuration"));
constructor.BaseConstructorArgs.Add(new CodeArgumentReferenceExpression("listenerProvider"));

target.Members.Add(constructor);
}
Expand Down
12 changes: 6 additions & 6 deletions src/InfusionSoft/Extensions/DataServiceExtensions.cs
Expand Up @@ -78,8 +78,9 @@ public static IEnumerable<T> Query<T>(this IDataService service, DataPage page)
XmlRpcStruct data = query.Dictionary.AsXmlRpcStruct();
string[] selectedFields = projection.Build();

IInfusionSoftConfiguration configuration = service.Configuration;
var wrapper = new CustomDataServiceWrapper(configuration);
var configuration = service.Configuration;
var methodListenerProvider = service.MethodListenerProvider;
var wrapper = new CustomDataServiceWrapper(configuration, methodListenerProvider);

return wrapper.Invoke<IEnumerable<object>, T[]>(d => d.Query(configuration.ApiKey,
typeof (T).Name, page.Size,
Expand Down Expand Up @@ -118,7 +119,7 @@ public static IEnumerable<T> FindByField<T>(this IDataService service, Action<IF
var projection = new Projection<T>();
fieldSelection(projection);

var wrapper = new CustomDataServiceWrapper(service.Configuration);
var wrapper = new CustomDataServiceWrapper(service.Configuration, service.MethodListenerProvider);

return wrapper.Invoke<IEnumerable<object>, T[]>(d => d.FindByField(service.Configuration.ApiKey,
typeof (T).Name, page.Size,
Expand Down Expand Up @@ -153,10 +154,9 @@ public static T Load<T>(this IDataService service, int id, Action<IProjection<T>
var fieldSelection = new Projection<T>();
projection(fieldSelection);

IInfusionSoftConfiguration configuration = service.Configuration;
var wrapper = new CustomDataServiceWrapper(configuration);
var wrapper = new CustomDataServiceWrapper(service.Configuration, service.MethodListenerProvider);
return
wrapper.Invoke<object, T>(d => d.Load(configuration.ApiKey, typeof (T).Name, id, fieldSelection.Build()));
wrapper.Invoke<object, T>(d => d.Load(service.Configuration.ApiKey, typeof (T).Name, id, fieldSelection.Build()));
}

public static int Update<T>(this IDataService service, int id, Action<IFieldSetter<T>> setter) where T : ITable
Expand Down
1 change: 1 addition & 0 deletions src/InfusionSoft/IInfusionSoftClient.cs
Expand Up @@ -28,5 +28,6 @@ public interface IInfusionSoftClient
ISearchService SearchService { get; }
IShippingService ShippingService { get; }
IWebFormService WebFormService { get; }
IMethodListener MethodListener { get; set; }
}
}
48 changes: 48 additions & 0 deletions src/InfusionSoft/IMethodListener.cs
@@ -0,0 +1,48 @@
#region License
// Copyright (c) 2012, EventDay
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion

using System.Diagnostics;

namespace InfusionSoft
{
public interface IMethodListener
{
void OnRequest(string contents);
void OnResponse(string contents);
}

public interface IMethodListenerProvider
{
IMethodListener GetListener();
}

public class DebugMethodListener : IMethodListener
{
public void OnRequest(string contents)
{
Debug.WriteLine(contents);
}

public void OnResponse(string contents)
{
Debug.WriteLine(contents);
}
}

public class NullMethodListener : IMethodListener
{
public void OnRequest(string contents)
{}

public void OnResponse(string contents)
{}
}
}
1 change: 1 addition & 0 deletions src/InfusionSoft/IService.cs
Expand Up @@ -16,5 +16,6 @@ namespace InfusionSoft
public interface IService
{
IInfusionSoftConfiguration Configuration { get; }
IMethodListenerProvider MethodListenerProvider { get; }
}
}
1 change: 1 addition & 0 deletions src/InfusionSoft/InfusionSoft.csproj
Expand Up @@ -59,6 +59,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Customer.cs" />
<Compile Include="IMethodListener.cs" />
<Compile Include="Support\AddEmailTemplateMergeContext.cs" />
<Compile Include="Support\BasicFieldSetter.cs" />
<Compile Include="Support\ContainsValue.cs" />
Expand Down
47 changes: 33 additions & 14 deletions src/InfusionSoft/InfusionSoftClient.cs
Expand Up @@ -15,27 +15,30 @@

namespace InfusionSoft
{
internal class InfusionSoftClient : IInfusionSoftClient
internal class InfusionSoftClient : IInfusionSoftClient, IMethodListenerProvider
{
private IMethodListener _methodListener;

public InfusionSoftClient(IInfusionSoftConfiguration configuration)
{
ServicePointManager.ServerCertificateValidationCallback +=
(sender, certificate, chain, sslpolicyerrors) => true;

ApplicationName = configuration.ApplicationName;

AffiliateService = new AffiliateServiceWrapper(configuration);
ContactService = new ContactServiceWrapper(configuration);
DataService = new CustomDataServiceWrapper(configuration);
DiscountService = new DiscountServiceWrapper(configuration);
EmailService = new EmailServiceWrapper(configuration);
InvoiceService = new InvoiceServiceWrapper(configuration);
FileService = new FileServiceWrapper(configuration);
OrderService = new OrderServiceWrapper(configuration);
ProductService = new ProductServiceWrapper(configuration);
SearchService = new SearchServiceWrapper(configuration);
ShippingService = new ShippingServiceWrapper(configuration);
WebFormService = new WebFormServiceWrapper(configuration);
MethodListener = new NullMethodListener();

AffiliateService = new AffiliateServiceWrapper(configuration, this);
ContactService = new ContactServiceWrapper(configuration, this);
DataService = new CustomDataServiceWrapper(configuration, this);
DiscountService = new DiscountServiceWrapper(configuration, this);
EmailService = new EmailServiceWrapper(configuration, this);
InvoiceService = new InvoiceServiceWrapper(configuration, this);
FileService = new FileServiceWrapper(configuration, this);
OrderService = new OrderServiceWrapper(configuration, this);
ProductService = new ProductServiceWrapper(configuration, this);
SearchService = new SearchServiceWrapper(configuration, this);
ShippingService = new ShippingServiceWrapper(configuration, this);
WebFormService = new WebFormServiceWrapper(configuration, this);
}

public InfusionSoftClient(string application, string apiKey)
Expand Down Expand Up @@ -71,6 +74,22 @@ public InfusionSoftClient(string application, string apiKey)

public IWebFormService WebFormService { get; private set; }

public IMethodListener MethodListener
{
get { return _methodListener; }
set
{
if(value == null)
_methodListener = new NullMethodListener();
_methodListener = value;
}
}

#endregion

public IMethodListener GetListener()
{
return MethodListener;
}
}
}
34 changes: 32 additions & 2 deletions src/InfusionSoft/Support/InfusionsoftProxy.cs
Expand Up @@ -12,6 +12,7 @@
#endregion

using System;
using System.IO;
using CookComputing.XmlRpc;
using InfusionSoft.Definition;

Expand All @@ -22,11 +23,13 @@ internal class InfusionsoftProxy<TService> where TService : IServiceDefinition
private const string UriMask = "https://{0}.infusionsoft.com/api/xmlrpc";
private const string UserAgent = "InfusionSoft .NET SDK";
private readonly IInfusionSoftConfiguration _configuration;
private readonly IMethodListenerProvider _listenerProvider;
private readonly Uri _uri;

public InfusionsoftProxy(IInfusionSoftConfiguration configuration)
public InfusionsoftProxy(IInfusionSoftConfiguration configuration, IMethodListenerProvider listenerProvider)
{
_configuration = configuration;
_listenerProvider = listenerProvider;
_uri = new Uri(string.Format(UriMask, _configuration.ApplicationName));
}

Expand Down Expand Up @@ -65,7 +68,7 @@ private ProxyManager<TService> CreateProxyManager(XmlRpcResponseEventHandler res
var proxy = (IXmlRpcProxy) definition;
// ReSharper restore PossibleInvalidCastException

proxy.AttachLogger(new XmlRpcDebugLogger());
proxy.AttachLogger(new MethodListenerXmlRpcLogger(_listenerProvider.GetListener()));

proxy.Url = _uri.AbsoluteUri;
proxy.UserAgent = UserAgent;
Expand Down Expand Up @@ -106,5 +109,32 @@ public void Dispose()
}

#endregion

class MethodListenerXmlRpcLogger : XmlRpcLogger
{
private readonly IMethodListener _listener;

public MethodListenerXmlRpcLogger(IMethodListener listener)
{
_listener = listener;
}

protected override void OnResponse(object sender, XmlRpcResponseEventArgs e)
{
_listener.OnResponse(DumpStream(e.ResponseStream));
}

protected override void OnRequest(object sender, XmlRpcRequestEventArgs e)
{
_listener.OnRequest(DumpStream(e.RequestStream));
}

private string DumpStream(Stream stm)
{
var s = new StreamReader(stm).ReadToEnd();
stm.Position = 0L;
return s;
}
}
}
}
7 changes: 5 additions & 2 deletions src/InfusionSoft/Support/ServiceBase.cs
Expand Up @@ -21,10 +21,11 @@ internal abstract class ServiceBase<TServiceDefinition> where TServiceDefinition
{
private readonly InfusionsoftProxy<TServiceDefinition> _proxy;

protected ServiceBase(IInfusionSoftConfiguration configuration)
protected ServiceBase(IInfusionSoftConfiguration configuration, IMethodListenerProvider listenerProvider)
{
MethodListenerProvider = listenerProvider;
Configuration = configuration;
_proxy = new InfusionsoftProxy<TServiceDefinition>(configuration);
_proxy = new InfusionsoftProxy<TServiceDefinition>(configuration, listenerProvider);
ApiKey = configuration.ApiKey;
}

Expand Down Expand Up @@ -61,5 +62,7 @@ private static T TryInvoke<T>(Func<T> func)
throw new InfusionSoftException(e.Message);
}
}

public IMethodListenerProvider MethodListenerProvider { get; private set; }
}
}
4 changes: 2 additions & 2 deletions src/InfusionSoft/Wrappers/AffiliateServiceWrapper.cs
Expand Up @@ -16,8 +16,8 @@ namespace InfusionSoft
internal partial class AffiliateServiceWrapper : ServiceBase<IAffiliateServiceDefinition>, IAffiliateService
{

public AffiliateServiceWrapper(InfusionSoft.IInfusionSoftConfiguration configuration) :
base(configuration)
public AffiliateServiceWrapper(InfusionSoft.IInfusionSoftConfiguration configuration, IMethodListenerProvider listenerProvider) :
base(configuration, listenerProvider)
{
}

Expand Down
6 changes: 3 additions & 3 deletions src/InfusionSoft/Wrappers/ContactServiceWrapper.cs
Expand Up @@ -15,9 +15,9 @@ namespace InfusionSoft

internal partial class ContactServiceWrapper : ServiceBase<IContactServiceDefinition>, IContactService
{
public ContactServiceWrapper(InfusionSoft.IInfusionSoftConfiguration configuration) :
base(configuration)

public ContactServiceWrapper(InfusionSoft.IInfusionSoftConfiguration configuration, IMethodListenerProvider listenerProvider) :
base(configuration, listenerProvider)
{
}

Expand Down
3 changes: 2 additions & 1 deletion src/InfusionSoft/Wrappers/CustomDataServiceWrapper.cs
Expand Up @@ -17,7 +17,8 @@ internal class CustomDataServiceWrapper : DataServiceWrapper
{
private readonly PasswordHasher _hasher;

public CustomDataServiceWrapper(IInfusionSoftConfiguration configuration) : base(configuration)
public CustomDataServiceWrapper(IInfusionSoftConfiguration configuration, IMethodListenerProvider listenerProvider) :
base(configuration, listenerProvider)
{
_hasher = new PasswordHasher();
}
Expand Down
6 changes: 3 additions & 3 deletions src/InfusionSoft/Wrappers/DataServiceWrapper.cs
Expand Up @@ -15,9 +15,9 @@ namespace InfusionSoft

internal partial class DataServiceWrapper : ServiceBase<IDataServiceDefinition>, IDataService
{
public DataServiceWrapper(InfusionSoft.IInfusionSoftConfiguration configuration) :
base(configuration)

public DataServiceWrapper(InfusionSoft.IInfusionSoftConfiguration configuration, IMethodListenerProvider listenerProvider) :
base(configuration, listenerProvider)
{
}

Expand Down
6 changes: 3 additions & 3 deletions src/InfusionSoft/Wrappers/DiscountServiceWrapper.cs
Expand Up @@ -15,9 +15,9 @@ namespace InfusionSoft

internal partial class DiscountServiceWrapper : ServiceBase<IDiscountServiceDefinition>, IDiscountService
{
public DiscountServiceWrapper(InfusionSoft.IInfusionSoftConfiguration configuration) :
base(configuration)

public DiscountServiceWrapper(InfusionSoft.IInfusionSoftConfiguration configuration, IMethodListenerProvider listenerProvider) :
base(configuration, listenerProvider)
{
}

Expand Down
6 changes: 3 additions & 3 deletions src/InfusionSoft/Wrappers/EmailServiceWrapper.cs
Expand Up @@ -15,9 +15,9 @@ namespace InfusionSoft

internal partial class EmailServiceWrapper : ServiceBase<IEmailServiceDefinition>, IEmailService
{
public EmailServiceWrapper(InfusionSoft.IInfusionSoftConfiguration configuration) :
base(configuration)

public EmailServiceWrapper(InfusionSoft.IInfusionSoftConfiguration configuration, IMethodListenerProvider listenerProvider) :
base(configuration, listenerProvider)
{
}

Expand Down
6 changes: 3 additions & 3 deletions src/InfusionSoft/Wrappers/FileServiceWrapper.cs
Expand Up @@ -15,9 +15,9 @@ namespace InfusionSoft

internal partial class FileServiceWrapper : ServiceBase<IFileServiceDefinition>, IFileService
{
public FileServiceWrapper(InfusionSoft.IInfusionSoftConfiguration configuration) :
base(configuration)

public FileServiceWrapper(InfusionSoft.IInfusionSoftConfiguration configuration, IMethodListenerProvider listenerProvider) :
base(configuration, listenerProvider)
{
}

Expand Down

0 comments on commit 81854fa

Please sign in to comment.