Skip to content
This repository was archived by the owner on Apr 10, 2021. It is now read-only.

Commit df1ed59

Browse files
authored
Merge pull request #159 from arduosoft/features/js-client
Features/js client
2 parents a7eac3d + f04771c commit df1ed59

File tree

5 files changed

+208
-7
lines changed

5 files changed

+208
-7
lines changed

Diff for: Plugins/RawCMS.Plugins.Core/Controllers/JsLambdaController.cs

+37-7
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77
// <autogenerated>true</autogenerated>
88
//******************************************************************************
99
using Jint;
10+
using Jint.Parser;
1011
using Microsoft.AspNetCore.Authorization;
1112
using Microsoft.AspNetCore.Mvc;
13+
using Microsoft.Extensions.Logging;
1214
using Newtonsoft.Json.Linq;
1315
using RawCMS.Library.Core;
1416
using RawCMS.Library.Core.Attributes;
17+
using RawCMS.Library.JavascriptClient;
1518
using RawCMS.Library.Service;
19+
using System;
1620
using System.Collections.Generic;
21+
using System.Reflection.Metadata;
1722

1823
namespace RawCMS.Plugins.Core.Controllers
1924
{
@@ -24,11 +29,14 @@ public class JsLambdaController
2429
{
2530
private readonly AppEngine lambdaManager;
2631
private readonly CRUDService crudService;
32+
private readonly ILogger logger;
2733

28-
public JsLambdaController(AppEngine lambdaManager, CRUDService crudService)
34+
35+
public JsLambdaController(AppEngine lambdaManager, CRUDService crudService, ILogger logger)
2936
{
3037
this.lambdaManager = lambdaManager;
3138
this.crudService = crudService;
39+
this.logger = logger;
3240
}
3341

3442
[AllowAnonymous]
@@ -47,12 +55,34 @@ public JObject Post(string lambda, [FromBody] JObject input)
4755
string code = js["Code"].ToString();
4856

4957
Dictionary<string, object> tmpIn = input.ToObject<Dictionary<string, object>>();
50-
Dictionary<string, object> tmpOur = new Dictionary<string, object>();
51-
Engine add = new Engine()
52-
.SetValue("input", tmpIn)
53-
.SetValue("output", tmpOur)
54-
.Execute(code);
55-
return JObject.FromObject(tmpOur);
58+
Dictionary<string, object> tmpOut = new Dictionary<string, object>();
59+
60+
61+
Engine engine = new Engine((x) => { x.AllowClr(typeof(JavascriptRestClient).Assembly); x.AllowClr(typeof(JavascriptRestClientRequest).Assembly); });
62+
63+
64+
engine.SetValue("input", tmpIn);
65+
engine.SetValue("RAWCMSRestClient", Jint.Runtime.Interop.TypeReference.CreateTypeReference(engine, typeof(JavascriptRestClient)));
66+
engine.SetValue("RAWCMSRestClientRequest", Jint.Runtime.Interop.TypeReference.CreateTypeReference(engine, typeof(JavascriptRestClientRequest)));
67+
68+
engine.SetValue("output", tmpOut);
69+
70+
try
71+
{
72+
73+
74+
75+
logger.LogDebug($"calling lambda: {lambda}");
76+
engine.Execute(code);
77+
78+
}
79+
catch (Exception e)
80+
{
81+
logger.LogError($"Error on lambda javascript script: {e.Message} ");
82+
tmpOut.Add("Error", e.Message);
83+
}
84+
logger.LogDebug($"Lambda response: {tmpOut}");
85+
return JObject.FromObject(tmpOut);
5686
}
5787
}
5888
}
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//******************************************************************************
2+
// <copyright file="license.md" company="RawCMS project (https://github.com/arduosoft/RawCMS)">
3+
// Copyright (c) 2019 RawCMS project (https://github.com/arduosoft/RawCMS)
4+
// RawCMS project is released under GPL3 terms, see LICENSE file on repository root at https://github.com/arduosoft/RawCMS .
5+
// </copyright>
6+
// <author>Daniele Fontani, Emanuele Bucarelli, Francesco Mina'</author>
7+
// <autogenerated>true</autogenerated>
8+
//******************************************************************************
9+
using Microsoft.Extensions.Logging;
10+
using Newtonsoft.Json.Linq;
11+
using RestSharp;
12+
using System;
13+
using System.Collections.Generic;
14+
using System.Text;
15+
16+
namespace RawCMS.Library.JavascriptClient
17+
{
18+
public class JavascriptRestClient
19+
{
20+
public ILogger logger { get; private set; }
21+
public JavascriptRestClient()
22+
{
23+
24+
}
25+
26+
27+
public JavascriptRestClientMessage<JObject> Execute(JavascriptRestClientRequest javascriptRequest)
28+
{
29+
JavascriptRestClientMessage<JObject> response = new JavascriptRestClientMessage<JObject>(new JObject());
30+
IRestResponse restResponse = null;
31+
32+
RestClient client = new RestClient(javascriptRequest.Url);
33+
Method reqMethod = Method.GET;
34+
if (!Enum.TryParse(javascriptRequest.Method, true, out reqMethod))
35+
{
36+
// log error method...
37+
}
38+
39+
40+
RestRequest request = new RestRequest()
41+
{
42+
//request headers type
43+
RequestFormat = DataFormat.Json,
44+
Method = reqMethod
45+
};
46+
47+
// set the request header
48+
if (javascriptRequest.Header!=null)
49+
{
50+
foreach (var el in javascriptRequest.Header)
51+
{
52+
request.AddHeader(el.Key, el.Value);
53+
54+
}
55+
}
56+
57+
58+
//add parameters to request
59+
request.Parameters.Clear();
60+
if (javascriptRequest.QueryParams!=null)
61+
{
62+
foreach (var el in javascriptRequest.QueryParams)
63+
{
64+
request.AddParameter(el.Key, el.Value);
65+
}
66+
}
67+
68+
if (!string.IsNullOrEmpty(javascriptRequest.Body))
69+
{
70+
request.AddJsonBody(javascriptRequest.Body);
71+
}
72+
73+
74+
// TODO: add logging
75+
//var uri = client.BuildUri(request);
76+
//logger.LogDebug($"request URI: {uri.AbsoluteUri}");
77+
78+
try
79+
{
80+
restResponse = client.Execute(request);
81+
82+
}
83+
catch (Exception untrapped)
84+
{
85+
//TODO: log here
86+
response.Errors.Add(new Library.Core.Error()
87+
{
88+
Code = "UNEXPEXTED",
89+
Title = "JavascriptRestClient produces an unexpexted error",
90+
Description = untrapped.Message,
91+
});
92+
}
93+
//make the API request and get a response
94+
95+
response.Data = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(restResponse.Content);
96+
return response;
97+
}
98+
99+
100+
//public string getText()
101+
//{
102+
// return "text";
103+
//}
104+
//public static string getStaticText()
105+
//{
106+
// return "static text";
107+
//}
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//******************************************************************************
2+
// <copyright file="license.md" company="RawCMS project (https://github.com/arduosoft/RawCMS)">
3+
// Copyright (c) 2019 RawCMS project (https://github.com/arduosoft/RawCMS)
4+
// RawCMS project is released under GPL3 terms, see LICENSE file on repository root at https://github.com/arduosoft/RawCMS .
5+
// </copyright>
6+
// <author>Daniele Fontani, Emanuele Bucarelli, Francesco Mina'</author>
7+
// <autogenerated>true</autogenerated>
8+
//******************************************************************************
9+
using Microsoft.AspNetCore.Http;
10+
using Newtonsoft.Json.Linq;
11+
using RestSharp;
12+
using System;
13+
using System.Collections.Generic;
14+
15+
namespace RawCMS.Library.JavascriptClient
16+
{
17+
public class JavascriptRestClientRequest
18+
{
19+
public string Url { get; set; }
20+
public Dictionary<string,string> Header { get; set; }
21+
public Dictionary<string,string> QueryParams { get; set; }
22+
public string Method { get; set; }
23+
public string Body { get; set; }
24+
}
25+
}

Diff for: RawCMS.Library/JavascriptClient/RestMessage.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//******************************************************************************
2+
// <copyright file="license.md" company="RawCMS project (https://github.com/arduosoft/RawCMS)">
3+
// Copyright (c) 2019 RawCMS project (https://github.com/arduosoft/RawCMS)
4+
// RawCMS project is released under GPL3 terms, see LICENSE file on repository root at https://github.com/arduosoft/RawCMS .
5+
// </copyright>
6+
// <author>Daniele Fontani, Emanuele Bucarelli, Francesco Mina'</author>
7+
// <autogenerated>true</autogenerated>
8+
//******************************************************************************
9+
using RawCMS.Library.Core;
10+
using System.Collections.Generic;
11+
12+
namespace RawCMS.Library.JavascriptClient
13+
{
14+
public enum RestStatus
15+
{
16+
OK,
17+
KO,
18+
CompletedWithErrors
19+
}
20+
21+
public class JavascriptRestClientMessage<T>
22+
{
23+
public List<Error> Errors { get; set; } = new List<Error>();
24+
public List<Error> Warnings { get; set; } = new List<Error>();
25+
public List<Error> Infos { get; set; } = new List<Error>();
26+
27+
public RestStatus Status { get; set; }
28+
29+
public T Data { get; set; }
30+
31+
public JavascriptRestClientMessage(T item)
32+
{
33+
Data = item;
34+
}
35+
}
36+
}

Diff for: RawCMS.Library/RawCMS.Library.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
2222
<PackageReference Include="NLog" Version="4.5.6" />
2323
<PackageReference Include="NLog.Web.AspNetCore" Version="4.5.4" />
24+
<PackageReference Include="RestSharp" Version="106.6.10" />
2425
</ItemGroup>
2526

2627
<ItemGroup>

0 commit comments

Comments
 (0)