Skip to content

Commit 4269714

Browse files
committed
Added method to get and cache TypeDefs (for WSAPI 1.25 and later)
Added method to get type attributes from cached typeDefs Fixed JSON serializer to escape double quotes Changed version info 1.0.12.0 Escape ampersand when building Query url
1 parent ee94249 commit 4269714

4 files changed

Lines changed: 58 additions & 5 deletions

File tree

Rally.RestApi/DynamicJsonSerializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
using System.Text;
33
using System.Web.Script.Serialization;
44
using System.Collections;
5+
using System;
56

67
namespace Rally.RestApi
78
{
8-
99
internal class DynamicJsonSerializer
1010
{
1111
readonly JavaScriptSerializer deSerializer;
@@ -76,7 +76,7 @@ private static string SerializeObject(object obj)
7676
{
7777
if (obj is string)
7878
{
79-
return "\"" + obj + "\"";
79+
return "\"" + ((String)obj).Replace("\"", "\\\"") + "\"";
8080
}
8181
return obj.ToString();
8282
}

Rally.RestApi/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.0.11.0")]
35-
[assembly: AssemblyFileVersion("1.0.11.0")]
34+
[assembly: AssemblyVersion("1.0.12.0")]
35+
[assembly: AssemblyFileVersion("1.0.12.0")]
3636

3737
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Rally.RestApi.Test")]

Rally.RestApi/Query.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text.RegularExpressions;
5+
using System.Web;
56

67
namespace Rally.RestApi
78
{
@@ -233,7 +234,7 @@ public string QueryClause
233234
return queryClause;
234235
}
235236
const char quote = '"';
236-
var outValue = Value.Contains(" ") ? quote + Value + quote : Value;
237+
var outValue = (Value.Contains(" ") ? quote + Value + quote : Value).Replace("&","%26");
237238
return string.Format("({0} {1} {2})", Attribute, GetOperator(QueryOperator), outValue);
238239
}
239240

Rally.RestApi/RallyRestApi.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public enum HeaderType
7171

7272
private readonly string wsapiVersion;
7373
internal HttpService Service { get; set; }
74+
private readonly Dictionary<string, DynamicJsonObject> typeDefs = new Dictionary<string, DynamicJsonObject>();
7475

7576
internal string DecodeHeaderName(HeaderType type)
7677
{
@@ -483,6 +484,57 @@ public DynamicJsonObject GetAllowedAttributeValues(string type, string attribute
483484
return MakeRequest(GetFullyQualifiedUri(string.Format("/{0}/{1}/allowedValues.js", type, attribute)));
484485
}
485486

487+
public List<DynamicJsonObject> GetTypeAttributes(string typeName, Boolean cache)
488+
{
489+
DynamicJsonObject typeDef = GetTypeDef(typeName, cache);
490+
var attributes = typeDef["Attributes"] as ArrayList;
491+
return attributes.Cast<DynamicJsonObject>().OrderBy(a => a["Name"]).ToList();
492+
}
493+
494+
public DynamicJsonObject GetTypeDef(string typeName, Boolean cache)
495+
{
496+
if (!wsapiVersion.ToLower().Equals(DEFAULT_WSAPI_VERSION.ToLower()))
497+
{
498+
float apiVersion;
499+
500+
if (float.TryParse(wsapiVersion, out apiVersion))
501+
{
502+
if (apiVersion < 1.25)
503+
throw new ArgumentOutOfRangeException(String.Format("webServiveVersion must be '1.25' or greater to use this method. Received '{1}'", wsapiVersion));
504+
}
505+
else
506+
{
507+
throw new ArgumentException(String.Format("webServiveVersion must be '{0}' or a floating point number. Received '{1}'", DEFAULT_WSAPI_VERSION, wsapiVersion));
508+
}
509+
}
510+
511+
DynamicJsonObject typeDef = null;
512+
513+
if (typeDefs.ContainsKey(typeName))
514+
{
515+
typeDef = typeDefs[typeName];
516+
}
517+
else
518+
{
519+
var typeDefRequest = new Request("TypeDefinition");
520+
typeDefRequest.Fetch = new List<string>() { "true" };
521+
typeDefRequest.Query = new Query("Name", RestApi.Query.Operator.Equals, typeName);
522+
QueryResult result = Query(typeDefRequest);
523+
if (!result.Success)
524+
throw new Exception(String.Format("Error fetching TypeDef for '{0}'\n\n{1}", typeName, String.Join("\n",result.Errors)));
525+
else if (result.TotalResultCount == 0)
526+
throw new Exception(String.Format("No TypeDef found for '{0}'",typeName));
527+
else if (result.TotalResultCount > 1)
528+
throw new Exception(String.Format("Too many ({0}) TypeDefs found for '{1}'", result.TotalResultCount, typeName));
529+
typeDef = result.Results.First();
530+
531+
if (cache)
532+
typeDefs[typeName] = typeDef;
533+
}
534+
535+
return typeDef;
536+
}
537+
486538
/// <summary>
487539
/// Get the attribute definitions for the specified type
488540
/// </summary>

0 commit comments

Comments
 (0)