From b46c5d3d63fe2c6be1fa81505f073ddb5a83d1c6 Mon Sep 17 00:00:00 2001 From: Shmuel Englard Date: Fri, 22 Feb 2013 13:50:41 -0500 Subject: [PATCH 01/39] Added JavaScript version --- src/PortableRest.sln | 6 + src/PortableRestJs/PortableRestJs.csproj | 106 +++++++++ src/PortableRestJs/Properties/AssemblyInfo.cs | 35 +++ src/PortableRestJs/Web.Debug.config | 30 +++ src/PortableRestJs/Web.Release.config | 31 +++ src/PortableRestJs/Web.config | 13 + src/PortableRestJs/portableRest.js | 222 ++++++++++++++++++ 7 files changed, 443 insertions(+) create mode 100644 src/PortableRestJs/PortableRestJs.csproj create mode 100644 src/PortableRestJs/Properties/AssemblyInfo.cs create mode 100644 src/PortableRestJs/Web.Debug.config create mode 100644 src/PortableRestJs/Web.Release.config create mode 100644 src/PortableRestJs/Web.config create mode 100644 src/PortableRestJs/portableRest.js diff --git a/src/PortableRest.sln b/src/PortableRest.sln index 988dabe..7f020fe 100644 --- a/src/PortableRest.sln +++ b/src/PortableRest.sln @@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{E43B39 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PortableRest.Tests", "PortableRest.Tests\PortableRest.Tests.csproj", "{500BCEAB-8F93-4BDE-84DB-D21147323837}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PortableRestJs", "PortableRestJs\PortableRestJs.csproj", "{3D45C414-46FA-4427-8B6C-26C7DEDF40DC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -26,6 +28,10 @@ Global {500BCEAB-8F93-4BDE-84DB-D21147323837}.Debug|Any CPU.Build.0 = Debug|Any CPU {500BCEAB-8F93-4BDE-84DB-D21147323837}.Release|Any CPU.ActiveCfg = Release|Any CPU {500BCEAB-8F93-4BDE-84DB-D21147323837}.Release|Any CPU.Build.0 = Release|Any CPU + {3D45C414-46FA-4427-8B6C-26C7DEDF40DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3D45C414-46FA-4427-8B6C-26C7DEDF40DC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3D45C414-46FA-4427-8B6C-26C7DEDF40DC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3D45C414-46FA-4427-8B6C-26C7DEDF40DC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/PortableRestJs/PortableRestJs.csproj b/src/PortableRestJs/PortableRestJs.csproj new file mode 100644 index 0000000..de47c97 --- /dev/null +++ b/src/PortableRestJs/PortableRestJs.csproj @@ -0,0 +1,106 @@ + + + + + Debug + AnyCPU + + + 2.0 + {3D45C414-46FA-4427-8B6C-26C7DEDF40DC} + {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} + Library + Properties + PortableRestJs + PortableRestJs + v4.0 + true + + + + + + + true + full + false + bin\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Web.config + + + Web.config + + + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + + + + + True + True + 0 + / + http://localhost:56617/ + False + False + + + False + + + + + + \ No newline at end of file diff --git a/src/PortableRestJs/Properties/AssemblyInfo.cs b/src/PortableRestJs/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..bf8cf93 --- /dev/null +++ b/src/PortableRestJs/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("PortableRestJs")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("PortableRestJs")] +[assembly: AssemblyCopyright("Copyright © 2013")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("f94e82b7-743c-4e6f-bf1d-fd1c7ea7a860")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/PortableRestJs/Web.Debug.config b/src/PortableRestJs/Web.Debug.config new file mode 100644 index 0000000..2e302f9 --- /dev/null +++ b/src/PortableRestJs/Web.Debug.config @@ -0,0 +1,30 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/PortableRestJs/Web.Release.config b/src/PortableRestJs/Web.Release.config new file mode 100644 index 0000000..c358444 --- /dev/null +++ b/src/PortableRestJs/Web.Release.config @@ -0,0 +1,31 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/PortableRestJs/Web.config b/src/PortableRestJs/Web.config new file mode 100644 index 0000000..c72873f --- /dev/null +++ b/src/PortableRestJs/Web.config @@ -0,0 +1,13 @@ + + + + + + + + + + diff --git a/src/PortableRestJs/portableRest.js b/src/PortableRestJs/portableRest.js new file mode 100644 index 0000000..d7a9f34 --- /dev/null +++ b/src/PortableRestJs/portableRest.js @@ -0,0 +1,222 @@ +(function (window) +{ + "use strict"; + + if (window.PortableRest !== undefined) + { + return; + } + + window.PortableRest = {}; + + window.PortableRest.ContentTypes = + { + FormUrlEncoded: 0, + Json: 1, + Xml: 2 + }; + + window.PortableRest.HttpMethod = + { + Delete: "DELETE", + Get: "GET", + Head: "HEAD", + Options: "OPTIONS", + Post: "POST", + Put: "PUT", + Trace: "TRACE" + }; + + window.PortableRest.RestRequest = function (resource, method, ignoreRoot) + { + /// Creates a new RestRequest instance for a given Resource and Method. + /// The specific resource to access. + /// The HTTP method to use for the request. + /// + /// + + if (!(this instanceof window.PortableRest.RestRequest)) + { + return new window.PortableRest.RestRequest(resource, method, ignoreRoot); + } + + this._urlSegments = []; + this._parameters = []; + + this.contentType = window.PortableRest.ContentTypes.FormUrlEncoded; + this.dateFormat = ""; + this.ignoreRootElement = ignoreRoot || false; + this.ignoreXmlAttributes = false; + this.method = method || window.PortableRest.HttpMethod.Get; + this.resource = resource || ""; + }; + + window.PortableRest.RestRequest.prototype.addUrlSegment = function (key, value) + { + /// + /// + /// + + this._urlSegments.push({ key: key, value: value }); + }; + + window.PortableRest.RestRequest.prototype.addParameter = function (key, value) + { + /// + /// + /// + + this._parameters.push({ key: key, value: value }); + }; + + window.PortableRest.RestRequest.prototype._getFormattedResource = function (baseUrl) + { + /// + /// + /// + + + for (var urlSegmentsIndex = 0, urlSegmentsLength = this._urlSegments.length; urlSegmentsIndex < urlSegmentsLength; urlSegmentsIndex++) + { + var segment = this._urlSegments[urlSegmentsIndex]; + this.resource = this.resource.replace("{" + segment.key + "}", encodeURIComponent(segment.value)); + } + + if ((this.resource !== null) && (this.resource !== undefined) && (this.resource !== "") && (this.resource.indexOf("/") === 0)) + { + this.resource = this.resource.substr(1); + } + + if ((baseUrl !== null) && (baseUrl !== undefined)) + { + this.resource = ((this.resource !== null) && (this.resource !== undefined) && (this.resource !== "")) ? baseUrl : baseUrl + "/" + this.resource; + } + + return this.resource; + }; + + window.PortableRest.RestRequest.prototype._getContentType = function () + { + /// + /// + + switch (this.contentType) + { + case window.PortableRest.ContentTypes.FormUrlEncoded: + return "application/x-www-form-urlencoded"; + case window.PortableRest.ContentTypes.Json: + return "application/xml"; + default: + return "application/json"; + } + }; + + window.PortableRest.RestRequest.prototype._getRequestBody = function () + { + /// + /// + + var parameters = ""; + + switch (this.contentType) + { + case window.PortableRest.ContentTypes.FormUrlEncoded: + for (var parameterIndex = 0, parametersLength = this._parameters.length; parameterIndex < parametersLength; parameterIndex++) + { + var parameter = this._parameters[parameterIndex]; + parameters = parameters + (parameters.length > 0 ? "&" : "") + encodeURIComponent(parameter.key) + "=" + encodeURIComponent(parameter.value.toString()); + } + break; + case window.PortableRest.ContentTypes.Xml: + throw new Error("Sending XML is not yet supported, but will be added in a future release."); + break; + case window.PortableRest.ContentTypes.Json: + parameters = this._parameters.length > 0 ? JSON.stringify(this._parameters[0].value) : ""; + break; + } + + return parameters; + }; + + window.PortableRest.RestClient = function () + { + /// Creates a new instance of the RestClient class. + /// + + if (!(this instanceof window.PortableRest.RestClient)) + { + return new window.PortableRest.RestClient(); + } + + this.baseUrl = ""; + this.dateFormat = ""; + this.userAgent = ""; + this._headers = []; + }; + + window.PortableRest.RestClient.prototype.addHeader = function (key, value) + { + /// Adds a header for a given string key and string value. + /// The header to add. + /// The value of the header being added. + + this._headers.push({ key: key, value: value }); + }; + + window.PortableRest.RestClient.prototype.execute = function (restRequest, callback) + { + /// Executes an asynchronous request to the given resource and deserializes the response + /// The RestRequest to execute. + /// + /// + + var url = restRequest._getFormattedResource(this.baseUrl); + + if (((restRequest.dateFormat === null) || (restRequest.dateFormat === undefined) || (restRequest.dateFormat === "")) && (this.dateFormat !== null) && (this.dateFormat !== undefined) && (this.dateFormat !== "")) + { + restRequest.dateFormat = this.dateFormat; + } + + this._client = new XMLHttpRequest(); + + for (var headerIndex = 0, headersLength = this._headers.length; headerIndex < headersLength; headerIndex++) + { + var header = this._headers[headerIndex]; + this._client.setRequestHeader(header.key, header.value); + } + + this._client.open(restRequest.method, url, true); + + var body = null; + + if ((restRequest.method === window.PortableRest.HttpMethod.Post) || (restRequest.method === window.PortableRest.HttpMethod.Put)) + { + this._client.setRequestHeader("Content-Type", restRequest._getContentType()); + body = restRequest._getRequestBody(); + } + + var $this = this; + this._client.onreadystatechange = function () + { + if ($this._client.readyState === 4) + { + if ($this._client.status !== 200) + { + throw new Error($this._client.status); + } + + if (($this._client.responseType.indexOf("application/xml") !== -1) && ($this._client.responseXML !== null) && ($this._client.responseXML !== undefined)) + { + callback($this._client.responseXML); + } + else + { + callback(JSON.parse($this._client.responseText)); + } + } + }; + + this._client.send(body); + }; + +})(window); \ No newline at end of file From 140b14a6138e259fda4434625a95e4b48d5e5dd6 Mon Sep 17 00:00:00 2001 From: Shmuel Englard Date: Mon, 25 Feb 2013 13:07:50 -0500 Subject: [PATCH 02/39] Fixed some bugs in JS version and added JS tests --- src/PortableRestJs/test.aspx | 1 + src/PortableRestJs/test.aspx.cs | 36 +++++++++ src/PortableRestJs/test.aspx.designer.cs | 15 ++++ src/PortableRestJs/test.html | 86 +++++++++++++++++++++ src/PortableRestJs/xmlTest.aspx | 1 + src/PortableRestJs/xmlTest.aspx.cs | 26 +++++++ src/PortableRestJs/xmlTest.aspx.designer.cs | 15 ++++ 7 files changed, 180 insertions(+) create mode 100644 src/PortableRestJs/test.aspx create mode 100644 src/PortableRestJs/test.aspx.cs create mode 100644 src/PortableRestJs/test.aspx.designer.cs create mode 100644 src/PortableRestJs/test.html create mode 100644 src/PortableRestJs/xmlTest.aspx create mode 100644 src/PortableRestJs/xmlTest.aspx.cs create mode 100644 src/PortableRestJs/xmlTest.aspx.designer.cs diff --git a/src/PortableRestJs/test.aspx b/src/PortableRestJs/test.aspx new file mode 100644 index 0000000..a669a86 --- /dev/null +++ b/src/PortableRestJs/test.aspx @@ -0,0 +1 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="PortableRestJs.test" %> \ No newline at end of file diff --git a/src/PortableRestJs/test.aspx.cs b/src/PortableRestJs/test.aspx.cs new file mode 100644 index 0000000..2f59789 --- /dev/null +++ b/src/PortableRestJs/test.aspx.cs @@ -0,0 +1,36 @@ +using System; +using System.IO; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace PortableRestJs +{ + public partial class test : System.Web.UI.Page + { + protected void Page_Load(object sender, EventArgs e) + { + Response.Clear(); + string body = string.Empty; + using (StreamReader streamReader = new StreamReader(Request.InputStream)) + { + body = streamReader.ReadToEnd(); + } + JObject jObject = null; + switch(Request.ContentType) + { + case "application/json": + jObject = JsonConvert.DeserializeObject(body); + break; + case "application/x-www-form-urlencoded": + jObject = new JObject(); + foreach (string postField in Request.Form.AllKeys) + { + jObject.AddFirst(new JProperty(postField, Request.Form[postField])); + } + break; + } + Response.Write(JsonConvert.SerializeObject(jObject)); + Response.ContentType = "application/json"; + } + } +} \ No newline at end of file diff --git a/src/PortableRestJs/test.aspx.designer.cs b/src/PortableRestJs/test.aspx.designer.cs new file mode 100644 index 0000000..cf44240 --- /dev/null +++ b/src/PortableRestJs/test.aspx.designer.cs @@ -0,0 +1,15 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace PortableRestJs { + + + public partial class test { + } +} diff --git a/src/PortableRestJs/test.html b/src/PortableRestJs/test.html new file mode 100644 index 0000000..9531407 --- /dev/null +++ b/src/PortableRestJs/test.html @@ -0,0 +1,86 @@ + + + + + + + + + +
+
    +
  • JSON Test:
  • +
  • Form Test:
  • +
  • Xml Test:
  • +
+
+ + diff --git a/src/PortableRestJs/xmlTest.aspx b/src/PortableRestJs/xmlTest.aspx new file mode 100644 index 0000000..559e647 --- /dev/null +++ b/src/PortableRestJs/xmlTest.aspx @@ -0,0 +1 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="xmlTest.aspx.cs" Inherits="PortableRestJs.xmlTest" %> \ No newline at end of file diff --git a/src/PortableRestJs/xmlTest.aspx.cs b/src/PortableRestJs/xmlTest.aspx.cs new file mode 100644 index 0000000..ed5854a --- /dev/null +++ b/src/PortableRestJs/xmlTest.aspx.cs @@ -0,0 +1,26 @@ +using System; +using System.IO; +using System.Xml.Linq; +using Newtonsoft.Json; + +namespace PortableRestJs +{ + public partial class xmlTest : System.Web.UI.Page + { + protected void Page_Load(object sender, EventArgs e) + { + Response.Clear(); + string body = string.Empty; + using (StreamReader streamReader = new StreamReader(Request.InputStream)) + { + body = streamReader.ReadToEnd(); + } + + XDocument xDoc = JsonConvert.DeserializeXNode(body); + + Response.Write(xDoc.ToString(SaveOptions.DisableFormatting)); + Response.ContentType = "application/xml"; + Response.Headers.Add("Content-Type", Response.ContentType); + } + } +} \ No newline at end of file diff --git a/src/PortableRestJs/xmlTest.aspx.designer.cs b/src/PortableRestJs/xmlTest.aspx.designer.cs new file mode 100644 index 0000000..4a6a324 --- /dev/null +++ b/src/PortableRestJs/xmlTest.aspx.designer.cs @@ -0,0 +1,15 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace PortableRestJs { + + + public partial class xmlTest { + } +} From a3eeae5f4a6ff1a32d9241b8b7b90ff00c466a64 Mon Sep 17 00:00:00 2001 From: Shmuel Englard Date: Mon, 25 Feb 2013 13:24:21 -0500 Subject: [PATCH 03/39] Missed some files in last commit --- .../PortableRest.Tests.csproj | 1 + src/PortableRest.Tests/packages.config | 1 + src/PortableRest/PortableRest.csproj | 23 ++++++++++-------- src/PortableRestJs/PortableRestJs.csproj | 24 +++++++++++++++++++ src/PortableRestJs/portableRest.js | 13 +++++----- 5 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/PortableRest.Tests/PortableRest.Tests.csproj b/src/PortableRest.Tests/PortableRest.Tests.csproj index 3e6d727..38c3e4b 100644 --- a/src/PortableRest.Tests/PortableRest.Tests.csproj +++ b/src/PortableRest.Tests/PortableRest.Tests.csproj @@ -105,6 +105,7 @@ +