Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Christian Findlay MelbourneDeveloper

🏠
Working from home
View GitHub Profile
View CreateClient.cs
var client = new Client(
new NewtonsoftSerializationAdapter(),
baseUrl: testServerBaseUri,
defaultRequestHeaders:
HeadersExtensions
.FromJsonContentType()
//Non-destructive mutation to create a new headers collection
.WithBearerTokenAuthentication(bearerToken));
View Url.fs
[<TestMethod>]
member this.TestComposition () =
let uri =
"host.com".ToHttpUrlFromHost(5000)
.AddQueryParameter("fieldname1", "field<>Value1")
.WithCredentials("username", "password")
.AddQueryParameter("FieldName2", "field<>Value2")
.WithFragment("frag")
.WithPath("pathpart1", "pathpart2")
@MelbourneDeveloper
MelbourneDeveloper / DI.cs
Created May 26, 2021
ASP .NET Core Dependency Injection (RestClient.Net)
View DI.cs
var serviceCollection = new ServiceCollection()
//Add a service which has an IClient dependency
.AddSingleton<IGetString, GetString1>()
//Add RestClient.Net with a default Base Url of http://www.test.com
.AddRestClient((o) => o.BaseUrl = "http://www.test.com".ToAbsoluteUrl());
//Use HttpClient dependency injection
_ = serviceCollection.AddHttpClient();
View IClient.cs
/// <summary>
/// Dependency Injection abstraction for Rest Clients. Use the CreateClient delegate to create an IClient when more than one is needed for an application.
/// </summary>
public interface IClient
{
/// <summary>
/// Sends a strongly typed request to the server and waits for a strongly typed response
/// </summary>
/// <typeparam name="TResponseBody">The expected type of the response body</typeparam>
/// <param name="request">The request that will be translated to a HTTP request</param>
View QueryParameter.cs
using System.Net;
namespace Urls
{
public record QueryParameter
{
private string? fieldValue;
public string FieldName { get; init; }
public string? Value
View StringValidator.cs
using System;
namespace ConsoleApp25
{
class Program
{
static void Main(string[] args)
{
//This throws an exception from the constructor
//var stringValidator = new StringValidator(null);
@MelbourneDeveloper
MelbourneDeveloper / StringValidator.cs
Last active Apr 28, 2021
String Validator Record
View StringValidator.cs
public record StringValidator
{
public string InputString { get; }
public StringValidator(string inputString)
{
if (string.IsNullOrEmpty(inputString)) throw new ArgumentNullException(nameof(inputString));
InputString = inputString;
}
View StringValidator.cs
public record StringValidator(string InputString);
View StringValidator.cs
public class StringValidator
{
public string InputString { get; }
public StringValidator(string inputString)
{
if (string.IsNullOrEmpty(inputString)) throw new ArgumentNullException(nameof(inputString));
InputString = inputString;
}