Skip to content

Commit

Permalink
Made the needed code changes for releasing a stable version of the CS…
Browse files Browse the repository at this point in the history
…harp client
  • Loading branch information
faniereynders committed Jan 22, 2015
1 parent 09df4f5 commit fb9475e
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 462 deletions.
2 changes: 1 addition & 1 deletion WebApiProxy.Core/ConnectionException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override string Message
{
get
{
return "ProxyApi: Configuration file not found. Please make sure 'ProxyApiConfig.json' exists within your project root.";
return "WebApiProxy: Configuration file not found. Please make sure 'WebApiProxy.config' exists within your project root.";
}
}
}
Expand Down
103 changes: 17 additions & 86 deletions WebApiProxy.Core/Models/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
using System.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace WebApiProxy.Core.Models
{
/// <summary>
/// Represents the available configurations to a proxy.
/// </summary>
[XmlRoot("proxy")]
public class Configuration
{
#region Constants
/// <summary>
/// The proxy configuration file.
/// </summary>
public const string FileName = "WebApiProxy.config";
#endregion
public const string CacheFile = "WebApiProxySource.cache";

#region Fields
private string _clientSuffix;
private string _namespace;
#endregion

#region Properties
/// <summary>
/// Gets or sets the client suffix.
/// </summary>
/// <value>
/// The client suffix.
/// </value>

[XmlAttribute("clientSuffix")]
public string ClientSuffix
{
Expand All @@ -42,12 +30,8 @@ public string ClientSuffix
}
}

/// <summary>
/// Gets or sets the namespace.
/// </summary>
/// <value>
/// The namespace.
/// </value>
private string _namespace;

[XmlAttribute("namespace")]
public string Namespace
{
Expand All @@ -61,73 +45,15 @@ public string Namespace
}
}

/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
[XmlAttribute("name")]
public string Name { get; set; }

/// <summary>
/// Gets or sets the endpoint.
/// </summary>
/// <value>
/// The endpoint.
/// </value>
[XmlAttribute("endpoint")]
public string Endpoint { get; set; }

/// <summary>
/// Gets or sets the metadata.
/// </summary>
/// <value>
/// The metadata.
/// </value>
[XmlIgnore]
public Metadata Metadata { get; set; }

/// <summary>
/// Gets or sets the remove from URL.
/// </summary>
/// <value>
/// The remove from URL.
/// </value>
[XmlAttribute("removeFromUrl")]
public string RemoveFromUrl { get; set; }

/// <summary>
/// Indicates if task must generate models.
/// </summary>
[XmlAttribute("generateModels")]
public bool GenerateModels { get; set; }

/// <summary>
/// A comma-separated list of namespaces to include
/// </summary>
[XmlAttribute("namespacesToInclude")]
public string NamespacesToInclude { get; set; }

/// <summary>
/// Gets or sets the clients namespace suffix.
/// Default is ".Clients".
/// </summary>
[XmlAttribute("clientsNamespaceSuffix")]
public string ClientsNamespaceSuffix { get; set; }

/// <summary>
/// Gets or sets the full path of the generated file.
/// </summary>
[XmlAttribute("filePath")]
public string FilePath { get; set; }
#endregion

#region Methods
/// <summary>
/// Loads the configuration from file.
/// </summary>
/// <returns>The configuration loaded.</returns>
public static Configuration Load()
{
if (!File.Exists(Configuration.FileName))
Expand All @@ -136,12 +62,17 @@ public static Configuration Load()
var xml = File.ReadAllText(Configuration.FileName);
var serializer = new XmlSerializer(typeof(Configuration), new XmlRootAttribute("proxy"));



var reader = new StreamReader(Configuration.FileName);
var config = (Configuration)serializer.Deserialize(reader);
reader.Close();


return config;

}
#endregion

}
}

}
2 changes: 1 addition & 1 deletion WebApiProxy.Tasks.FunctionalTests/App.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
Expand Down
63 changes: 43 additions & 20 deletions WebApiProxy.Tasks/ProxyGenerationTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public class ProxyGenerationTask : ITask
{
private Configuration config;

private const string DefaultFilePath = "WebApiProxySource.cs";

[Output]
public string Filename { get; set; }

Expand All @@ -23,36 +21,38 @@ public class ProxyGenerationTask : ITask

public bool Execute()
{
config = Configuration.Load();
config.FilePath = config.FilePath ?? DefaultFilePath;
var fileDirectory = new FileInfo(config.FilePath).Directory.FullName;
if (!Directory.Exists(fileDirectory))
{
Directory.CreateDirectory(fileDirectory);
}
var cacheFilePath = config.FilePath + ".cache";

string source;

try
{

config = Configuration.Load();

config.Metadata = GetProxy();

var template = new CSharpProxyTemplate(config);
source = template.TransformText();

File.WriteAllText(cacheFilePath, source);
var source = template.TransformText();

File.WriteAllText(Filename, source);
File.WriteAllText(Configuration.CacheFile, source);
}
catch (InvalidOperationException)
catch (ConnectionException)
{
source = File.ReadAllText(cacheFilePath);
tryReadFromCache();
// throw ex;
}
catch (Exception ex)
{
throw ex;
}

File.WriteAllText(config.FilePath, source);

this.Filename = config.FilePath;

return true;
}



private Metadata GetProxy()
{
var url = string.Empty;
Expand All @@ -61,21 +61,44 @@ private Metadata GetProxy()
{
using (var client = new HttpClient())
{

client.DefaultRequestHeaders.Add("X-Proxy-Type", "metadata");

var response = Task.Run(() => client.GetAsync(config.Endpoint)).Result;
var response = client.GetAsync(config.Endpoint).Result;

response.EnsureSuccessStatusCode();

var metadata = response.Content.ReadAsAsync<Metadata>().Result;



return metadata;
}
}
catch (Exception ex)
{
throw new InvalidOperationException(config.Endpoint, ex);

throw new ConnectionException(config.Endpoint);
}



}

private void tryReadFromCache()
{

if (!File.Exists(Configuration.CacheFile))
{
throw new ConnectionException(config.Endpoint);
}
var source = File.ReadAllText(Configuration.CacheFile);
File.WriteAllText(Filename, source);


}


}
}

2 changes: 0 additions & 2 deletions WebApiProxy.Tasks/Templates/CSharpProxyTemplate.Parameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public CSharpProxyTemplate(Configuration config)

this.Configuration = config;
}

public bool renderNamespaces { get; set; }
public Configuration Configuration { get; set; }

}
Expand Down
Loading

0 comments on commit fb9475e

Please sign in to comment.