Giter Club home page Giter Club logo

somelyricsapi.net's Introduction

SomeLyricsApi.Net

SomeLyricsApi.Net a .Net standard wrapper for the Some-Random-Api Lyrics Wrapper.

This is just a random project for Day 4 of our Discords challenge series.

How to setup.

//You can choose to either use DI via the extension method provided or simply new up a static SomeLyricsClient
//Using a static client would be like, Please note that this is the intended use of the wrapper but it should work fine.
public static SomeLyricsClient lyricsClient = new SomeLyricsClient();

//The correct way to use this wrapper is with a Dependency Injection setup. I provide an extension method via for Microsofts Dependency Injection.
//You can use that like so... (Note in this example I am making the setup of the DI container I static method to call at the start of my program)
public static class Container
{
  public static IServiceProvider SetupServiceProvider()
    => new ServiceCollection()
          .UseSomeLyricsApi()
          .BuildServiceProvider();
}

//The other way would be to simply add the Interface and Concrete client to your DI container. Like so...
public static class Container
{
  public static IServiceProvider SetupServiceProvider()
    => new ServiceCollection()
          .AddSingleton<ISomeLyricsClient, SomeLyricsClient>()
          .BuildServiceProvider();
}

How to use

Currently the only method of getting lyrics is the base lyrics getter method. You can use it like so:

//With a static client
var search = await lyricsClient.GetBaseLyricsAsync("Search Here");
//Do something with the search like..
Console.WriteLine(search.Lyrics);

Using it with DI, you would ideally use constructor injection like so...

public class LyricsService
{
  private readonly ISomeLyricsClient _client;
  
  public LyricsService(ISomeLyricsClient client)
  {
    _client = client;
  }
  
  public async Task<string> GetLyrics(string search)
  {
    var result = await _client.GetLyricsAsync(search);
    return results.Lyrics;
  }
}

Unit Tests

The Majority of the code is unit tested Here

Please note that it will throw WebExceptions on bad search results as shown here..

public async Task<Lyric> GetBaseLyrics(string song)
{
  try
  {
    var url = new Uri($"https://some-random-api.ml/lyrics?title={song}");
    using (var client = new WebClient())
    {
      var baseJson = await client.DownloadStringTaskAsync(url);
      return JsonConvert.DeserializeObject<Lyric>(baseJson);
    }
  }
  catch (WebException e)
  {
    throw new WebException("Song Not found. Sorry!", e.InnerException);
  }
}

This means you can handle the exception as you see fit.

somelyricsapi.net's People

Contributors

draxcodes avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.