示例
以下示例为开始使用搜索路由奠定了基础。 请记住,您可以随时更改 rules
数组的内容,使其成为适合您的搜索查询。
- Python
- Javascript
- cURL
# Dependencies to install:
# $ python -m pip install requests
import json
import requests
CLIENT_ID = "YOUR_CLIENT_ID"
CLIENT_SECRET = "YOUR_CLIENT_SECRET"
API_URL = "https://api.getport.io/v1"
credentials = {"clientId": CLIENT_ID, "clientSecret": CLIENT_SECRET}
token_response = requests.post(f"{API_URL}/auth/access_token", json=credentials)
access_token = f"Bearer {token_response.json()['accessToken']}"
# You can now use the value in access_token when making further requests
headers = {
'Authorization': access_token
}
query = {
"combinator": "or",
"rules": [
{
"property": "$title",
"operator": "=",
"value": "admin-prod"
},
{
"property": "$title",
"operator": "=",
"value": "admin-test"
}
]
}
search_req = requests.post(f"{API_URL}/entities/search", headers=headers, json=query)
search_entities = search_req.json()['entities']
for entity in search_entities:
print(json.dumps(entity))
// Dependencies to install:
// $ npm install axios --save
const axios = require("axios").default;
const CLIENT_ID = "YOUR_CLIENT_ID";
const CLIENT_SECRET = "YOUR_CLIENT_SECRET";
const API_URL = "https://api.getport.io/v1";
(async () => {
const tokenResp = await axios.post(`${API_URL}/auth/access_token`, {
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});
const accessToken = tokenResp.data.accessToken;
// You can now use the value in accessToken when making further requests
const config = {
headers: {
Authorization: `Bearer ${accessToken}`,
},
};
const query = {
combinator: "or",
rules: [
{
property: "$title",
operator: "=",
value: "admin-prod",
},
{
property: "$title",
operator: "=",
value: "admin-test",
},
],
};
const response = await axios.post(
`${API_URL}/entities/search`,
query,
config
);
console.log(response.data["entities"]);
})();
#!/bin/bash
# Dependencies to install:
# For apt:
# $ sudo apt-get install jq
# For yum:
# $ sudo yum install jq
access_token=$(curl --location --request POST 'https://api.getport.io/v1/auth/access_token' \
--header 'Content-Type: application/json' \
--data-raw '{
"clientId": "CLIENT_ID",
"clientSecret": "CLIENT_SECRET"
}' | jq '.accessToken' | sed 's/"//g')
# The token will be available in the access_token variable
curl --location --request POST 'https://api.getport.io/v1/entities/search?attach_title_to_relation=true&exclude_calculated_properties=false' \
--header "Authorization: Bearer $access_token" \
--header 'Content-Type: application/json' \
--data-raw '{
"combinator": "or",
"rules": [
{
"property": "$title",
"operator": "=",
"value": "admin-prod"
},
{
"property": "$title",
"operator": "=",
"value": "admin-test"
}
]
}'
##在工作流程中使用目录搜索
存储在 Port 中的信息可被用作工作流、自动化和 CI/CD 流程的一部分。请参阅我们的service locking 指南,了解有关将 Port 的搜索与 CI/CD 集成的实用示例。