https://github.com/timothymakkison/yahoo-finance-api-console-application
A simple Yahoo Finance api console application to learn various libraries.
https://github.com/timothymakkison/yahoo-finance-api-console-application
automapper commandline commandlineparser csharp dependency-injection fluentvalidation mediatr refit
Last synced: 7 months ago
JSON representation
A simple Yahoo Finance api console application to learn various libraries.
- Host: GitHub
- URL: https://github.com/timothymakkison/yahoo-finance-api-console-application
- Owner: TimothyMakkison
- Created: 2021-03-28T20:03:28.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-05-02T13:56:33.000Z (over 3 years ago)
- Last Synced: 2025-01-21T02:12:36.428Z (9 months ago)
- Topics: automapper, commandline, commandlineparser, csharp, dependency-injection, fluentvalidation, mediatr, refit
- Language: C#
- Homepage:
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Yahoo Finance Api Console App
A simple Yahoo Finance api console application to learn various libraries. Contains simple functions to query stock prices and summaries.
# Example Usage
In this example the historic price for Tesla is retrieved.
# Behavior
* When run, the app immediately calls `Startup.BuildService()`. This behaves similarly to an Asp.Net `Startup` call by loading a configuration and then building a 'ServiceCollection' by adding services.
```
services.AddMediatR(typeof(Program));
services.AddAutoMapper(typeof(Program));
services.AddCommandLineParser(typeof(Program));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
services.AddValidatorsFromAssembly(typeof(Program).Assembly);BuildRefitclient(configuration, services);
services.AddSingleton();
```
* In `Program` the service collection is used to build an instance of `CommandLineParser` and args are passed to it.
* `CommandLineParser` parses the args, either printing an error/providing help or constructing an object.
* The object is passed to the `IMediator` instance and sent.
* If the object has a registered handler then it enters the mediator pipeline.
* Inside `AbstractValidator` validators matching the object type are applied, either throwing an error if validation fails or passing it to the handler.
* The corresponding handler constructs an api request and awaits a response.
* If the response is Ok then the response is mapped to a result type and returned.# Libraries used:
* Microsoft.Extensions.Configuration
- Used to load appSettings.json containing the API base address, secrets and host.
* Microsoft.Extensions.DependencyInjection
- Scans assembly for types and creates a service provider.
* CommandLineParser
- Handles parsing input arguments into objects and providing a basic ux - help text, error messages etc.
* Refit
- When configured automatically turns an interface into a REST Api client.
- Used to create IStockApi.
* MediatR
- Mediator pattern library, Mediator.Extensions.Microsoft.DependencyInjection automatically gets all valid handlers.
- Receives request objects and sends them to a valid handler. Handlers send a HTTP request and map the response to a result.
- Supports pipeline behavior to validate the requests before sending it.
* FluentValidation
- By creating a custom IPipelineBehavior ValidationBehavior class we can requests validate being sent through MediatR.
- Validation is performed using FluentValidation in custom classes inheriting from AbstractValidator.
- Validators are scanned for when building services using FluentValidation.DependencyInjection, Validators are then injected into the custom ValidationBehavior class.
* AutoMapper
- Scans the assembly for Profiles containing mapping rules.
- IMapper takes objects and attempts to map into different types.