Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
WondeTadesse committed Nov 18, 2014
1 parent 49b88cc commit 96b318b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 31 deletions.
2 changes: 1 addition & 1 deletion WebAPIClient/DataStreamingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private static void ValidateDownload(this HttpContent content, string fileFullPa
if (!overWrite && File.Exists(fileFullPath))
throw new InvalidOperationException(string.Format("{0} file is already exists !", fileFullPath));

if (!content.Headers.ContentLength.HasValue && content.Headers.ContentLength.Value > 0)
if (!content.Headers.ContentLength.HasValue && content.Headers.ContentLength.Value <= 0)
throw new InvalidOperationException(string.Format("{0} file stream content is empty !", fileFullPath));
}

Expand Down
12 changes: 5 additions & 7 deletions WebAPIClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static void Main(string[] args)

ExecuteOperation(() => Console.WriteLine("You exited the system successfully !"), 2);
}

#region Main Operations

/// <summary>
Expand All @@ -75,6 +75,7 @@ private static void MainMenu()
{
ConsoleKeyInfo keyInfo;
bool isNewLineRequired = false;
Console.WindowWidth = 90;

do
{
Expand All @@ -84,8 +85,6 @@ private static void MainMenu()
else
isNewLineRequired = true;

Console.WindowWidth = 90;

Console.WriteLine("Select the operation you want to perform !\nNote: Each operation result will be displayed for only 5 seconds.\n");
Console.WriteLine("Select 1. To perform GetFileMetaData.");
Console.WriteLine("Select 2. To perform Download. Default Download files path is C:\\");
Expand Down Expand Up @@ -276,7 +275,7 @@ private static async Task DownloadFile()
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Please specify file name with extension and Press Enter :- ");
string fileName = Console.ReadLine();
string localDownloadPath = string.Concat(@"c:\", fileName); // the path can be configurable
string localDownloadPath = Path.Combine(@"c:\", fileName); // the path can be configurable
bool overWrite = true;
string actionURL = string.Concat("downloadasync?fileName=", fileName);

Expand All @@ -292,7 +291,7 @@ private static async Task DownloadFile()
httpClient.BaseAddress = baseStreamingURL;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, actionURL);

await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).
await httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead).
ContinueWith((response)
=>
{
Expand Down Expand Up @@ -322,8 +321,7 @@ private static async Task DownloadFile()
/// <param name="localDownloadFilePath">Local download file path</param>
/// <param name="overWrite">An indicator to overwrite a file if it exist in the client.</param>
/// <param name="response">Awaitable HttpResponseMessage task value</param>
private static void ProcessDownloadResponse(string localDownloadFilePath, bool overWrite,
Task<HttpResponseMessage> response)
private static void ProcessDownloadResponse(string localDownloadFilePath, bool overWrite, Task<HttpResponseMessage> response)
{
if (response.Result.IsSuccessStatusCode)
{
Expand Down
96 changes: 80 additions & 16 deletions WebAPIDataStreaming/Controllers/StreamFilesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace WebAPIDataStreaming.Controllers
[RequestModelValidator]
public class StreamFilesController : ApiController
{
private string exMessage = "Opps! exception happens";

/// <summary>
/// Get File meta data
/// </summary>
Expand All @@ -39,15 +41,16 @@ public class StreamFilesController : ApiController
[Route("getfilemetadata")]
public HttpResponseMessage GetFileMetaData(string fileName)
{
FileMetaData metaData = new FileMetaData();
metaData.FileResponseMessage.IsExists = false;

try
{
string filePath = string.Concat(this.GetDownloadPath(), "\\", fileName);
string filePath = Path.Combine(this.GetDownloadPath(), "\\", fileName);
FileInfo fileInfo = new FileInfo(filePath);
FileMetaData metaData = new FileMetaData();

if (!fileInfo.Exists)
{
metaData.FileResponseMessage.IsExists = false;
metaData.FileResponseMessage.Content = string.Format("{0} file is not found !", fileName);
return Request.CreateResponse(HttpStatusCode.NotFound, metaData, new MediaTypeHeaderValue("text/json"));
}
Expand All @@ -62,7 +65,18 @@ public HttpResponseMessage GetFileMetaData(string fileName)
}
catch (Exception exception)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception);
// Log exception and return gracefully


if (string.IsNullOrWhiteSpace(exception.Message))
{
metaData.FileResponseMessage.Content = string.Concat("Exception : - StackTrace : ", exception.StackTrace);
}
else
{
metaData.FileResponseMessage.Content = string.Concat("Exception : - Message : ", exception.Message, " ", "StackTrace : ", exception.StackTrace);
}
return Request.CreateResponse(HttpStatusCode.InternalServerError, metaData, new MediaTypeHeaderValue("text/json"));
}

}
Expand All @@ -76,17 +90,18 @@ public HttpResponseMessage GetFileMetaData(string fileName)
[Route("searchfileindownloaddirectory")]
public HttpResponseMessage SearchFileInDownloadDirectory(string fileName)
{
List<FileMetaData> filesMetaData = new List<FileMetaData>();
FileMetaData metaData = new FileMetaData();
try
{
string[] files = Directory.GetFiles(this.GetDownloadPath(), fileName, SearchOption.AllDirectories);

if (files != null && files.Count() > 0)
{
List<FileMetaData> filesMetaData = new List<FileMetaData>();
foreach (string file in files)
{
FileInfo fileInfo = new FileInfo(file);
FileMetaData metaData = new FileMetaData();
metaData = new FileMetaData();
metaData.FileResponseMessage.IsExists = true;
metaData.FileName = fileName;
metaData.FileExtension = fileInfo.Extension;
Expand All @@ -109,7 +124,23 @@ public HttpResponseMessage SearchFileInDownloadDirectory(string fileName)
}
catch (Exception exception)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception);
if (!string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace))
{
metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message, " ", "StackTrace : ", exception.StackTrace);
}
else if (!string.IsNullOrWhiteSpace(exception.Message) && string.IsNullOrWhiteSpace(exception.StackTrace))
{
metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message);
}
else if (string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace))
{
metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - StackTrace : ", exception.StackTrace);
}
else
{
metaData.FileResponseMessage.Content = exMessage;
}
return Request.CreateResponse(HttpStatusCode.InternalServerError, metaData, new MediaTypeHeaderValue("text/json"));
}
}

Expand Down Expand Up @@ -139,11 +170,11 @@ public async Task<HttpResponseMessage> DownloadFileAsync(string fileName)
public HttpResponseMessage DownloadFile(string fileName)
{
HttpResponseMessage response = Request.CreateResponse();
FileMetaData metaData = new FileMetaData();
try
{
string filePath = string.Concat(this.GetDownloadPath(), "\\", fileName);
string filePath = Path.Combine(this.GetDownloadPath(), "\\", fileName);
FileInfo fileInfo = new FileInfo(filePath);
FileMetaData metaData = new FileMetaData();

if (!fileInfo.Exists)
{
Expand All @@ -164,7 +195,24 @@ public HttpResponseMessage DownloadFile(string fileName)
}
catch (Exception exception)
{
response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception);
if (!string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace))
{
metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message, " ", "StackTrace : ", exception.StackTrace);
}
else if (!string.IsNullOrWhiteSpace(exception.Message) && string.IsNullOrWhiteSpace(exception.StackTrace))
{
metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message);
}
else if (string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace))
{
metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - StackTrace : ", exception.StackTrace);
}
else
{
metaData.FileResponseMessage.Content = exMessage;
}
response = Request.CreateResponse(HttpStatusCode.InternalServerError, metaData, new MediaTypeHeaderValue("text/json"));

}
return response;
}
Expand All @@ -181,7 +229,7 @@ public HttpResponseMessage UploadFile(bool overWrite)
{
HttpResponseMessage response = Request.CreateResponse();
List<FileResponseMessage> fileResponseMessages = new List<FileResponseMessage>();
FileResponseMessage fileResponseMessage = new FileResponseMessage { IsExists = false, };
FileResponseMessage fileResponseMessage = new FileResponseMessage { IsExists = false };

try
{
Expand All @@ -200,7 +248,25 @@ public HttpResponseMessage UploadFile(bool overWrite)
}
catch (Exception exception)
{
response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception);
fileResponseMessage = new FileResponseMessage { IsExists = false };
if (!string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace))
{
fileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message, " ", "StackTrace : ", exception.StackTrace);
}
else if (!string.IsNullOrWhiteSpace(exception.Message) && string.IsNullOrWhiteSpace(exception.StackTrace))
{
fileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message);
}
else if (string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace))
{
fileResponseMessage.Content = string.Concat(exMessage, " Exception : - StackTrace : ", exception.StackTrace);
}
else
{
fileResponseMessage.Content = exMessage;
}
fileResponseMessages.Add(fileResponseMessage);
response = Request.CreateResponse(HttpStatusCode.InternalServerError, fileResponseMessages, new MediaTypeHeaderValue("text/json"));
}
return response;
}
Expand Down Expand Up @@ -251,7 +317,7 @@ private HttpResponseMessage ProcessUploadRequest(bool overWrite)

foreach (string file in files)
{
string filePath = string.Concat(uploadPath, "\\", file);
string filePath = Path.Combine(uploadPath, "\\", file);
fileResponseMessage = new FileResponseMessage();

if (!overWrite && File.Exists(filePath))
Expand Down Expand Up @@ -288,12 +354,10 @@ private HttpResponseMessage ProcessUploadRequest(bool overWrite)
}
catch (Exception exception)
{
response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception);
throw exception;
}
}
return response;
}


}
}
8 changes: 1 addition & 7 deletions WebAPIDocumentationExtenderLibrary/RegisterAPIHelp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,14 @@
//|---------------------------------------------------------------|
//| WEB API DOUCMENTATION EXTENDER LIBRARY |
//|---------------------------------------------------------------|

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Http;
using System.Xml.Serialization;
using System.Web.Http.Controllers;

namespace WebAPIDocumentationExtenderLibrary
{
Expand Down

0 comments on commit 96b318b

Please sign in to comment.