Last active
March 6, 2023 20:35
-
-
Save elmogallen/9ae4148a0c92d251d44a4168438a6c53 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here are some options:
https://stackoverflow.com/questions/11803485/how-to-parse-json-using-restsharp