接收 Autodesk Forge 的集线器列表 API
Receiving list of hubs with Autodesk Forge API
我正在尝试使用 Autodesk forge API for C# 来获取集线器列表。
这是我到目前为止所做的:
HubsApi api = new HubsApi();
Hubs hubs = api.GetHubs();
很简单。但是当我这样做时,我得到一个异常,抱怨说无法从 DynamicJsonResponse
转换为 Hubs
。我认为,这是因为我在响应字符串中收到了两个警告,因此它不再是 Hub 对象。警告看起来像这样:
"warnings":[
{
"Id":null,
"HttpStatusCode":"403",
"ErrorCode":"BIM360DM_ERROR",
"Title":"Unable to get hubs from BIM360DM EMEA.",
"Detail":"You don't have permission to access this API",
"AboutLink":null,
"Source":[
],
"meta":[
]
}
所有这些都包含在一个包含四个条目的字典中,其中只有两个是数据。但是,根据 Autodesk 的说法,可以忽略此警告。
所以在那之后我尝试将它转换成字典并且只有 select 数据条目
HubsApi api = new HubsApi();
DynamicJsonResponse resp = api.GetHubs();
DynamicDictionary hubs = (DynamicDictionary)resp.Dictionary["data"];
然后我遍历它:
for(int i = 0; i < hubs.Dictionary.Count && bim360hub == null; i++)
{
string hub = hubs.Dictionary.ElementAt(i).ToString();
[....]
}
但是字符串 hub
也不是 json-hub。它是一个如下所示的数组:
[
0,
{
"type": "hubs",
"id": "****",
"attributes": {...},
"links": {...},
"relationships": {...},
}
]
数组中的第二个元素是我的枢纽。我知道,我怎么可以select第二个元素。但是必须有一个更容易获得集线器列表的方法。
在参考资料中的一个示例中,它似乎可以使用以下简单的两行代码:
HubsApi api = new HubsApi();
Hubs hubs = api.GetHubs();
有什么想法,我是如何设法获得集线器的?
首先,考虑使用这些方法的异步版本,避免使用 非异步 调用,因为它会导致您的桌面应用程序冻结(在连接时)或分配更多资源ASP.NET.
以下function is part of this sample, which lists all hubs, projects and files under a user account. It's a good place to start. Note it's organizing the Hubs in a TreeNode
list, which is compatible with jsTree.
private async Task<IList<TreeNode>> GetHubsAsync()
{
IList<TreeNode> nodes = new List<TreeNode>();
HubsApi hubsApi = new HubsApi();
hubsApi.Configuration.AccessToken = AccessToken;
var hubs = await hubsApi.GetHubsAsync();
string urn = string.Empty;
foreach (KeyValuePair<string, dynamic> hubInfo in new DynamicDictionaryItems(hubs.data))
{
string nodeType = "hubs";
switch ((string)hubInfo.Value.attributes.extension.type)
{
case "hubs:autodesk.core:Hub":
nodeType = "hubs";
break;
case "hubs:autodesk.a360:PersonalHub":
nodeType = "personalhub";
break;
case "hubs:autodesk.bim360:Account":
nodeType = "bim360hubs";
break;
}
TreeNode hubNode = new TreeNode(hubInfo.Value.links.self.href, (nodeType == "bim360hubs" ? "BIM 360 Projects" : hubInfo.Value.attributes.name), nodeType, true);
nodes.Add(hubNode);
}
return nodes;
}
我正在尝试使用 Autodesk forge API for C# 来获取集线器列表。 这是我到目前为止所做的:
HubsApi api = new HubsApi();
Hubs hubs = api.GetHubs();
很简单。但是当我这样做时,我得到一个异常,抱怨说无法从 DynamicJsonResponse
转换为 Hubs
。我认为,这是因为我在响应字符串中收到了两个警告,因此它不再是 Hub 对象。警告看起来像这样:
"warnings":[
{
"Id":null,
"HttpStatusCode":"403",
"ErrorCode":"BIM360DM_ERROR",
"Title":"Unable to get hubs from BIM360DM EMEA.",
"Detail":"You don't have permission to access this API",
"AboutLink":null,
"Source":[
],
"meta":[
]
}
所有这些都包含在一个包含四个条目的字典中,其中只有两个是数据。但是,根据 Autodesk 的说法,可以忽略此警告。
所以在那之后我尝试将它转换成字典并且只有 select 数据条目
HubsApi api = new HubsApi();
DynamicJsonResponse resp = api.GetHubs();
DynamicDictionary hubs = (DynamicDictionary)resp.Dictionary["data"];
然后我遍历它:
for(int i = 0; i < hubs.Dictionary.Count && bim360hub == null; i++)
{
string hub = hubs.Dictionary.ElementAt(i).ToString();
[....]
}
但是字符串 hub
也不是 json-hub。它是一个如下所示的数组:
[
0,
{
"type": "hubs",
"id": "****",
"attributes": {...},
"links": {...},
"relationships": {...},
}
]
数组中的第二个元素是我的枢纽。我知道,我怎么可以select第二个元素。但是必须有一个更容易获得集线器列表的方法。 在参考资料中的一个示例中,它似乎可以使用以下简单的两行代码:
HubsApi api = new HubsApi();
Hubs hubs = api.GetHubs();
有什么想法,我是如何设法获得集线器的?
首先,考虑使用这些方法的异步版本,避免使用 非异步 调用,因为它会导致您的桌面应用程序冻结(在连接时)或分配更多资源ASP.NET.
以下function is part of this sample, which lists all hubs, projects and files under a user account. It's a good place to start. Note it's organizing the Hubs in a TreeNode
list, which is compatible with jsTree.
private async Task<IList<TreeNode>> GetHubsAsync()
{
IList<TreeNode> nodes = new List<TreeNode>();
HubsApi hubsApi = new HubsApi();
hubsApi.Configuration.AccessToken = AccessToken;
var hubs = await hubsApi.GetHubsAsync();
string urn = string.Empty;
foreach (KeyValuePair<string, dynamic> hubInfo in new DynamicDictionaryItems(hubs.data))
{
string nodeType = "hubs";
switch ((string)hubInfo.Value.attributes.extension.type)
{
case "hubs:autodesk.core:Hub":
nodeType = "hubs";
break;
case "hubs:autodesk.a360:PersonalHub":
nodeType = "personalhub";
break;
case "hubs:autodesk.bim360:Account":
nodeType = "bim360hubs";
break;
}
TreeNode hubNode = new TreeNode(hubInfo.Value.links.self.href, (nodeType == "bim360hubs" ? "BIM 360 Projects" : hubInfo.Value.attributes.name), nodeType, true);
nodes.Add(hubNode);
}
return nodes;
}