Last active
March 2, 2022 22:43
-
-
Save lgzarturo/a8a643553ccab179964f12483c44aa64 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net; | |
using System.IO; | |
namespace Example | |
{ | |
class Program | |
{ | |
public static void functionOne() { | |
var client = new RestClient("https://api.twilio.com/json/send"); | |
client.Authenticator = new SimpleAuthenticator("username", "myusername", "password", "mypassword"); | |
var request = new RestRequest(Method.POST); | |
request.AddHeader("Cache-Control", "no-cache"); | |
request.AddHeader("Content-Type", "application/json"); | |
request.AddParameter("undefined", "{\"message\":\"Text of the SMS message\", \"tpoa\":\"Sender\",\"recipient\":[{\"msisdn\":\"12015550123\"},{\"msisdn\":\"447400123456\"},{\"msisdn\":\"5212221234567\"}]}", ParameterType.RequestBody); | |
IRestResponse response = client.Execute(request); | |
} | |
public static void functionTwo() { | |
WebClient client = new WebClient(); | |
client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); | |
client.QueryString.Add("username", "xx@xx.xx"); | |
client.QueryString.Add("password", "xxxxxxxx"); | |
string baseurl ="http://api.twilio.com/get/balance.php"; | |
Stream data = client.OpenRead(baseurl); | |
StreamReader reader = new StreamReader(data); | |
string s = reader.ReadToEnd(); | |
data.Close(); | |
reader.Close(); | |
} | |
public static string FunctioThree(this DateTime date) | |
{ | |
if (date <= DateTime.Now) | |
{ | |
var timeSince = DateTime.Now.Subtract(date); | |
if (timeSince.TotalMilliseconds < 1) return "not yet"; | |
if (timeSince.TotalMinutes < 1) return "just now"; | |
if (timeSince.TotalMinutes < 2) return "1 minute ago"; | |
if (timeSince.TotalMinutes < 60) return string.Format("{0} minutes ago", timeSince.Minutes); | |
if (timeSince.TotalMinutes < 120) return "1 hour ago"; | |
if (timeSince.TotalHours < 24) return string.Format("{0} hours ago", timeSince.Hours); | |
if (timeSince.TotalDays == 1) return "yesterday"; | |
if (timeSince.TotalDays < 7) return string.Format("{0} days ago", timeSince.Days); | |
if (timeSince.TotalDays < 14) return "last week"; | |
if (timeSince.TotalDays < 21) return "2 weeks ago"; | |
if (timeSince.TotalDays < 28) return "3 weeks ago"; | |
if (timeSince.TotalDays < 60) return "last month"; | |
if (timeSince.TotalDays < 365) | |
return string.Format("{0} months ago", Math.Round(timeSince.TotalDays / 30)); | |
if (timeSince.TotalDays < 730) return "last year"; | |
return string.Format("{0} years ago", Math.Round(timeSince.TotalDays / 365)); | |
} | |
else | |
{ | |
var timeUntil = date.Subtract(DateTime.Now); | |
if (timeUntil.TotalMilliseconds < 1) return "not yet"; | |
if (timeUntil.TotalMinutes < 1) return "just now"; | |
if (timeUntil.TotalMinutes < 2) return "1 minute"; | |
if (timeUntil.TotalMinutes < 60) return string.Format("{0} minutes", timeUntil.Minutes); | |
if (timeUntil.TotalMinutes < 120) return "1 hour"; | |
if (timeUntil.TotalHours < 24) return string.Format("{0} hours", timeUntil.Hours); | |
if ((int)timeUntil.TotalDays == 1) return "tomorrow"; | |
if (timeUntil.TotalDays < 7) return string.Format("{0} days", timeUntil.Days); | |
if (timeUntil.TotalDays < 14) return "next week"; | |
if (timeUntil.TotalDays < 21) return "in 2 weeks"; | |
if (timeUntil.TotalDays < 28) return "in 3 weeks"; | |
if (timeUntil.TotalDays < 60) return "next month"; | |
if (timeUntil.TotalDays < 365) return string.Format("{0} months", Math.Round(timeUntil.TotalDays / 30)); | |
if (timeUntil.TotalDays < 730) return "next year"; | |
return string.Format("in {0} years", Math.Round(timeUntil.TotalDays / 365)); | |
} | |
} | |
public static string ToString(this DateTime? date, string nullText, string format = "") | |
{ | |
if (date.HasValue) | |
{ | |
if (format.IsNullOrEmpty()) | |
return date.Value.ToString(); | |
return date.Value.ToString(format); | |
} | |
return nullText; | |
} | |
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek) | |
{ | |
int diff = dt.DayOfWeek - startOfWeek; | |
if (diff < 0) | |
{ | |
diff += 7; | |
} | |
return dt.AddDays(-1 * diff).Date; | |
} | |
public static bool FunctionFour(this DateTime date, DateTime firstDate, DateTime secondDate) | |
{ | |
return (date >= firstDate && date <= secondDate) || (date <= firstDate && date >= secondDate); | |
} | |
public static bool FunctionFive(this DateTime date) | |
{ | |
return date.IsBetween((DateTime)System.Data.SqlTypes.SqlDateTime.MinValue, (DateTime)System.Data.SqlTypes.SqlDateTime.MaxValue); | |
} | |
} | |
class ProgramTwo | |
{ | |
private const string GRANT_TYPE = "client_credentials"; | |
private const string AUTH_URL = "https://login.microsoftonline.com/"; | |
private readonly string _tenant, _clientId, _clientSecret, _resource, _scope; | |
public async Task<string> FunctionAsync() | |
{ | |
var values = new List<KeyValuePair<string, string>> | |
{ | |
new KeyValuePair<string, string>("client_id", _clientId), | |
new KeyValuePair<string, string>("client_secret", _clientSecret), | |
new KeyValuePair<string, string>("scope", _scope), | |
new KeyValuePair<string, string>("grant_type", GRANT_TYPE), | |
new KeyValuePair<string, string>("resource",_resource) | |
}; | |
var client = new HttpClient(); | |
var requestUrl = $"{AUTH_URL}{_tenant}/oauth2/token"; | |
var requestContent = new FormUrlEncodedContent(values); | |
var response = await client.PostAsync(requestUrl, requestContent); | |
var responseBody = await response.Content.ReadAsStringAsync(); | |
dynamic tokenResponse = JsonConvert.DeserializeObject(responseBody); | |
return tokenResponse?.access_token; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment