Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added versioning #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .whitesource
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"generalSettings": {
"shouldScanRepo": true
},
"checkRunSettings": {
"vulnerableCheckRunConclusionLevel": "failure"
}
}
135 changes: 134 additions & 1 deletion Core/SharpSquare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using System.IO;
using System.Web.Script.Serialization;
using FourSquare.SharpSquare.Entities;
using System.Collections;
using System.Reflection;
using System.Diagnostics;

namespace FourSquare.SharpSquare.Core
{
Expand Down Expand Up @@ -50,6 +53,7 @@ private string Request(string url, HttpMethod httpMethod)

private string Request(string url, HttpMethod httpMethod, string data)
{
url = url + "&v=20140101";
string result = string.Empty;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = httpMethod.ToString();
Expand Down Expand Up @@ -95,7 +99,124 @@ private FourSquareSingleResponse<T> GetSingle<T>(string endpoint, Dictionary<str
{
return GetSingle<T>(endpoint, parameters, false);
}
/// <summary>
/// TODO: make this add to global errors to return to the caller
/// </summary>
/// <param name="message"></param>
private void InterceptError(string message)
{
var cc = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message);
Console.ForegroundColor = cc;
Debug.WriteLine(message);
}
private object DeserializeObject(Dictionary<string, object> dictItem, Type tip)
{

var newInstance = Activator.CreateInstance(tip);
var isDictionary = (tip.GetInterface("IDictionary") != null);
PropertyInfo p = null;
foreach (var k in dictItem.Keys)
{
var ser = new JavaScriptSerializer().Serialize(dictItem[k]);


Type tipP = null;
if (isDictionary)
{
tipP = tip.GetGenericArguments()[1];
}
else
{

try
{
p = tip.GetProperty(k);
if (p == null)
throw new ArgumentNullException(k);
}
catch (Exception)
{
//Console.WriteLine(ex.Message);
InterceptError("missing property:" + k + " to class" + tip.FullName + " value:" + ser);
continue;
}
tipP = p.PropertyType;
}


if (tipP.IsClass && tipP.FullName != typeof(string).FullName)
{

dynamic val = null;
try
{
val = new JavaScriptSerializer().Deserialize(ser, tipP);
}
catch (Exception ex)
{
Console.WriteLine("error for class:" + k + ex.Message);
IList arr = dictItem[k] as object[];
if (arr == null)
{
arr = dictItem[k] as ArrayList;
}
if (arr == null)
{
var t1 = dictItem[k] as Dictionary<string, object>;
if (t1 == null)
{
InterceptError("Not a dictionary, not an array - please contact [email protected] for " + k);
}

val = DeserializeObject(dictItem[k] as Dictionary<string, object>, tipP);

}
else
{
val = Activator.CreateInstance(tipP);
var tipGen = tipP.GetGenericArguments()[0];
foreach (var obj in arr)
{
val.Add((dynamic)DeserializeObject(obj as Dictionary<string, object>, tipGen));
}
}


}

if (isDictionary)
{
((IDictionary)newInstance).Add(k, Convert.ChangeType(val, tipP));
}
else
{
p.SetValue(newInstance, Convert.ChangeType(val, tipP), null);

}
}
else//simple int , string,
{
try
{
if (isDictionary)
{
((IDictionary)newInstance).Add(k, Convert.ChangeType(dictItem[k], tipP));
}
else
{
p.SetValue(newInstance, Convert.ChangeType(dictItem[k], tipP), null);
}
}
catch (Exception ex)
{
InterceptError("!!!not a simple property " + k + " from " + tip + " value:" + ser);
}
}
}
return newInstance;
}
private FourSquareSingleResponse<T> GetSingle<T>(string endpoint, Dictionary<string, string> parameters, bool unauthenticated) where T : FourSquareEntity
{
string serializedParameters = "";
Expand All @@ -115,7 +236,19 @@ private FourSquareSingleResponse<T> GetSingle<T>(string endpoint, Dictionary<str
}

string json = Request(string.Format("{0}{1}?{2}{3}", apiUrl, endpoint, oauthToken, serializedParameters), HttpMethod.GET);
FourSquareSingleResponse<T> fourSquareResponse = new JavaScriptSerializer().Deserialize<FourSquareSingleResponse<T>>(json);
FourSquareSingleResponse<T> fourSquareResponse;
try
{
fourSquareResponse = new JavaScriptSerializer().Deserialize<FourSquareSingleResponse<T>>(json); //json parameter is the string content of the api
}
catch (Exception ex)
{
var obj = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(json); //json parameter is the string content of the api
fourSquareResponse = DeserializeObject(obj, typeof(FourSquareSingleResponse<T>)) as FourSquareSingleResponse<T>;
//TODO: look into debug in VS to see what it is missing
Debugger.Break();
throw;
}
return fourSquareResponse;
}

Expand Down
9 changes: 8 additions & 1 deletion Entities/Category.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@

namespace FourSquare.SharpSquare.Entities
{
public class ImageNew
{
public string prefix { get; set; }
public string suffix { get; set; }

}
public class Category : FourSquareEntity
{

/// <summary>
/// A unique identifier for this category.
/// </summary>
Expand Down Expand Up @@ -48,7 +55,7 @@ public string shortName
/// Combine prefix with a size (32, 44, 64, and 88 are available) and suffix, e.g. https://foursquare.com/img/categories/food/default_64.png.
/// To get an image with a gray background, use bg_ before the size, e.g. https://foursquare.com/img/categories_v2/food/icecream_bg_32.png.
/// </summary>
public string icon
public Image icon
{
get;
set;
Expand Down
2 changes: 1 addition & 1 deletion Entities/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public string gender
/// <summary>
/// URL of a profile picture for this user.
/// </summary>
public string photo
public ImageNew photo
{
get;
set;
Expand Down