Created
June 1, 2012 03:03
-
-
Save JohnMurray/2848335 to your computer and use it in GitHub Desktop.
Code descriptions for log-entry on API Anti-Patterns (johnmurray.io)
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
{ | |
"user_id": 1, | |
"car_id": 2, | |
"template_data": [ | |
{"Key": "key1", "Value": "value1"}, | |
{"Key": "key2", "Value": "value2"}, | |
{"Key": "key3", "Value": "value3"} | |
], | |
"key": [ | |
{"Key": "key1", "Value": "value1"}, | |
{"Key": "key2", "Value": "value2"} | |
] | |
} |
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
public static class IDictionaryExtension | |
{ | |
public static JsonDictionary<K, V> ToJsonDictionary<K,V>(this IDictionary<K, V> dict) | |
{ | |
if (dict == null) | |
return null; | |
var jsonDict = new JsonDictionary<K, V>(); | |
foreach (var key in dict.Keys) | |
{ | |
jsonDict[key] = dict[key]; | |
} | |
return jsonDict; | |
} | |
} |
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
uri = URI.parse('http://localhost:4567') | |
Net::HTTP.start(uri.host, uri.port) do |http| | |
headers = {'Content-Type': 'application/json'} | |
http.post2('/service_path', data.to_json, headers) do |resp| | |
return resp.code == '200' | |
end | |
end |
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
var dict = new Dictionary<String, Int32> | |
{ | |
{"one", 1 }, | |
{"two", 2 }, | |
{"three", 3 }, | |
// ... | |
{"ten", 10} | |
}; |
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
{ | |
"user_id": 1, | |
"car_id": 2, | |
"template_data": { | |
"key1": "value1", | |
"key2": "value2", | |
"key3": "value3" | |
}, | |
"key": { | |
"key1": "value1", | |
"key2": "value2" | |
} | |
} |
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
/// <summary> | |
/// Taken from: http://goo.gl/Xafe5 | |
/// </summary> | |
[Serializable] | |
public class JsonDictionary<K, V> : ISerializable | |
{ | |
Dictionary<K, V> dict = new Dictionary<K, V>(); | |
public JsonDictionary() { } | |
protected JsonDictionary(SerializationInfo info, StreamingContext context) | |
{ | |
throw new NotImplementedException(); | |
} | |
public void GetObjectData(SerializationInfo info, StreamingContext context) | |
{ | |
foreach (K key in dict.Keys) | |
{ | |
info.AddValue(key.ToString(), dict[key]); | |
} | |
} | |
public void Add(K key, V value) | |
{ | |
dict.Add(key, value); | |
} | |
public V this[K index] | |
{ | |
set { dict[index] = value; } | |
get { return dict[index]; } | |
} | |
} |
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
[DataContract] | |
public class MyObjectDataContract | |
{ | |
[DataMember(Name = "user_id")] | |
public int? UserId { get; set; } | |
[DataMember(Name = "car_id")] | |
public int? CarId { get; set; } | |
[DataMember(Name = "template_data")] | |
public Dictionary<String, object> TemplateData { get; set; } | |
[DataMember(Name = "key")] | |
public Dictionary<String, object> Key { get; set; } | |
} |
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
WebRequest request = WebRequest.Create(NSUrl); | |
request.Method = "POST"; | |
request.ContentLength = jsonStream.Length; | |
request.ContentType = "application/json"; | |
Stream requestStream = request.GetRequestStream(); | |
requestStream.Write(jsonStream.ToArray(), 0, (int)jsonStream.Length); | |
requestStream.Close(); |
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
// Build up request | |
// ... | |
// Send request | |
request.GetResponse(); |
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
// assume myObjectDataContract is already set and defined | |
MemoryStream jsonStream = new MemoryStream(); | |
DataContractJsonSerializer ser = new DataContractJsonSerializer( | |
typeof(MyObjectDataContract)); | |
ser.WriteObject(jsonStream, myObjectDataContract); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment