Skip to content

Commit

Permalink
WebApi.Client
Browse files Browse the repository at this point in the history
  • Loading branch information
mgesing committed Sep 29, 2022
1 parent f5a8d45 commit 719e267
Show file tree
Hide file tree
Showing 20 changed files with 2,454 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Smartstore.Tools.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{5135A2B0
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Smartstore.Packager", "tools\Smartstore.Packager\Smartstore.Packager.csproj", "{4BFFDE97-4EC0-423E-91C6-A202FD0A5978}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Smartstore.WebApi.Client", "tools\Smartstore.WebApi.Client\Smartstore.WebApi.Client.csproj", "{784C2C99-F3D5-4B70-B571-C5A944C7DC96}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -50,13 +52,20 @@ Global
{4BFFDE97-4EC0-423E-91C6-A202FD0A5978}.DebugNoRazorCompile|Any CPU.Build.0 = Debug|Any CPU
{4BFFDE97-4EC0-423E-91C6-A202FD0A5978}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BFFDE97-4EC0-423E-91C6-A202FD0A5978}.Release|Any CPU.Build.0 = Release|Any CPU
{784C2C99-F3D5-4B70-B571-C5A944C7DC96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{784C2C99-F3D5-4B70-B571-C5A944C7DC96}.Debug|Any CPU.Build.0 = Debug|Any CPU
{784C2C99-F3D5-4B70-B571-C5A944C7DC96}.DebugNoRazorCompile|Any CPU.ActiveCfg = Debug|Any CPU
{784C2C99-F3D5-4B70-B571-C5A944C7DC96}.DebugNoRazorCompile|Any CPU.Build.0 = Debug|Any CPU
{784C2C99-F3D5-4B70-B571-C5A944C7DC96}.Release|Any CPU.ActiveCfg = Release|Any CPU
{784C2C99-F3D5-4B70-B571-C5A944C7DC96}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{1DE93C7F-22CE-462C-BB6E-7282F4403F2E} = {4C0385CB-1ABE-4569-AA9D-0C3DCBE44479}
{4BFFDE97-4EC0-423E-91C6-A202FD0A5978} = {5135A2B0-95B5-46AD-906A-70A2364B892B}
{784C2C99-F3D5-4B70-B571-C5A944C7DC96} = {5135A2B0-95B5-46AD-906A-70A2364B892B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A0A0B995-5D0B-458E-BCC0-1071B76949D1}
Expand Down
45 changes: 45 additions & 0 deletions tools/Smartstore.WebApi.Client/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Smartstore.WebApi.Client.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<Smartstore.WebApi.Client.Properties.Settings>
<setting name="ApiPublicKey" serializeAs="String">
<value />
</setting>
<setting name="ApiSecretKey" serializeAs="String">
<value />
</setting>
<setting name="ApiUrl" serializeAs="String">
<value />
</setting>
<setting name="ApiPaths" serializeAs="String">
<value />
</setting>
<setting name="ApiContent" serializeAs="String">
<value />
</setting>
<setting name="ApiQuery" serializeAs="String">
<value />
</setting>
<setting name="ApiPaths2" serializeAs="String">
<value />
</setting>
<setting name="ApiVersion" serializeAs="String">
<value />
</setting>
<setting name="ApiHeaders" serializeAs="String">
<value />
</setting>
<setting name="FileUpload" serializeAs="String">
<value />
</setting>
<setting name="ApiProxyPort" serializeAs="String">
<value />
</setting>
</Smartstore.WebApi.Client.Properties.Settings>
</userSettings>
</configuration>
62 changes: 62 additions & 0 deletions tools/Smartstore.WebApi.Client/Extensions/ComboBoxExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Text;

namespace Smartstore.WebApi.Client
{
internal static class ComboBoxExtensions
{
private const char _delimiter = '¶';

public static void InsertRolled(this ComboBox combo, string str, int max)
{
if (!string.IsNullOrEmpty(str))
{
int i;
for (i = combo.Items.Count - 1; i >= 0; --i)
{
if (string.Compare(combo.Items[i].ToString(), str, true) == 0)
combo.Items.RemoveAt(i);
}

combo.Items.Insert(0, str);

for (i = combo.Items.Count - 1; i > max; --i)
{
combo.Items.RemoveAt(i);
}

combo.Text = str;
}
}

public static void FromString(this ComboBox.ObjectCollection coll, string values)
{
if (!string.IsNullOrWhiteSpace(values))
{
string[] items = values.Split(new[] { _delimiter }, StringSplitOptions.RemoveEmptyEntries);
coll.AddRange(items);
}
}

public static string IntoString(this ComboBox.ObjectCollection coll)
{
if (coll.Count <= 0)
return string.Empty;

var sb = new StringBuilder();
foreach (var item in coll)
{
if (sb.Length > 0)
sb.Append(_delimiter);
sb.Append(item);
}

return string.Join(_delimiter.ToString(), sb.ToString());
}

public static void RemoveCurrent(this ComboBox combo)
{
combo.Items.Remove(combo.Text);
combo.ResetText();
}
}
}
49 changes: 49 additions & 0 deletions tools/Smartstore.WebApi.Client/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Diagnostics;
using System.Globalization;

namespace Smartstore.WebApi.Client
{
internal static class StringExtensions
{
public static void Dump(this string value, bool appendMarks = false)
{
Debug.WriteLine(value);
Debug.WriteLineIf(appendMarks, "------------------------------------------------");
}

public static DialogResult Box(this string message, MessageBoxButtons buttons = MessageBoxButtons.OK, MessageBoxIcon icon = MessageBoxIcon.Information)
{
return MessageBox.Show(message, Program.AppName, buttons, icon);
}

public static int ToInt(this string value, int defaultValue = 0)
{
int result;
if (int.TryParse(value, out result))
{
return result;
}
return defaultValue;
}

public static string EmptyNull(this string value)
{
return (value ?? string.Empty).Trim();
}

public static bool IsEmpty(this string value)
{
return string.IsNullOrWhiteSpace(value);
}

public static bool HasValue(this string value)
{
return !string.IsNullOrWhiteSpace(value);
}

public static string FormatInvariant(this string format, params object[] objects)
{
return string.Format(CultureInfo.InvariantCulture, format, objects);
}
}
}
34 changes: 34 additions & 0 deletions tools/Smartstore.WebApi.Client/HttpClient/WebApiRequestContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Text;

namespace Smartstore.WebApi.Client
{
public class WebApiRequestContext
{
public string PublicKey { get; set; }
public string SecretKey { get; set; }

public string Url { get; set; }
public int ProxyPort { get; set; }
public string HttpMethod { get; set; }
public string HttpAcceptType { get; set; }
public string AdditionalHeaders { get; set; }

public bool IsValid => !string.IsNullOrWhiteSpace(PublicKey) && !string.IsNullOrWhiteSpace(SecretKey) &&
!string.IsNullOrWhiteSpace(Url) &&
!string.IsNullOrWhiteSpace(HttpMethod) && !string.IsNullOrWhiteSpace(HttpAcceptType);

public override string ToString()
{
var sb = new StringBuilder();

sb.AppendLine("PublicKey: " + PublicKey);
sb.AppendLine("SecretKey: " + SecretKey);
sb.AppendLine("Url: " + Url);
sb.AppendLine("Proxy Port: " + (ProxyPort > 0 ? ProxyPort.ToString() : string.Empty));
sb.AppendLine("HttpMethod: " + HttpMethod);
sb.AppendLine("HttpAcceptType: " + HttpAcceptType);

return sb.ToString();
}
}
}
44 changes: 44 additions & 0 deletions tools/Smartstore.WebApi.Client/HttpClient/WebApiResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Newtonsoft.Json.Linq;
using Smartstore.WebApi.Client.Models;

namespace Smartstore.WebApi.Client
{
public class WebApiResponse
{
public string Status { get; set; }
public string Headers { get; set; }
public string Content { get; set; }
public string ContentType { get; set; }
public long ContentLength { get; set; }

/// <seealso cref="http://weblog.west-wind.com/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing" />
/// <seealso cref="http://james.newtonking.com/json/help/index.html?topic=html/QueryJsonDynamic.htm" />
/// <seealso cref="http://james.newtonking.com/json/help/index.html?topic=html/LINQtoJSON.htm" />
public List<Customer> ParseCustomers()
{
if (string.IsNullOrWhiteSpace(Content))
{
return null;
}

// dynamic dynamicJson = JObject.Parse(response.Content);

// foreach (dynamic customer in dynamicJson.value)
// {
// string str = string.Format("{0} {1} {2}", customer.Id, customer.CustomerGuid, customer.Email);
//str.Dump();
// }

var json = JObject.Parse(Content);
string metadata = (string)json["@odata.context"];

if (!string.IsNullOrWhiteSpace(metadata) && metadata.EndsWith("#Customers"))
{
var customers = json["value"].Select(x => x.ToObject<Customer>()).ToList();
return customers;
}

return null;
}
}
}
Loading

0 comments on commit 719e267

Please sign in to comment.