pull/1/head
Ogoun 5 years ago
parent 80a35e4063
commit 5ccb2120bf

@ -1,13 +1,41 @@
using System; using Newtonsoft.Json;
using ZeroLevel.Services; using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using ZeroLevel.Services.Web;
namespace TestApp namespace TestApp
{ {
public class TestQuery
{
public string Name { get; set; }
public int Age { get; set; }
public string[] Roles { get; set; }
}
internal static class Program internal static class Program
{ {
private static string Serialize(object instance)
{
return JsonConvert.SerializeObject(instance);
}
private static void Main(string[] args) private static void Main(string[] args)
{ {
var fiber = new Fiber(); var t = new TestQuery
{
Age = 133,
Roles = new[] { "ad\"\"\"min", "user", "operator" },
Name = "su"
};
var builder = new UrlBuilder(Serialize);
var url = builder.BuildRequestUrlFromDTO("http://google.com", "/api/v0/getuserinfo", t);
Console.WriteLine(url);
Console.ReadKey();
/*var fiber = new Fiber();
fiber fiber
.Add((s) => { Console.WriteLine("1"); s.Add<int>("1", 1); return s; }) .Add((s) => { Console.WriteLine("1"); s.Add<int>("1", 1); return s; })
.Add((s) => { Console.WriteLine("2"); s.Add<int>("2", 2); return s; }) .Add((s) => { Console.WriteLine("2"); s.Add<int>("2", 2); return s; })
@ -21,9 +49,9 @@ namespace TestApp
foreach (var key in result.Keys<int>()) foreach (var key in result.Keys<int>())
{ {
Console.WriteLine($"{key}: {result.Get<int>(key)}"); Console.WriteLine($"{key}: {result.Get<int>(key)}");
} }*/
Console.ReadKey();
/*Configuration.Save(Configuration.ReadFromApplicationConfig()); /*Configuration.Save(Configuration.ReadFromApplicationConfig());

@ -5,6 +5,10 @@
<TargetFramework>netcoreapp2.2</TargetFramework> <TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ZeroLevel\ZeroLevel.csproj" /> <ProjectReference Include="..\ZeroLevel\ZeroLevel.csproj" />
</ItemGroup> </ItemGroup>

@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
namespace ZeroLevel.Services.Web
{
public class UrlBuilder
{
private readonly Func<object, string> _serializer;
public UrlBuilder(Func<object, string> serializer)
{
_serializer = serializer;
}
public Uri BuildRequestUrlFromDTO<T>(string baseUri, string resource, T instance)
{
if (null == instance)
{
return BuildRequestUrl(baseUri, resource, null);
}
var members = typeof(T).GetMembers(
BindingFlags.Public |
BindingFlags.FlattenHierarchy |
BindingFlags.GetField |
BindingFlags.GetProperty |
BindingFlags.Instance);
var parameters = new Dictionary<string, object>();
foreach (var member in members)
{
switch (member.MemberType)
{
case MemberTypes.Property:
parameters.Add(member.Name.ToLowerInvariant(), (member as PropertyInfo).GetValue(instance));
break;
case MemberTypes.Field:
parameters.Add(member.Name.ToLowerInvariant(), (member as FieldInfo).GetValue(instance));
break;
default:
continue;
}
}
return BuildRequestUrl(baseUri, resource, parameters);
}
public Uri BuildRequestUrl(string baseUri, string resource, IDictionary<string, object> parameters)
{
if (null == resource) throw new ArgumentNullException("resource");
var stringBuilder = new StringBuilder(baseUri);
if (baseUri[baseUri.Length - 1] != '/')
stringBuilder.Append('/');
if (resource[0] != '/')
{
stringBuilder.Append(resource);
}
else
{
stringBuilder.Append(resource.Substring(1));
}
if (parameters != null && parameters.Count > 0)
{
stringBuilder.Append("?");
foreach (var pair in parameters)
{
if (pair.Value == null)
{
stringBuilder.Append(pair.Value);
}
else
{
var vtype = pair.Value.GetType();
if (vtype.IsArray)
{
if (vtype.GetElementType() == typeof(string))
{
var arr = (string[])pair.Value;
stringBuilder.Append(string.Join("&", arr.Select(i => $"{HttpUtility.UrlEncode(pair.Key)}[]={HttpUtility.UrlEncode(i)}")));
}
else
{
var arr = (object[])pair.Value;
stringBuilder.Append(string.Join("&", arr.Select(i => $"{HttpUtility.UrlEncode(pair.Key)}[]={HttpUtility.UrlEncode(_serializer(i))}")));
}
}
else
{
if (vtype == typeof(string))
{
stringBuilder.Append($"{HttpUtility.UrlEncode(pair.Key)}={HttpUtility.UrlEncode((string)pair.Value)}");
}
else
{
stringBuilder.Append($"{HttpUtility.UrlEncode(pair.Key)}={HttpUtility.UrlEncode(_serializer(pair.Value))}");
}
}
}
stringBuilder.Append("&");
}
}
return new Uri(stringBuilder.ToString().TrimEnd('&'));
}
}
}

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -522,6 +524,6 @@ namespace ZeroLevel.Services.Web
i++; i++;
} }
return result; return result;
} }
} }
} }
Loading…
Cancel
Save

Powered by TurnKey Linux.