使用 Aras RESTful API 深入篇

原文:

http://community.aras.com/en/tech-tip-using-aras-restful-api/

Aras Innovator 11 SP12引入了一个可用于查询和编辑Aras项目的RESTful API。大多数情况下,这些REST调用将通过外部应用程序或集成发送到Aras服务器。您可以在Aras Labs GitHub页面上找到使用新Aras RESTful API 的示例AngularJS应用程序。然而,为了本博文的目的,我将使用流行的API开发环境  Postman来演示一些常见的REST调用。Postman是一个无需编写任何代码即可测试API调用的便捷工具。

设置呼叫

对Aras Innovator成功的API调用需要四个关键信息。首先,您需要Innovator实例的OData服务的URL:http:// {server} / {web alias} / server / odata。为简洁起见,我们将在这篇文章中将其称为“基础网址”。

您还需要包含以下标头以验证请求:

  • DATABASE:您要连接的数据库
  • AUTHUSER:你想用来连接的Innovator用户名
  • AUTHPASSWORD:MD5为AUTHUSER散列密码

在Postman中,您可以在“标题”选项卡下将标题添加到您的请求中

屏幕截图显示了在Postman客户端中输入的必需标题。

重要的提示!

在Aras Innovator中评估用户权限时,将使用请求标头中使用的AUTHUSER。如果您尝试获取或编辑AUTHUSER无权获取或编辑的项目,您将在电话的回复中收到错误或“找不到任何类型的{ItemType}”。同样,如果您按照本博客中所示对根用户或管理用户进行硬编码,则可能会无意中暴露出并非所有用户都应该能够获取和编辑的数据。

获取Part

最简单的GET调用检索ItemType中所有项目的集合。例如,我可以使用GET http:// {server} / {web alias} / server / odata / Part检索所有零件。

使用OData请求网址“http:// {server} / {web alias} / server / odata / Part”获取所有零件。

当然,您可能不想获取ItemType所有项目的所有属性。有几种不同的方式可以查询商品数据:

通过id检索单个项目

GET {base url}/Part(‘F45F259F527942EB8A6C4011BC784EF0’)

{
    "@odata.context": "http://localhost/FedSP12/server/odata/$metadata#Part/$entity",
    "classification": "Component",
    "created_on": "2018-03-20T19:37:02",
    "description": "Part 2 description",
    "generation": "1",
    "has_change_pending": "0",
    "id": "F45F259F527942EB8A6C4011BC784EF0",
    "is_current": "1",
    "is_released": "0",
    "keyed_name": "P-00002",
    "major_rev": "A",
    "make_buy": "Make",
    "modified_on": "2018-03-20T19:37:02",
    "name": "Part 2",
    "new_version": "0",
    "not_lockable": "0",
    "state": "Preliminary",
    "unit": "EA",
    "item_number": "P-00002"
}

根据条件筛选项目

 

GET {base url}/Part?$filter=item_number eq ‘P-00001’

{
    "@odata.context": "http://localhost/FedSP12/server/odata/$metadata#Part",
    "value": [
        {
            "classification": "Component",
            "created_on": "2018-03-20T19:36:06",
            "description": "Part 1 description",
            "generation": "1",
            "has_change_pending": "0",
            "id": "FE186527EB6B40079EE0334AD47B353A",
            "is_current": "1",
            "is_released": "0",
            "keyed_name": "P-00001",
            "major_rev": "A",
            "make_buy": "Make",
            "modified_on": "2018-03-20T19:36:06",
            "name": "Part 1",
            "new_version": "0",
            "not_lockable": "0",
            "state": "Preliminary",
            "unit": "EA",
            "item_number": "P-00001"
        }
    ]
}

选择特定的属性

GET {base url}/Part?$select=id,item_number,name

{
    "@odata.context": "http://localhost/FedSP12/server/odata/$metadata#Part(id,item_number,name)",
    "value": [
        {
            "id": "FE186527EB6B40079EE0334AD47B353A",
            "item_number": "P-00001",
            "name": "Part 1"
        },
        {
            "id": "F45F259F527942EB8A6C4011BC784EF0",
            "item_number": "P-00002",
            "name": "Part 2"
        },
        {
            "id": "16AA95A80DEB4B56BD8BA9BC509EAF0C",
            "item_number": "P-00033",
            "name": "Part 3"
        },
        {
            "id": "836DB08EB33D412EB662D75934CADCF1",
            "item_number": "Part-00005",
            "name": "Test Part 5"
        }
    ]
}

计算结果项目

GET {base url} / Part / $ count

4

获取物业价值

GET {base url} / Part(’F45F259F527942EB8A6C4011BC784EF0’)/ item_number / $ value

P-00002

获取项目属性的数据

 

GET {base url}/Part(‘F45F259F527942EB8A6C4011BC784EF0’)?$expand=created_by_id&$select=item_number

{
    "@odata.context": "http://localhost/FedSP12/server/odata/$metadata#Part(item_number)/$entity",
    "@odata.id": "Part('F45F259F527942EB8A6C4011BC784EF0')",
    "item_number": "P-00002",
    "created_by_id": {
        "created_on": "2002-04-24T13:46:12",
        "generation": "1",
        "id": "30B991F927274FA3829655F50C99472E",
        "is_current": "1",
        "is_released": "0",
        "keyed_name": "Innovator Admin",
        "last_login_date": "2018-03-22T12:56:04",
        "login_name": "admin",
        "logon_enabled": "1",
        "major_rev": "A",
        "modified_on": "2004-01-16T20:18:31",
        "new_version": "0",
        "not_lockable": "0",
        "state": "Released",
        "working_directory": "C:",
        "first_name": "Innovator",
        "last_name": "Admin"
    }
}

获取关系/相关项目

 

GET {base url}/Part(‘C3F13FE4A2674B9691A6B311B5CCBCDB’)/Part CAD?$expand=related_id

{
    "@odata.context": "http://localhost/FedSP12/server/odata/$metadata#Part CAD",
    "value": [
        {
            "behavior": "hard_fixed",
            "created_on": "2018-03-26T21:23:54",
            "generation": "1",
            "id": "A732D5425B0F4362BA48062BACA1C7F4",
            "is_current": "1",
            "is_released": "0",
            "keyed_name": "A732D5425B0F4362BA48062BACA1C7F4",
            "major_rev": "A",
            "modified_on": "2018-03-26T21:23:54",
            "new_version": "1",
            "not_lockable": "0",
            "related_id": {
                "created_on": "2018-03-26T21:23:30",
                "generation": "1",
                "has_change_pending": "0",
                "id": "B1ABC9872FF2440A9909E9D34A5AF363",
                "is_current": "1",
                "is_released": "0",
                "is_standard": "0",
                "is_template": "0",
                "keyed_name": "CAD-00002",
                "major_rev": "A",
                "modified_on": "2018-03-26T21:23:30",
                "name": "CAD for P-00002",
                "new_version": "1",
                "not_lockable": "0",
                "state": "Preliminary",
                "item_number": "CAD-00002"
            },
            "sort_order": "128"
        }
    ]
}

创建项目

要在Aras Innovator中创建新项目,请使用“POST”HTTP操作并将新项目的属性包含在请求正文中。

使用“POST”HTTP操作和请求主体中的必需属性创建一个新部件。

创建一个新项目

POST {base url}/Part

Body:

{
    "item_number": "Part-00005",
    "name": "Test Part 5"
}

Result:

{
    "@odata.context": "http://localhost/FedSP12/server/odata/$metadata#Part/$entity",
    "created_on": "2018-03-20T20:24:49",
    "generation": "1",
    "has_change_pending": "0",
    "id": "836DB08EB33D412EB662D75934CADCF1",
    "is_current": "1",
    "is_released": "0",
    "keyed_name": "Part-00005",
    "major_rev": "A",
    "make_buy": "Make",
    "modified_on": "2018-03-20T20:24:49",
    "name": "Test Part 5",
    "new_version": "0",
    "not_lockable": "0",
    "state": "Preliminary",
    "unit": "EA",
    "item_number": "Part-00005"
}

在属性中创建一个新项目(deep insert)

POST {base url} / Part

Body:

{
    "item_number": "Part-00006",
    "name": "Test Part 6",
    "owned_by_id":
    {
        "name": "New Identity"
    }
}

Response:

{
    "@odata.context": "http://localhost/FedSP12/server/odata/$metadata#Part/$entity",
    "created_on": "2018-03-26T21:45:28",
    "generation": "1",
    "has_change_pending": "0",
    "id": "8CAD376AC9284307A1299695FAC40F8F",
    "is_current": "1",
    "is_released": "0",
    "keyed_name": "Part-00006",
    "major_rev": "A",
    "make_buy": "Make",
    "modified_on": "2018-03-26T21:45:29",
    "name": "Test Part 6",
    "new_version": "0",
    "not_lockable": "0",
    "owned_by_id": {
        "created_on": "2018-03-26T21:45:29",
        "generation": "1",
        "id": "41D69C4C75BF4512942E0368385B3A26",
        "is_alias": "0",
        "is_current": "1",
        "is_released": "0",
        "keyed_name": "New Identity",
        "major_rev": "A",
        "modified_on": "2018-03-26T21:45:29",
        "new_version": "1",
        "not_lockable": "0",
        "name": "New Identity"
    },
    "state": "Preliminary",
    "unit": "EA",
    "item_number": "Part-00006"
}

编辑项目

要更新Aras项目,请使用“PATCH”HTTP操作,并在请求的主体中包含要更新的任何属性。请确保只包含可更新的属性,否则请求将失败。

更新部件号P-00033以修复item_number属性。

更新一个项目

PATCH {base url}/Part(’16AA95A80DEB4B56BD8BA9BC509EAF0C’)

Body:

{
    "item_number": "Part-00003"
}

Response:

{
    "@odata.context": "http://localhost/FedSP12/server/odata/$metadata#Part/$entity",
    "classification": "Assembly",
    "created_on": "2018-03-20T19:37:43",
    "description": "Part 3 description",
    "generation": "2",
    "has_change_pending": "0",
    "id": "7F7556DE8ED642A6BE78C43259A00087",
    "is_current": "1",
    "is_released": "0",
    "keyed_name": "Part-00003",
    "major_rev": "A",
    "make_buy": "Make",
    "modified_on": "2018-03-26T21:51:58",
    "name": "Part 3",
    "new_version": "0",
    "not_lockable": "0",
    "state": "Preliminary",
    "unit": "EA",
    "item_number": "Part-00003"
}

删除项目

要使用Aras RESTful API删除项目,您可以使用“DELETE”HTTP操作。一定要包含您要删除的项目的ID。

通过id删除零件。

删除一个项目

DELETE {base url}/Part(‘C3F13FE4A2674B9691A6B311B5CCBCDB’)

删除项目时,请求正文和响应都是空的。请注意,您一次只能删除一个项目。

405.0 – 方法不允许的错误

尝试从Innovator删除项目时是否收到405 HTTP错误?如果开箱即用的安装中的WebDAV模块与RESTful API冲突,则可能会发生这种情况。要解决此错误,您可以尝试以下步骤。

  1. 备份您的代码树并将副本保存在安全的地方。
  2. 在服务器上打开IIS。
  3. 在“连接”窗格中,导航至您的Aras Innovator实例的服务器子应用程序。
  4. 在功能视图列表中,双击模块。
  5. 选择WebDAVModule模块。
  6. 在“操作”窗格中,单击“删除”。
  7. 点击“是”进行确认。
打赏
分享到QQ 分享到微信 分享到微博

0 条评论

发表我的观点

取消

切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录
切换登录

注册