Last active
May 27, 2021 06:36
-
-
Save elden1337/e53711e47e5c450d794a7c9f7c9a898c to your computer and use it in GitHub Desktop.
Basic implementation of the Booli API rest-service using C#
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 RestSharp; | |
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.Security.Cryptography; | |
using System.Text; | |
namespace api.Services | |
{ | |
public class BooliServices | |
{ | |
readonly string key = "YOUR-API-KEY"; | |
readonly string callerId = "YOUR-CALLER-ID"; | |
readonly string refererUrl = "YOUR-REFERER-URL"; | |
public IRestResponse GetBooliProperties(string requestQuery, int? requestOffset, string objectType) | |
{ | |
//CREATE YOUR MODEL TO DESERIALIZE THE JSON-RESPONSE | |
var Json = new BooliPropertyListing(); | |
var timestamp = UnixTimestamp(); | |
var unique = Unique16(); | |
var hash = HashFactory.createHash(stringToHash(timestamp, unique, callerId)); | |
int requestLimit = 400; | |
var requestClient = new RestClient("https://api.booli.se/"); | |
var Request = new RestRequest("listings?bbox=" + requestQuery + "&objectType=" + objectType + "&limit=" + requestLimit + "&offset=" + requestOffset + "&callerId=" + callerId + "&time=" + timestamp + "&unique=" + unique + "&hash=" + hash, Method.GET); | |
Request.AddHeader("Referer", refererUrl); | |
return requestClient.Execute<BooliPropertyListing>(Request); | |
} | |
private int UnixTimestamp() | |
{ | |
Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; | |
return unixTimestamp; | |
} | |
private string Unique16() | |
{ | |
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
var stringChars = new char[16]; | |
var random = new Random(); | |
for (int i = 0; i < stringChars.Length; i++) | |
{ | |
stringChars[i] = chars[random.Next(chars.Length)]; | |
} | |
return new String(stringChars); | |
} | |
private string stringToHash(int timestamp, string unique, string callerId) | |
{ | |
return string.Format("{0}{1}{2}{3}",callerId,timestamp,key,unique); | |
} | |
public static class HashFactory | |
{ | |
public static string createHash(string s) | |
{ | |
byte[] bytes = Encoding.UTF8.GetBytes(s); | |
var sha1 = SHA1.Create(); | |
byte[] hashBytes = sha1.ComputeHash(bytes); | |
return HexStringFromBytes(hashBytes); | |
} | |
public static string HexStringFromBytes(byte[] bytes) | |
{ | |
var sb = new StringBuilder(); | |
foreach (byte b in bytes) | |
{ | |
var hex = b.ToString("x2"); | |
sb.Append(hex); | |
} | |
return sb.ToString(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment