From dd2cd0a851117658af3f2942a050b3e4c74e487f Mon Sep 17 00:00:00 2001 From: Rikki Tooley Date: Tue, 4 Nov 2014 14:38:06 +0000 Subject: [PATCH 1/2] Updating README with more sample code --- .gitignore | 3 ++ README.md | 86 ++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 73 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index df6decd..0696013 100644 --- a/.gitignore +++ b/.gitignore @@ -161,3 +161,6 @@ pip-log.txt # Mac crap .DS_Store + +# ignore html files in the root, all are generated by VS or subl +/*.html diff --git a/README.md b/README.md index 3677d07..03aed98 100644 --- a/README.md +++ b/README.md @@ -4,28 +4,82 @@ MIT licensed. Maintained by [@rikkilt](http://twitter.com/rikkilt). Feature request? Bug? Or just wanna help out? Check out [the issues on GitHub](https://github.com/inflatablefriends/lastfm/issues). -If you have comments or need some help, just post to our chat room on [![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/inflatablefriends/lastfm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) - -## Quickstart - -```c# -var auth = new Auth("apikey", "apisecret"); -var response = await auth.GetSessionTokenAsync("username", "pass"); - -if (response.Success && auth.HasAuthenticated) { - var albumApi = new AlbumApi(auth); - var visions = await albumApi.GetAlbumInfoAsync("Grimes", "Visions"); -} -``` - -Some documentation is available on the [GitHub wiki](https://github.com/rikkit/lastfm-wp/wiki), but hopefully the source is good enough to document itself! +If you have comments or need some help, post to our chat room on [![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/inflatablefriends/lastfm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) ## Project Goals - Provide complete, and completely tested, bindings for the Last.fm REST API for use on modern .NET platforms. -- Provide functionality beyond mere API bindings, to spread the joy of Last.fm to developers everywhere. +- Provide functionality beyond API bindings, to spread the joy of Last.fm to developers everywhere. - To be the very best, like no-one ever was. +## Quickstart +### Acquiring the SDK + +You can either: + +- clone this repo and build from source, referencing IF.Lastfm.Core in your project (requires a version of Visual Studio that supports portable class libraries - i.e. any version of VS 2012/2013 *except* 2012 Express) +- download and reference the .dlls from the [latest release](https://github.com/inflatablefriends/lastfm/releases) + +A NuGet package is coming soon. + +### Using the SDK + +Once IF.Lastfm.Core is referenced, it's pretty simple to get started. First, sign up for Last.fm API access if you haven't already + +This is how to get album info: + +```c# +var auth = new LastAuth("apikey", "apisecret"); +var albumApi = new AlbumApi(auth); // this is an unauthenticated call to the API +var response = await albumApi.GetAlbumInfoAsync("Grimes", "Visions"); +var visions = response.Content; // visions is a LastAlbum +``` + +For methods that return several items, you can simply iterate over the response + +```c# +var pageResponse = await artistApi.GetTopTracksForArtistAsync("Ben Frost", page: 5, itemsPerPage: 100); + +foreach (var wallOfSound in pageResponse) +{ + // wallOfSound is a LastTrack +} +``` + +Several API methods require user authentication. Once you have your user's Last.fm username and password: + +```c# +var auth = new LastAuth("apikey", "apisecret"); + +// wait for authentication +var response = await auth.GetSessionTokenAsync("username", "pass"); + +if (response.Success && auth.HasAuthenticated) { + var trackApi = new TrackApi(auth); + var loved = await trackApi.LoveTrackAsync("CIRCLONT6A [141.98][Syrobonkus mix]", "Aphex Twin"); +} +``` + +Some documentation is available on the [GitHub wiki](https://github.com/rikkit/lastfm-wp/wiki). You can also check the Windows Phone demo project for some example code. + +Any problems, just ask in [Gitter](https://gitter.im/inflatablefriends/lastfm). + +## Dependency Injection + +The SDK is built to work with IoC libraries like MvvmLight and Ninject. To inject an API as a dependency to a viewmodel, you just need to register ```ILastAuth``` to an instance of ```LastAuth```: + +```c# +// mvvmlight +var auth = new LastAuth("apikey", "apisecret"); +SimpleIoc.Default.Register(() => auth); + +// ... + +var artistApi = ServiceLocator.Current.GetInstance(); +var response = await artistApi.GetArtistInfoAsync("The Knife"); +var theKnife = artist.Content; +``` + ## Implemented Features Check the [progress report](https://github.com/inflatablefriends/lastfm/blob/master/PROGRESS.md) for a list of implemented methods. From 63bc96beac85c3dd8edb8fa83880058c41a9010a Mon Sep 17 00:00:00 2001 From: Rikki Tooley Date: Sun, 9 Nov 2014 15:49:15 +0000 Subject: [PATCH 2/2] Don't send the ExpectContinue header on API requests - fixes #36 --- .../Api/Commands/GetAsyncCommandBase.cs | 2 +- .../Api/Commands/LastAsyncCommandBase.cs | 11 +++++++++++ .../Api/Commands/PostAsyncCommandBase.cs | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/IF.Lastfm.Core/Api/Commands/GetAsyncCommandBase.cs b/src/IF.Lastfm.Core/Api/Commands/GetAsyncCommandBase.cs index 2bf25bf..0446ee0 100644 --- a/src/IF.Lastfm.Core/Api/Commands/GetAsyncCommandBase.cs +++ b/src/IF.Lastfm.Core/Api/Commands/GetAsyncCommandBase.cs @@ -24,7 +24,7 @@ public async override Task ExecuteAsync() try { - var httpClient = new HttpClient(); + var httpClient = GetHttpClient(); var response = await httpClient.GetAsync(Url); return await HandleResponse(response); } diff --git a/src/IF.Lastfm.Core/Api/Commands/LastAsyncCommandBase.cs b/src/IF.Lastfm.Core/Api/Commands/LastAsyncCommandBase.cs index b8b3e06..13e1c78 100644 --- a/src/IF.Lastfm.Core/Api/Commands/LastAsyncCommandBase.cs +++ b/src/IF.Lastfm.Core/Api/Commands/LastAsyncCommandBase.cs @@ -54,5 +54,16 @@ protected void DisableCaching() public abstract Task HandleResponse(HttpResponseMessage response); + /// + /// + protected HttpClient GetHttpClient() + { + var client = new HttpClient(); + + // See http://stackoverflow.com/questions/14595021/how-to-disable-the-expect-100-continue-header-in-winrts-httpwebrequest + client.DefaultRequestHeaders.ExpectContinue = false; + + return client; + } } } \ No newline at end of file diff --git a/src/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs b/src/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs index 2ce96ca..01ece22 100644 --- a/src/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs +++ b/src/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs @@ -38,7 +38,7 @@ public override async Task ExecuteAsync() try { - var httpClient = new HttpClient(); + var httpClient = GetHttpClient(); var response = await httpClient.PostAsync(Url, postContent); return await HandleResponse(response); }