Skip to content

Instantly share code, notes, and snippets.

@elmogallen
Last active March 6, 2023 20:35
Show Gist options
  • Save elmogallen/9ae4148a0c92d251d44a4168438a6c53 to your computer and use it in GitHub Desktop.
Save elmogallen/9ae4148a0c92d251d44a4168438a6c53 to your computer and use it in GitHub Desktop.
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Script.Serialization;
namespace RetailerUrlStatusChecker
{
public class SifProcessor
{
public string Token { get; set; }
public List<Location> Locations { get; set; }
public string GetToken()
{
// Get Token
var client = new RestClient("https://your-url.com/login");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\"username\": \"your-username\", \"password\": \"your-password\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
JavaScriptSerializer serializer = new JavaScriptSerializer();
var d = serializer.Deserialize<TokenResponse>(response.Content);
var token = d.token;
return token;
}
public List<Location> GetLocations()
{
var client = new RestClient("https://your-url.com/locations");
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", this.Token);
IRestResponse response = client.Execute(request);
JavaScriptSerializer serializer = new JavaScriptSerializer();
var d = serializer.Deserialize<LocationResponse>(response.Content);
return d.locations;
}
public void Process()
{
this.Token = GetToken();
this.Locations = GetLocations();
Console.WriteLine(this.Locations.Count());
foreach (var loc in this.Locations)
{
Console.WriteLine(loc.busName);
}
}
}
// -----------------------------
public class TokenResponse {
public string username { get; set; }
public bool authenticated { get; set; }
public string token { get; set; }
}
public class LocationResponse
{
public List<Location> locations { get; set; }
}
public class Location
{
public int id { get; set; }
public string busName { get; set; }
}
}
@davehaynes-shawinc
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment