mirror of
https://github.com/Sarsoo/IF.Lastfm.git
synced 2024-10-17 07:13:09 +01:00
Request builder
This commit is contained in:
parent
55decdda69
commit
997f3da9d1
@ -12,19 +12,19 @@ namespace IF.Lastfm.Core.Api.Commands
|
||||
public abstract class LastAsyncCommandBase
|
||||
{
|
||||
public string Method { get; protected set; }
|
||||
|
||||
public Dictionary<string, string> Parameters { get; set; }
|
||||
}
|
||||
|
||||
public abstract class LastAsyncCommandBase<T> : LastAsyncCommandBase, IAsyncCommand<T> where T : LastResponse, new()
|
||||
{
|
||||
public Uri Url { get; protected set; }
|
||||
|
||||
public ILastAuth Auth { get; protected set; }
|
||||
|
||||
public int Page { get; set; }
|
||||
|
||||
public int Count { get; set; }
|
||||
|
||||
public Dictionary<string, string> Parameters { get; set; }
|
||||
public Uri Url { get; protected set; }
|
||||
|
||||
protected LastAsyncCommandBase()
|
||||
{
|
||||
|
@ -23,12 +23,7 @@ public override async Task<T> ExecuteAsync()
|
||||
SetParameters();
|
||||
|
||||
Url = BuildRequestUrl();
|
||||
|
||||
if (Auth.Authenticated)
|
||||
{
|
||||
Parameters.Add("sk", Auth.UserSession.Token);
|
||||
}
|
||||
|
||||
|
||||
var apisig = Auth.GenerateMethodSignature(Method, Parameters);
|
||||
|
||||
var postContent = LastFm.CreatePostBody(Method,
|
||||
|
@ -59,6 +59,10 @@ public string GenerateMethodSignature(string method, Dictionary<string, string>
|
||||
|
||||
parameters.Add("api_key", ApiKey);
|
||||
parameters.Add("method", method);
|
||||
if (Authenticated)
|
||||
{
|
||||
parameters.Add("sk", UserSession.Token);
|
||||
}
|
||||
|
||||
var builder = new StringBuilder();
|
||||
|
||||
|
@ -9,7 +9,7 @@
|
||||
using System.Text;
|
||||
|
||||
[assembly: InternalsVisibleTo("IF.Lastfm.Core.Tests")]
|
||||
[assembly: InternalsVisibleTo("IF.Lastfm.ProgressReport")]
|
||||
[assembly: InternalsVisibleTo("IF.Lastfm.Syro")]
|
||||
namespace IF.Lastfm.Core
|
||||
{
|
||||
public class LastFm
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
namespace IF.Lastfm.Core.Objects
|
||||
{
|
||||
public class Shout
|
||||
public class Shout : ILastfmObject
|
||||
{
|
||||
#region Properties
|
||||
|
||||
|
@ -249,7 +249,6 @@
|
||||
-->
|
||||
<ProjectExtensions />
|
||||
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
|
||||
<Import Project="$(XamlSpyInstallPath)MSBuild\FirstFloor.XamlSpy.WindowsPhone.targets" Condition="'$(XamlSpyInstallPath)' != '' and '$(Configuration)' == 'DEBUG'" />
|
||||
<Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
|
||||
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
|
||||
<Error Condition="!Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
|
||||
|
@ -2,7 +2,7 @@
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace IF.Common.Metro.Mvvm
|
||||
namespace IF.Lastfm.Syro.Helpers
|
||||
{
|
||||
public class AsyncDelegateCommand : ICommand
|
||||
{
|
||||
|
@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace IF.Common.Metro.Mvvm
|
||||
namespace IF.Lastfm.Syro.Helpers
|
||||
{
|
||||
public class DelegateCommand : ICommand
|
||||
{
|
||||
|
31
src/IF.Lastfm.Syro/Helpers/DummyGetAsyncCommand.cs
Normal file
31
src/IF.Lastfm.Syro/Helpers/DummyGetAsyncCommand.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using IF.Lastfm.Core.Api;
|
||||
using IF.Lastfm.Core.Api.Commands;
|
||||
using IF.Lastfm.Core.Api.Helpers;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IF.Lastfm.Syro.Helpers
|
||||
{
|
||||
public class DummyGetAsyncCommand<T> : GetAsyncCommandBase<T>, IDummyCommand where T : LastResponse, new()
|
||||
{
|
||||
public JObject Response { get; set; }
|
||||
|
||||
public DummyGetAsyncCommand(ILastAuth auth)
|
||||
: base(auth)
|
||||
{
|
||||
}
|
||||
|
||||
public override void SetParameters()
|
||||
{
|
||||
}
|
||||
|
||||
public async override Task<T> HandleResponse(HttpResponseMessage response)
|
||||
{
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
Response = JObject.Parse(json);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
31
src/IF.Lastfm.Syro/Helpers/DummyPostAsyncCommand.cs
Normal file
31
src/IF.Lastfm.Syro/Helpers/DummyPostAsyncCommand.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using IF.Lastfm.Core.Api;
|
||||
using IF.Lastfm.Core.Api.Commands;
|
||||
using IF.Lastfm.Core.Api.Helpers;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace IF.Lastfm.Syro.Helpers
|
||||
{
|
||||
public class DummyPostAsyncCommand<T> : PostAsyncCommandBase<T>, IDummyCommand where T : LastResponse, new()
|
||||
{
|
||||
public JObject Response { get; private set; }
|
||||
public DummyPostAsyncCommand(ILastAuth auth) : base(auth)
|
||||
{
|
||||
}
|
||||
|
||||
public override void SetParameters()
|
||||
{
|
||||
}
|
||||
|
||||
public async override Task<T> HandleResponse(HttpResponseMessage response)
|
||||
{
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
Response = JObject.Parse(json);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
9
src/IF.Lastfm.Syro/Helpers/IDummyCommand.cs
Normal file
9
src/IF.Lastfm.Syro/Helpers/IDummyCommand.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace IF.Lastfm.Syro.Helpers
|
||||
{
|
||||
public interface IDummyCommand
|
||||
{
|
||||
JObject Response { get; }
|
||||
}
|
||||
}
|
57
src/IF.Lastfm.Syro/Helpers/Pair.cs
Normal file
57
src/IF.Lastfm.Syro/Helpers/Pair.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace IF.Lastfm.Syro.Helpers
|
||||
{
|
||||
public class Pair<TK,TV> : INotifyPropertyChanged
|
||||
{
|
||||
private TK _key;
|
||||
private TV _value;
|
||||
|
||||
public TK Key
|
||||
{
|
||||
get { return _key; }
|
||||
set
|
||||
{
|
||||
if (Equals(_key, value)) return;
|
||||
_key = value;
|
||||
NotifyPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public TV Value
|
||||
{
|
||||
get { return _value; }
|
||||
set
|
||||
{
|
||||
if (Equals(_value, value)) return;
|
||||
_value = value;
|
||||
NotifyPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public Pair()
|
||||
{
|
||||
}
|
||||
|
||||
public Pair(TK key, TV value)
|
||||
: this()
|
||||
{
|
||||
Key = key;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
#region INotifyPropertyChanged Members
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
private void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
|
||||
{
|
||||
var handler = PropertyChanged;
|
||||
if (handler != null)
|
||||
handler(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -56,6 +56,10 @@
|
||||
<Reference Include="Microsoft.Practices.ServiceLocation">
|
||||
<HintPath>..\..\packages\CommonServiceLocator.1.3\lib\portable-net4+sl5+netcore45+wpa81+wp8\Microsoft.Practices.ServiceLocation.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\packages\Newtonsoft.Json.6.0.5\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
@ -87,11 +91,16 @@
|
||||
<SubType>Designer</SubType>
|
||||
</ApplicationDefinition>
|
||||
<Compile Include="Helpers\AsyncDelegateCommand.cs" />
|
||||
<Compile Include="Helpers\DummyGetAsyncCommand.cs" />
|
||||
<Compile Include="Helpers\DummyPostAsyncCommand.cs" />
|
||||
<Compile Include="Helpers\IDummyCommand.cs" />
|
||||
<Compile Include="Helpers\Pair.cs" />
|
||||
<Compile Include="Helpers\DelegateCommand.cs" />
|
||||
<Compile Include="Properties\Annotations.cs" />
|
||||
<Compile Include="Resources\BoolFlipConverter.cs" />
|
||||
<Compile Include="Resources\NullToInvisibleConverter.cs" />
|
||||
<Compile Include="Tools\Program.cs" />
|
||||
<Compile Include="Tools\ProgressReport.cs" />
|
||||
<Compile Include="Tools\Reflektor.cs" />
|
||||
<Compile Include="ViewModels\MainViewModel.cs" />
|
||||
<Compile Include="ViewModels\ViewModelBase.cs" />
|
||||
<Compile Include="ViewModels\ViewModelLocator.cs" />
|
||||
|
@ -3,7 +3,7 @@
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
|
||||
Height="350"
|
||||
MinHeight="300"
|
||||
MinHeight="500"
|
||||
MinWidth="400"
|
||||
Width="525"
|
||||
Foreground="#111111"
|
||||
|
@ -1,20 +1,140 @@
|
||||
<Page
|
||||
x:Class="IF.Lastfm.Syro.Pages.MainPage"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
|
||||
mc:Ignorable="d"
|
||||
d:DataContext="{Binding Source=MainViewModel}">
|
||||
|
||||
<Page x:Class="IF.Lastfm.Syro.Pages.MainPage"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
|
||||
xmlns:viewModels="clr-namespace:IF.Lastfm.Syro.ViewModels"
|
||||
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
|
||||
xmlns:objectModel="clr-namespace:System.Collections.ObjectModel;assembly=System"
|
||||
mc:Ignorable="d"
|
||||
d:DataContext="{d:DesignInstance viewModels:MainViewModel}" d:DesignWidth="400" d:DesignHeight="308">
|
||||
|
||||
<Grid>
|
||||
<TabControl Style="{StaticResource SyroTabControlStyle}">
|
||||
<TabItem Header="Request Builder">
|
||||
<Border Padding="28,10">
|
||||
<Border Padding="28,10,28,0">
|
||||
<Grid>
|
||||
<TextBlock Style="{StaticResource SyroNormalFontStyle}"
|
||||
Foreground="{StaticResource GrayNormalBrush}">Coming soon...</TextBlock>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="*"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Grid Margin="0,0,0,10">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<ComboBox Grid.Column="0"
|
||||
ItemsSource="{Binding BaseCommandTypes}"
|
||||
SelectedItem="{Binding SelectedBaseCommandType, Mode=TwoWay}"
|
||||
IsTextSearchEnabled="False" />
|
||||
|
||||
<ComboBox Grid.Column="1"
|
||||
ItemsSource="{Binding LastResponseTypes}"
|
||||
SelectedItem="{Binding SelectedResponseType, Mode=TwoWay}"
|
||||
IsTextSearchEnabled="False" />
|
||||
|
||||
<ComboBox Grid.Column="2"
|
||||
ItemsSource="{Binding LastObjectTypes}"
|
||||
SelectedItem="{Binding SelectedLastObjectType, Mode=TwoWay}"
|
||||
IsTextSearchEnabled="False" />
|
||||
</Grid>
|
||||
<Grid Row="1"
|
||||
Margin="0,0,0,10">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="2*"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Grid Grid.Column="0">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<TextBlock Text="Method"
|
||||
Style="{StaticResource SyroNormalFontStyle}"
|
||||
Margin="0,0,10,0"/>
|
||||
<TextBox Grid.Column="1"
|
||||
Text="{Binding CommandMethodName, Mode=TwoWay}"/>
|
||||
</Grid>
|
||||
|
||||
<Grid Grid.Column="1">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<TextBlock Text="Page"
|
||||
Style="{StaticResource SyroNormalFontStyle}"
|
||||
Margin="10,0"/>
|
||||
<TextBox Grid.Column="1"
|
||||
Text="{Binding CommandPageNumber, Mode=TwoWay}"/>
|
||||
</Grid>
|
||||
|
||||
<Grid Grid.Column="2">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<TextBlock Text="Count"
|
||||
Style="{StaticResource SyroNormalFontStyle}"
|
||||
Margin="10,0"/>
|
||||
<TextBox Grid.Column="1"
|
||||
Text="{Binding CommandItemCount, Mode=TwoWay}"/>
|
||||
</Grid>
|
||||
</Grid>
|
||||
|
||||
<StackPanel Grid.Row="2"
|
||||
Margin="0,10,0,0">
|
||||
<DataGrid ItemsSource="{Binding CommandParameters}"
|
||||
AutoGenerateColumns="False"
|
||||
CanUserAddRows="True">
|
||||
<DataGrid.Columns >
|
||||
<DataGridTemplateColumn Header="Key" Width="*">
|
||||
<DataGridTemplateColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Style="{StaticResource SyroNormalFontStyle}"
|
||||
Text="{Binding Key}" />
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellTemplate>
|
||||
<DataGridTemplateColumn.CellEditingTemplate>
|
||||
<DataTemplate>
|
||||
<TextBox Text="{Binding Key}" />
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellEditingTemplate>
|
||||
</DataGridTemplateColumn>
|
||||
<DataGridTemplateColumn Header="Value" Width="*">
|
||||
<DataGridTemplateColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Style="{StaticResource SyroNormalFontStyle}"
|
||||
Text="{Binding Value}" />
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellTemplate>
|
||||
<DataGridTemplateColumn.CellEditingTemplate>
|
||||
<DataTemplate>
|
||||
<TextBox Text="{Binding Value}" />
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellEditingTemplate>
|
||||
</DataGridTemplateColumn>
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
<Button Command="{Binding ExecuteSelectedCommandCommand}"
|
||||
HorizontalAlignment="Left"
|
||||
IsEnabled="{Binding ExecutingCommand, Converter={StaticResource BoolFlipConverter}}"
|
||||
Margin="0,10,0,0">
|
||||
<TextBlock Style="{StaticResource SyroNormalFontStyle}"
|
||||
Margin="10,5">
|
||||
Execute command and open result
|
||||
</TextBlock>
|
||||
</Button>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Border>
|
||||
</TabItem>
|
||||
@ -23,36 +143,42 @@
|
||||
<StackPanel>
|
||||
<TextBlock Style="{StaticResource SyroNormalFontStyle}"
|
||||
Text="Solution Directory"
|
||||
Margin="0,0,0,10"/>
|
||||
Margin="0,0,0,10" />
|
||||
<TextBox Text="{Binding SolutionDir, Mode=TwoWay}"
|
||||
Margin="0,0,0,15"/>
|
||||
Margin="0,0,0,15" />
|
||||
<StackPanel Orientation="Horizontal"
|
||||
Margin="0,0,0,15">
|
||||
<Button Command="{Binding GenerateProgressReportCommand}"
|
||||
IsEnabled="{Binding GeneratingProgressReport, Converter={StaticResource BoolFlipConverter}}"
|
||||
Margin="0,0,10,0">
|
||||
<TextBlock Style="{StaticResource SyroNormalFontStyle}"
|
||||
Margin="10,5">Generate report</TextBlock>
|
||||
Margin="10,5">
|
||||
Generate report
|
||||
</TextBlock>
|
||||
</Button>
|
||||
<controls:ProgressRing IsActive="{Binding GeneratingProgressReport}"
|
||||
Width="30"
|
||||
Height="30"/>
|
||||
Height="30" />
|
||||
</StackPanel>
|
||||
<StackPanel Visibility="{Binding RemainingCommands, Converter={StaticResource NullToInvisibleConverter}}">
|
||||
<StackPanel
|
||||
Visibility="{Binding RemainingCommands, Converter={StaticResource NullToInvisibleConverter}}">
|
||||
<TextBlock Style="{StaticResource SyroNormalFontStyle}">
|
||||
<Run Text="{Binding ApiProgress}"/>% complete, <Run Text="{Binding RemainingCommands.Count, Mode=OneWay}"/> commands left
|
||||
<Run Text="{Binding ApiProgress}" />% complete,
|
||||
<Run Text="{Binding RemainingCommands.Count, Mode=OneWay}" /> commands left
|
||||
</TextBlock>
|
||||
<Button Command="{Binding OpenReportCommand}"
|
||||
HorizontalAlignment="Left"
|
||||
IsEnabled="{Binding GeneratingProgressReport, Converter={StaticResource BoolFlipConverter}}"
|
||||
Margin="0,10,0,0">
|
||||
<TextBlock Style="{StaticResource SyroNormalFontStyle}"
|
||||
Margin="10,5">Open PROGRESS.md</TextBlock>
|
||||
</Button>
|
||||
<TextBlock Style="{StaticResource SyroNormalFontStyle}"
|
||||
Margin="10,5">
|
||||
Open PROGRESS.md
|
||||
</TextBlock>
|
||||
</Button>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</Border>
|
||||
</TabItem>
|
||||
</TabControl>
|
||||
</Grid>
|
||||
</Page>
|
||||
</Page>
|
@ -1,4 +1,5 @@
|
||||
using System.Windows.Controls;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using IF.Lastfm.Syro.ViewModels;
|
||||
|
||||
namespace IF.Lastfm.Syro.Pages
|
||||
|
@ -3,10 +3,8 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using HtmlAgilityPack;
|
||||
using IF.Lastfm.Core.Api.Commands;
|
||||
|
||||
namespace IF.Lastfm.Syro.Tools
|
||||
{
|
||||
@ -86,50 +84,7 @@ private static bool HasClass(HtmlNode stay, string classy)
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Reflect
|
||||
|
||||
/// <summary>
|
||||
/// With thanks to Tim Murphy
|
||||
/// http://stackoverflow.com/a/4529684/268555
|
||||
/// </summary>
|
||||
public static IEnumerable<Type> FindSubClassesOf(Type type)
|
||||
{
|
||||
var typeInfo = type.GetTypeInfo();
|
||||
var assembly = typeInfo.Assembly;
|
||||
return assembly.DefinedTypes.Where(t => t.IsSubclassOf(type)).Select(t => t.AsType());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reflect on implemented commands
|
||||
/// </summary>
|
||||
internal static IEnumerable<string> GetImplementedCommands()
|
||||
{
|
||||
var types = FindSubClassesOf(typeof(LastAsyncCommandBase))
|
||||
.Select(t => t.GetTypeInfo())
|
||||
.Where(t => t.IsClass && !t.IsAbstract)
|
||||
.Select(t => t.AsType());
|
||||
var methods = types.Select(GetApiMethodFromCommandType);
|
||||
|
||||
return methods;
|
||||
}
|
||||
|
||||
private static string GetApiMethodFromCommandType(Type type)
|
||||
{
|
||||
var typeInfo = type.GetTypeInfo();
|
||||
|
||||
// assuming there is only one constructor
|
||||
var constructor = typeInfo.DeclaredConstructors.First();
|
||||
var parameters = constructor.GetParameters();
|
||||
var arguments = new object[parameters.Count()]; // to keep reflection happy
|
||||
|
||||
var instance = (LastAsyncCommandBase)Activator.CreateInstance(type, arguments);
|
||||
|
||||
return instance.Method;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Report
|
||||
|
||||
internal static void WriteReport(Dictionary<string, IEnumerable<string>> apiGroup, List<string> allImplemented, string path)
|
49
src/IF.Lastfm.Syro/Tools/Reflektor.cs
Normal file
49
src/IF.Lastfm.Syro/Tools/Reflektor.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using IF.Lastfm.Core.Api.Commands;
|
||||
|
||||
namespace IF.Lastfm.Syro.Tools
|
||||
{
|
||||
public class Reflektor
|
||||
{
|
||||
/// <summary>
|
||||
/// With thanks to Tim Murphy
|
||||
/// http://stackoverflow.com/a/4529684/268555
|
||||
/// </summary>
|
||||
public static IEnumerable<Type> FindClassesCastableTo(Type type)
|
||||
{
|
||||
var typeInfo = type.GetTypeInfo();
|
||||
var assembly = typeInfo.Assembly;
|
||||
return assembly.DefinedTypes.Where(t => type.IsAssignableFrom(t) && t != type).Select(t => t.AsType());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reflect on implemented commands
|
||||
/// </summary>
|
||||
internal static IEnumerable<Type> GetImplementedCommands()
|
||||
{
|
||||
var types = FindClassesCastableTo(typeof(LastAsyncCommandBase))
|
||||
.Select(t => t.GetTypeInfo())
|
||||
.Where(t => t.IsClass && !t.IsAbstract)
|
||||
.Select(t => t.AsType());
|
||||
|
||||
return types;
|
||||
}
|
||||
|
||||
public static LastAsyncCommandBase CreateCommand(Type type)
|
||||
{
|
||||
var typeInfo = type.GetTypeInfo();
|
||||
|
||||
// assuming there is only one constructor
|
||||
var constructor = typeInfo.DeclaredConstructors.First();
|
||||
var parameters = constructor.GetParameters();
|
||||
var arguments = new object[parameters.Count()]; // to keep reflection happy
|
||||
|
||||
var instance = (LastAsyncCommandBase)Activator.CreateInstance(type, arguments);
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +1,23 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using IF.Common.Metro.Mvvm;
|
||||
using System.Reflection;
|
||||
using System.Security.AccessControl;
|
||||
using IF.Lastfm.Core;
|
||||
using IF.Lastfm.Core.Api;
|
||||
using IF.Lastfm.Core.Api.Commands;
|
||||
using IF.Lastfm.Core.Api.Commands.UserApi;
|
||||
using IF.Lastfm.Core.Api.Helpers;
|
||||
using IF.Lastfm.Core.Objects;
|
||||
using IF.Lastfm.Syro.Helpers;
|
||||
using IF.Lastfm.Syro.Tools;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace IF.Lastfm.Syro.ViewModels
|
||||
{
|
||||
@ -18,8 +28,35 @@ internal class MainViewModel : ViewModelBase
|
||||
private List<string> _remainingCommands;
|
||||
private int _apiProgress;
|
||||
private string _reportPath;
|
||||
private Type _selectedCommandType;
|
||||
private ObservableCollection<Pair<string, string>> _commandParameters;
|
||||
private Type _selectedLastObjectType;
|
||||
private bool _executingCommand;
|
||||
private Type _selectedResponseType;
|
||||
private ILastAuth _lastAuth;
|
||||
private string _commandResult;
|
||||
private string _commandMethodName;
|
||||
private string _commandPageNumber;
|
||||
private string _commandItemCount;
|
||||
private const string ReportFilename = "PROGRESS.md";
|
||||
|
||||
#region Binding properties
|
||||
|
||||
public ICommand GenerateProgressReportCommand { get; private set; }
|
||||
public ICommand OpenReportCommand { get; private set; }
|
||||
public ICommand ExecuteSelectedCommandCommand { get; private set; }
|
||||
|
||||
public string CommandResult
|
||||
{
|
||||
get { return _commandResult; }
|
||||
set
|
||||
{
|
||||
if (value == _commandResult) return;
|
||||
_commandResult = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public bool GeneratingProgressReport
|
||||
{
|
||||
get { return _generatingProgressReport; }
|
||||
@ -64,18 +101,236 @@ public List<string> RemainingCommands
|
||||
}
|
||||
}
|
||||
|
||||
public ICommand GenerateProgressReportCommand { get; private set; }
|
||||
public ICommand OpenReportCommand { get; private set; }
|
||||
public IEnumerable<Type> BaseCommandTypes { get; private set; }
|
||||
|
||||
public MainViewModel()
|
||||
public Type SelectedBaseCommandType
|
||||
{
|
||||
get { return _selectedCommandType; }
|
||||
set
|
||||
{
|
||||
if (value == _selectedCommandType) return;
|
||||
|
||||
_selectedCommandType = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Type> LastObjectTypes { get; private set; }
|
||||
|
||||
public Type SelectedLastObjectType
|
||||
{
|
||||
get { return _selectedLastObjectType; }
|
||||
set
|
||||
{
|
||||
if (value == _selectedLastObjectType) return;
|
||||
_selectedLastObjectType = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Type> LastResponseTypes { get; private set; }
|
||||
|
||||
public Type SelectedResponseType
|
||||
{
|
||||
get { return _selectedResponseType; }
|
||||
set
|
||||
{
|
||||
if (value == _selectedResponseType) return;
|
||||
_selectedResponseType = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollection<Pair<string, string>> CommandParameters
|
||||
{
|
||||
get { return _commandParameters; }
|
||||
set
|
||||
{
|
||||
if (Equals(value, _commandParameters)) return;
|
||||
_commandParameters = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public bool ExecutingCommand
|
||||
{
|
||||
get { return _executingCommand; }
|
||||
set
|
||||
{
|
||||
if (value.Equals(_executingCommand)) return;
|
||||
_executingCommand = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public string CommandMethodName
|
||||
{
|
||||
get { return _commandMethodName; }
|
||||
set
|
||||
{
|
||||
if (value == _commandMethodName) return;
|
||||
_commandMethodName = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public string CommandPageNumber
|
||||
{
|
||||
get { return _commandPageNumber; }
|
||||
set
|
||||
{
|
||||
if (value == _commandPageNumber) return;
|
||||
_commandPageNumber = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public string CommandItemCount
|
||||
{
|
||||
get { return _commandItemCount; }
|
||||
set
|
||||
{
|
||||
if (value == _commandItemCount) return;
|
||||
_commandItemCount = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public MainViewModel(ILastAuth lastAuth)
|
||||
{
|
||||
_lastAuth = lastAuth;
|
||||
|
||||
GenerateProgressReportCommand = new AsyncDelegateCommand(GenerateProgressReport);
|
||||
OpenReportCommand = new DelegateCommand(OpenProgressReport);
|
||||
ExecuteSelectedCommandCommand = new AsyncDelegateCommand(ExecuteSelectedCommand);
|
||||
|
||||
var currentDir = AppDomain.CurrentDomain.BaseDirectory;
|
||||
SolutionDir = Path.GetFullPath(currentDir + "../../../../"); // assuming this is running in debug dir
|
||||
|
||||
BaseCommandTypes = new List<Type>
|
||||
{
|
||||
typeof(DummyGetAsyncCommand<>),
|
||||
typeof(DummyPostAsyncCommand<>)
|
||||
};
|
||||
LastObjectTypes = Reflektor.FindClassesCastableTo(typeof (ILastfmObject));
|
||||
LastResponseTypes = Reflektor.FindClassesCastableTo(typeof (LastResponse));
|
||||
|
||||
SelectedBaseCommandType = BaseCommandTypes.FirstOrDefault();
|
||||
SelectedLastObjectType = LastObjectTypes.FirstOrDefault();
|
||||
SelectedResponseType = LastResponseTypes.FirstOrDefault();
|
||||
|
||||
CommandParameters = new ObservableCollection<Pair<string, string>>(new List<Pair<string, string>>
|
||||
{
|
||||
new Pair<string, string>(),
|
||||
new Pair<string, string>(),
|
||||
new Pair<string, string>(),
|
||||
new Pair<string, string>(),
|
||||
new Pair<string, string>()
|
||||
});
|
||||
|
||||
CommandMethodName = "album.getInfo";
|
||||
CommandPageNumber = "0";
|
||||
CommandItemCount = "20";
|
||||
}
|
||||
|
||||
|
||||
private async Task ExecuteSelectedCommand()
|
||||
{
|
||||
if (ExecutingCommand)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ExecutingCommand = true;
|
||||
|
||||
try
|
||||
{
|
||||
// build up the command<response<lastobject>>
|
||||
var responseType = SelectedResponseType.MakeGenericType(SelectedLastObjectType);
|
||||
var genericType = SelectedBaseCommandType.MakeGenericType(responseType);
|
||||
|
||||
if (!_lastAuth.Authenticated)
|
||||
{
|
||||
await _lastAuth.GetSessionTokenAsync("tehrikkit", "#facedusk17a");
|
||||
}
|
||||
|
||||
var instance = Activator.CreateInstance(genericType, _lastAuth);
|
||||
|
||||
var parameters = CommandParameters
|
||||
.Where(pair => !string.IsNullOrWhiteSpace(pair.Key) && !string.IsNullOrWhiteSpace(pair.Value))
|
||||
.ToDictionary(pair => pair.Key, pair => pair.Value);
|
||||
|
||||
var methodProperty = genericType.GetProperty("Method", BindingFlags.Public | BindingFlags.Instance);
|
||||
methodProperty.SetValue(instance, CommandMethodName);
|
||||
|
||||
if (SelectedResponseType == typeof(PageResponse<>))
|
||||
{
|
||||
var pageProperty = genericType.GetProperty("Page", BindingFlags.Public | BindingFlags.Instance);
|
||||
pageProperty.SetValue(instance, int.Parse(CommandPageNumber));
|
||||
|
||||
var countProperty = genericType.GetProperty("Count", BindingFlags.Public | BindingFlags.Instance);
|
||||
countProperty.SetValue(instance, int.Parse(CommandItemCount));
|
||||
|
||||
var addPageParamsMethod = genericType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).First(m => m.Name == "AddPagingParameters");
|
||||
addPageParamsMethod.Invoke(instance, null);
|
||||
}
|
||||
|
||||
var parametersProperty = genericType.GetProperty("Parameters",
|
||||
BindingFlags.Public | BindingFlags.Instance);
|
||||
parametersProperty.SetValue(instance, parameters);
|
||||
|
||||
//test
|
||||
var command = new GetRecentStationsCommand(_lastAuth, "tehrikkit")
|
||||
{
|
||||
Count = 5,
|
||||
Page = 1
|
||||
};
|
||||
await command.ExecuteAsync();
|
||||
|
||||
// execute
|
||||
var executeMethod = genericType.GetMethods().First(m => m.Name == "ExecuteAsync");
|
||||
await (dynamic) executeMethod.Invoke(instance, null);
|
||||
|
||||
|
||||
|
||||
// cast so we can get the Json response
|
||||
var dummyCommand = (IDummyCommand) instance;
|
||||
var jo = dummyCommand.Response;
|
||||
|
||||
var formattedJson = jo.ToString(Formatting.Indented);
|
||||
|
||||
// writeout to file
|
||||
var filename = string.Format("syro-{0}-{1}.json", jo.Properties().First().Name,
|
||||
DateTime.Now.ToString("yyMMdd-HHmmss"));
|
||||
var tempDirPath = Path.GetFullPath(SolutionDir + "tmp/");
|
||||
|
||||
if (!Directory.Exists(tempDirPath))
|
||||
{
|
||||
Directory.CreateDirectory(tempDirPath);
|
||||
}
|
||||
var path = Path.GetFullPath(tempDirPath + filename);
|
||||
|
||||
// write to output directory and launch
|
||||
using (var fs = new FileStream(path, FileMode.Create))
|
||||
{
|
||||
using (var sw = new StreamWriter(fs))
|
||||
{
|
||||
sw.Write(formattedJson);
|
||||
}
|
||||
}
|
||||
Process.Start(path);
|
||||
|
||||
CommandResult = formattedJson;
|
||||
}
|
||||
finally
|
||||
{
|
||||
ExecutingCommand = false;
|
||||
}
|
||||
}
|
||||
|
||||
#region Progress report
|
||||
|
||||
private async Task GenerateProgressReport()
|
||||
{
|
||||
if (GeneratingProgressReport)
|
||||
@ -95,7 +350,7 @@ await Task.Run(() =>
|
||||
}
|
||||
|
||||
// reflect on Last.fm assembly to find all implemented commands
|
||||
var allImplemented = ProgressReport.GetImplementedCommands().ToList();
|
||||
var allImplemented = Reflektor.GetImplementedCommands().Select(Reflektor.CreateCommand).Select(c => c.Method).ToList();
|
||||
|
||||
// generate the markdown
|
||||
_reportPath = Path.GetFullPath(SolutionDir + ReportFilename);
|
||||
@ -118,11 +373,14 @@ await Task.Run(() =>
|
||||
|
||||
GeneratingProgressReport = false;
|
||||
}
|
||||
|
||||
private void OpenProgressReport()
|
||||
{
|
||||
var path = _reportPath ?? Path.GetFullPath(SolutionDir + ReportFilename);
|
||||
|
||||
Process.Start(path);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using GalaSoft.MvvmLight.Ioc;
|
||||
using IF.Lastfm.Core.Api;
|
||||
using Microsoft.Practices.ServiceLocation;
|
||||
|
||||
namespace IF.Lastfm.Syro.ViewModels
|
||||
@ -15,7 +16,9 @@ public class ViewModelLocator
|
||||
public ViewModelLocator()
|
||||
{
|
||||
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
|
||||
|
||||
|
||||
var auth = new LastAuth("a6ab4b9376e54cdb06912bfbd9c1f288", "3aa7202fd1bc6d5a7ac733246cbccc4b");
|
||||
SimpleIoc.Default.Register<ILastAuth>(() => auth);
|
||||
SimpleIoc.Default.Register<MainViewModel>();
|
||||
}
|
||||
|
||||
@ -23,10 +26,5 @@ public T Get<T>()
|
||||
{
|
||||
return ServiceLocator.Current.GetInstance<T>();
|
||||
}
|
||||
|
||||
public static void Cleanup()
|
||||
{
|
||||
// TODO Clear the ViewModels
|
||||
}
|
||||
}
|
||||
}
|
@ -8,4 +8,5 @@
|
||||
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net451" />
|
||||
<package id="Microsoft.Net.Http" version="2.2.28" targetFramework="net451" />
|
||||
<package id="MvvmLightLibs" version="5.0.2.0" targetFramework="net45" />
|
||||
<package id="Newtonsoft.Json" version="6.0.5" targetFramework="net451" />
|
||||
</packages>
|
Loading…
Reference in New Issue
Block a user