Skip to content

Instantly share code, notes, and snippets.

View Chibuikekenneth's full-sized avatar
💥
Stabilizing my DSAS

Chibuike Kenneth Chibuikekenneth

💥
Stabilizing my DSAS
View GitHub Profile
/// <summary>
/// Post for a REST api
/// </summary>
/// <param name="token">authorization token</param>
/// <param name="url">REST url for the resource</param>
/// <param name="content">content</param>
/// <returns>response from the rest url</returns>
public async Task<JObject> PostAsync(string token, string url, string content)
{
byte[] data = Encoding.UTF8.GetBytes(content);
@Chibuikekenneth
Chibuikekenneth / webrequest.cs
Last active October 29, 2020 14:55
webRequest definitions
byte[] data = Encoding.UTF8.GetBytes(content);
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer " + token);
request.ContentLength = data.Length;
@Chibuikekenneth
Chibuikekenneth / webrequest-try-catch.cs
Last active October 29, 2020 14:54
webrequest-try-catch
try
{
WebResponse response = await request.GetResponseAsync();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string responseContent = reader.ReadToEnd();
JObject adResponse =
Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(responseContent);
return adResponse;
}
public class School
{
private readonly ISchool _school;
public School(ISchool school)
{
this._school = school;
}
public string AttendClass(string student)
public interface ISchool
{
public string Learn(string student);
}
public class HavardUniversity : ISchool
{
public string Learn(string student)
{
return string.Format($"My name is {student}, and I attend Havard");
}
}
public class OxfordUniversity : ISchool
{
School havard = new School(new HavardUniversity());
School oxford = new School(new OxfordUniversity());
var havardStudent = havard.AttendClass("Anthony");
var oxfordStudent = oxford.AttendClass("Kenneth");
public class School
{
private ISchool _school;
public ISchool school
{
get
{
if (_school == null) school = new HavardUniversity();
return _school;
var schoolInstance = new School();
schoolInstance.AttendClass("Anthony");
var oxford = new OxfordUniversity();
//Injecting the dependency via a property, which is the School Property
schoolInstance.school = oxford;
schoolInstance.AttendClass("Kenneth");
public class School
{
public string AttendClass(string student, ISchool school)
{
return school.Learn(student);
}
}