Put response of REST-API in variable Windows Batch File - rest

i am new to Windows Batch File Scripting. Could anyone explain me how i can extract one value of an REST API Response into a variable?
i made this call:
curl https://api.stripe.com/v1/customers -u sk_live_1qwnCoXXXXXXXXXXXXX: -d card[number]=494XXXXXXX9 -d card[exp_month]=10 -d card[exp_year]=2022 -d card[cvc]=081 -d email=XXXXXXXXXXXX
{
"id": "**cus_DqXXXXXXXXXXTu**",
"object": "customer",
"account_balance": 0,
"created": 1540311116,
"currency": null,
"default_source": "card_1DOXXXXXXMf",
"delinquent": false,
"description": null,
"discount": null,
"email": "XXXXXXXXXX",
"invoice_prefix": "69C53EF",
"livemode": true,
"metadata": {
},
"shipping": null,
"sources": {
"object": "list",
"data": [
{
"id": "card_1DOXXXXXXXXXXf",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "DE",
"customer": "**cus_DXXXXXXXTu**",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 10,
"exp_year": 2022,
"fingerprint": "1WXXXXXXXXXX5",
"funding": "prepaid",
"last4": "XXXX",
"metadata": {
},
"name": null,
"tokenization_method": null
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/customers/cus_Dq8XXXXXXXTu/sources"
},
"subscriptions": {
"object": "list",
"data": [
],
"has_more": false,
"total_count": 0,
"url": "/v1/customers/cus_Dq8XXXXXXXTu/subscriptions"
},
"tax_info": null,
"tax_info_verification": null
}
I have to extract the id starting with "cus_" in first line of response and this id in sources section starting with "card_"
I have search through google, but maybe i didn't see the right answer.
Need this script to automate the invoicing in our inventory managment system.

whilst not completely impossible, parsing json is far more advanced than what people should be doing with MS.bat scripting, which is a rather simple language. if you can, please switch to a better scripting language, this would be trivial in something like PHP or Python or Perl. for example with php-cli, which you can get here, it'd be:
<?php
$json=trim(shell_exec('curl https://api.stripe.com/v1/customers -u sk_live_1qwnCoXXXXXXXXXXXXX: -d card[number]=494XXXXXXX9 -d card[exp_month]=10 -d card[exp_year]=2022 -d card[cvc]=081 -d email=XXXXXXXXXXXX'));
$data=json_decode($json,true);
$id=$data['id'];
$id2=$data['sources']['data'][0]['id'];
echo "first id: {$id} - data id: {$id2}";
and if you set up php-cli's php.exe as the default handler for .php files, you can run your php script directly from the command line, example:

Related

Whats wrong in my camunda fetchAndLock API request. Getting InvalidRequestException

I have 2 topics to receive data from API, those I can Successfully executed through code. Now I'm trying to execute through rest api using postman tool. now i'm getting InvalidRequestException. Before attempting request I fetched the external tasks using camunda get external-task api and my topics showing there.Later I tried to use /external-task/fetchAndLock API to send input variables.
External tasks response is:
http://localhost:8080/engine-rest/external-task
[
{
"activityId": "Activity_0jokenq",
"activityInstanceId": "Activity_0jokenq:0623e6f2-4837-11ec-8c7e-02426d005d3a",
"errorMessage": null,
"executionId": "0623e6f1-4837-11ec-8c7e-02426d005d3a",
"id": "0623e6f3-4837-11ec-8c7e-02426d005d3a",
"lockExpirationTime": null,
"processDefinitionId": "Process_0qcjqnm:1:da2ae20a-4836-11ec-8c7e-02426d005d3a",
"processDefinitionKey": "Process_0qcjqnm",
"processDefinitionVersionTag": null,
"processInstanceId": "0623bfdb-4837-11ec-8c7e-02426d005d3a",
"retries": null,
"suspended": false,
"workerId": null,
"topicName": "yvalue",
"tenantId": null,
"priority": 0,
"businessKey": null
},
{
"activityId": "Activity_1xxpyet",
"activityInstanceId": "Activity_1xxpyet:0623e6f6-4837-11ec-8c7e-02426d005d3a",
"errorMessage": null,
"executionId": "0623e6f5-4837-11ec-8c7e-02426d005d3a",
"id": "0623e6f7-4837-11ec-8c7e-02426d005d3a",
"lockExpirationTime": null,
"processDefinitionId": "Process_0qcjqnm:1:da2ae20a-4836-11ec-8c7e-02426d005d3a",
"processDefinitionKey": "Process_0qcjqnm",
"processDefinitionVersionTag": null,
"processInstanceId": "0623bfdb-4837-11ec-8c7e-02426d005d3a",
"retries": null,
"suspended": false,
"workerId": null,
"topicName": "testingtopic",
"tenantId": null,
"priority": 0,
"businessKey": null
}
]
my request is:
POST http://localhost:8080/engine-rest/external-task/fetchAndLock
{
"workerId": 1,
"maxTasks": 100,
"topics": [
{
"topicName": "testingtopic",
"lockDuration": 100000,
"variables": {
"a": {
"value": 1,
"type": "long"
},
"b": {
"value": 2,
"type": "long"
},
"id": {
"value": 1,
"type": "long"
}
}
}
],
"asyncResponseTimeout": 5
}
my BPMN diagram is:
Sorry mistake was mine I mentioned wrongly in request body. I mentioned
"variables": {}
But it's a array of json "variables": []
I mentioned here just variable names "variables": ["a","b","id"]
Later I used POST /external-task/{id}/complete request to pass with values to complete the process

Unable to delete Apache Atlas Classification

I have a list of Classifications & Sub-classifications in Apache Atlas. Want to delete them & create a new list.
All the other classifications are getting deleted but one of them with name "PII" giving following error when we select Delete Classification.
Error: Given type PII has references
When we do a search via Rest API using below URL:
http://ip.of.atlas:21000/api/atlas/v2/search/basic?classification=PII
Following Result comes:
{
"queryType": "BASIC",
"searchParameters": {
"classification": "PII",
"excludeDeletedEntities": false,
"includeClassificationAttributes": false,
"includeSubTypes": true,
"includeSubClassifications": true,
"limit": 100,
"offset": 0
},
"entities": [
{
"typeName": "hive_table",
"attributes": {
"owner": "nifi",
"createTime": 1557832055000,
"qualifiedName": "demo.test_table#demopilot",
"name": "test_table"
},
"guid": "ecb7bb24-bdde-448c-b718-07273e5ce572",
"status": "DELETED",
"displayText": "test_table",
"classificationNames": [
"PII"
],
"meaningNames": [],
"meanings": []
},
{
"typeName": "hive_table",
"attributes": {
"owner": "nifi",
"createTime": 1557832055000,
"qualifiedName": "demo.test_table#demopilot",
"name": "test_table"
},
"guid": "ed5a9284-c290-4431-ab76-27b820478e29",
"status": "DELETED",
"displayText": "test_table",
"classificationNames": [
"PII"
],
"meaningNames": [],
"meanings": []
},
{
"typeName": "hive_column",
"attributes": {
"owner": "nifi",
"qualifiedName": "demo.test_table.traffic_case#demopilot",
"name": "traffic_case"
},
"guid": "73f75a6c-9f4e-41f0-b0ef-6c05ca132639",
"status": "DELETED",
"displayText": "traffic_case",
"classificationNames": [
"PII"
],
"meaningNames": [],
"meanings": []
}
]
}
Questions:
1. Is there a API which help to delete all Classifications irrespective of whether they are attached to Entity or not?
2. Delete Single Classification forcefully with Classification Name or GUID?
Running below GET request:
http://ip.of.atlas:21000/api/atlas/v2/types/typedefs
& then Delete the guid attached to the typedefs
I tested it out and you could use below API to delete a tag:
curl -k -X DELETE --insecure --negotiate -u : --header \
''{"classificationDefs":[{"name":"PII","superTypes":[],"attributeDefs":[]}]}' \
'https://atlas-host:21443/api/atlas/v2/types/typedefs?type=classification'

Azure Functions ARM template redeployment deletes my published functions

I have an Azure Functions (2.0) instance deployed by an ARM Template using Azure DevOps pipelines. I have another pipeline that deploys a functions application to the instance through zip deploy. This almost works perfectly, however, if I deploy the functions Infrastructure as Code, then deploy the app and then redeploy the Infrastructure as Code, my functions app is removed and all the functions disappear. I am using the incremental deployment so I cont see why it does this. Any thoughts on why it is behaving like this or how to troubleshoot?
I have copied my resource group deployment script and ARM template below.
New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $TemplateFile `
-TemplateParameterFile $TemplateParametersFile `
-Mode Incremental `
#OptionalParameters `
-Force -Verbose `
-AdminsGroup $AdminsGroup `
-AdminsGroupObjectId $AdminsGroupObjectId `
-ErrorVariable ErrorMessages
ARM template
{
"type": "Microsoft.Web/serverfarms",
"sku": {
"name": "Y1",
"tier": "Dynamic",
"size": "Y1",
"family": "Y",
"capacity": 0
},
"kind": "app",
"apiVersion": "2016-09-01",
"name": "[variables('FunctionPlanNameMyStuff')]",
"location": "[resourceGroup().location]",
"properties": {
"name": "[variables('FunctionPlanNameMyStuff')]",
"perSiteScaling": false,
"reserved": false,
"targetWorkerCount": 0,
"targetWorkerSizeId": 0
}
},
{
"comments": "MyStuff Functions Site",
"type": "Microsoft.Web/sites",
"kind": "functionapp",
"name": "[variables('FunctionSiteNameMyStuff')]",
"apiVersion": "2016-08-01",
"location": "[resourceGroup().location]",
"identity": {
"type": "SystemAssigned"
},
"scale": null,
"properties": {
"enabled": true,
"hostNameSslStates": [
{
"name": "[concat(variables('FunctionSiteNameMyStuff'),'.azurewebsites.net')]",
"sslState": "Disabled",
"virtualIP": null,
"thumbprint": null,
"toUpdate": null,
"hostType": "Standard"
},
{
"name": "[concat(variables('FunctionSiteNameMyStuff'),'.scm.azurewebsites.net')]",
"sslState": "Disabled",
"virtualIP": null,
"thumbprint": null,
"toUpdate": null,
"hostType": "Repository"
}
],
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('FunctionPlanNameMyStuff'))]",
"reserved": false,
"siteConfig": null,
"scmSiteAlsoStopped": false,
"hostingEnvironmentProfile": null,
"clientAffinityEnabled": false,
"clientCertEnabled": false,
"hostNamesDisabled": false,
"containerSize": 1536,
"dailyMemoryTimeQuota": 0,
"cloningInfo": null
},
"resources": [
{
"apiVersion": "2015-08-01",
"name": "appsettings",
"type": "config",
"dependsOn": [
"[resourceId('Microsoft.Web/Sites', variables('FunctionSiteNameMyStuff'))]",
"[resourceId('Microsoft.Storage/storageAccounts', variables('FunctionStorageAccountMyStuff'))]"
],
"properties": {
"AzureWebJobsStorage": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('FunctionStorageAccountMyStuff'), ';AccountKey=', listKeys(variables('FunctionStorageResourceIdMyStuff'), '2017-10-01').keys[0].value)]",
"AzureWebJobsDashboard": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('FunctionStorageAccountMyStuff'), ';AccountKey=', listKeys(variables('FunctionStorageResourceIdMyStuff'), '2017-10-01').keys[0].value)]",
"WEBSITE_CONTENTAZUREFILECONNECTIONSTRING": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('FunctionStorageAccountMyStuff'), ';AccountKey=', listKeys(variables('FunctionStorageResourceIdMyStuff'),'2015-05-01-preview').key1)]",
"WEBSITE_CONTENTSHARE": "[toLower(variables('FunctionSiteNameMyStuff'))]",
"FUNCTIONS_EXTENSION_VERSION": "~2",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"FUNCTION_APP_EDIT_MODE": "readwrite",
"KeyVaultUrl": "[concat('https://', variables('KeyVaultName'), '.vault.azure.net/')]",
"DeveloperMode": false
}
}
],
"dependsOn": [
"[resourceId('Microsoft.Web/serverfarms', variables('FunctionPlanNameMyStuff'))]",
"[resourceId('Microsoft.Storage/storageAccounts', variables('FunctionStorageAccountMyStuff'))]"
]
},
{
"comments": "Functions web site config",
"type": "Microsoft.Web/sites/config",
"name": "[concat(variables('FunctionSiteNameMyStuff'), '/', 'web')]",
"apiVersion": "2016-08-01",
"location": "[resourceGroup().location]",
"tags": {
},
"scale": null,
"properties": {
"numberOfWorkers": 1,
"defaultDocuments": [
"Default.htm",
"Default.html",
"Default.asp",
"index.htm",
"index.html",
"iisstart.htm",
"default.aspx",
"index.php",
"hostingstart.html"
],
"netFrameworkVersion": "v4.0",
"phpVersion": "5.6",
"pythonVersion": "",
"nodeVersion": "",
"linuxFxVersion": "",
"requestTracingEnabled": false,
"remoteDebuggingEnabled": false,
"remoteDebuggingVersion": null,
"httpLoggingEnabled": false,
"logsDirectorySizeLimit": 35,
"detailedErrorLoggingEnabled": false,
"publishingUsername": "[concat('$', variables('FunctionSiteNameMyStuff'), 'pubuser')]",
"publishingPassword": "[concat(variables('FnAppPublishingPasswordMyStuff'), uniqueString(resourceGroup().id))]",
"appSettings": null,
"metadata": null,
"connectionStrings": null,
"machineKey": null,
"handlerMappings": null,
"documentRoot": null,
"scmType": "None",
"use32BitWorkerProcess": true,
"webSocketsEnabled": false,
"alwaysOn": false,
"javaVersion": null,
"javaContainer": null,
"javaContainerVersion": null,
"appCommandLine": "",
"managedPipelineMode": "Integrated",
"virtualApplications": [
{
"virtualPath": "/",
"physicalPath": "site\\wwwroot",
"preloadEnabled": false,
"virtualDirectories": null
}
],
"winAuthAdminState": 0,
"winAuthTenantState": 0,
"customAppPoolIdentityAdminState": false,
"customAppPoolIdentityTenantState": false,
"runtimeADUser": null,
"runtimeADUserPassword": null,
"loadBalancing": "LeastRequests",
"routingRules": [],
"experiments": {
"rampUpRules": []
},
"limits": null,
"autoHealEnabled": false,
"autoHealRules": null,
"tracingOptions": null,
"vnetName": "",
"siteAuthEnabled": false,
"siteAuthSettings": {
"enabled": null,
"unauthenticatedClientAction": null,
"tokenStoreEnabled": null,
"allowedExternalRedirectUrls": null,
"defaultProvider": null,
"clientId": null,
"clientSecret": null,
"issuer": null,
"allowedAudiences": null,
"additionalLoginParams": null,
"isAadAutoProvisioned": false,
"googleClientId": null,
"googleClientSecret": null,
"googleOAuthScopes": null,
"facebookAppId": null,
"facebookAppSecret": null,
"facebookOAuthScopes": null,
"twitterConsumerKey": null,
"twitterConsumerSecret": null,
"microsoftAccountClientId": null,
"microsoftAccountClientSecret": null,
"microsoftAccountOAuthScopes": null
},
"cors": null,
"push": null,
"apiDefinition": {
"url": "[concat('https://', variables('FunctionPlanNameMyStuff'), '.azurewebsites.net/swagger/docs/v1')]"
},
"autoSwapSlotName": null,
"localMySqlEnabled": false,
"managedServiceIdentityId": null,
"ipSecurityRestrictions": null,
"http20Enabled": false,
"minTlsVersion": "1.0"
},
"dependsOn": [
"[resourceId('Microsoft.Web/sites', variables('FunctionSiteNameMyStuff'))]"
]
},
Add this app setting to your template:
{ "name": "WEBSITE_RUN_FROM_PACKAGE", "value": "1" }
Functions can only be run from zip file if this setting is present, and redeploying a template without it will remove existing value for this setting (if there was any).
So #curiouscoder's answer does work but it requires more than a one liner really so I'm expanding on it here.
I hit this issue after adding some CORS origins to my ARM template, when I ran the template it removed the functions, to fix it you need to do the following (this assumes a Windows based function app which yours is and so is mine and that you're deploying from Azure DevOps):
Add the WEBSITE_RUN_FROM_PACKAGE setting to your function's app service settings and set it to 1 (can also be set to a URL where the package is but that affects cold start up negatively for function apps)
Now confusingly in your Azure DevOps release pipeline set your deployment method to "zip deploy" and not to "run from package"!
Re-running your ARM template with changes will now not remove the site
The function app will now be running from the zip file rather from a deployment direct to the wwwroot folder, this means the wwwroot folder is made readonly. You'll get errors if you try to edit anything there:
The benefit of this approach is that you can't half deploy your app and having it read only means you can't accidentally mess with it once deployed.
The above does not work as smoothly as a slot change and will require the app to be restarted so it needs to be used in conjunction with a staging app or slots although as of writing the latter are still in preview for function apps.
The original github announcement is worth a read here as they're more detailed than the docs.
Your ARM template is problematic because it sets the App Settings as a separate action from the app creation, which is not supported when using Azure Files.
See this sample for guidance.

Zendesk API - Creating a ticket gives wrong ticket status

Zendesk API documentation states that when creating a ticket through the API, the status field can be set to "new" (https://developer.zendesk.com/rest_api/docs/core/tickets#create-ticket), yet if I call the API with the following command specifying a value for status, it creates a token yet still sets status to be the default value of open:
curl -u <username>/token:<api_token> https://<subdomain>.zendesk.com/api/v2/tickets.json -d '{"ticket": {"subject":"test subject", "comment": { "body": "test body" }, "status": "new" } }' -H "Content-Type: application/json" -v POST
The relevant part of the response body is:
"ticket": {
"url": "https://<subdomain>.zendesk.com/api/v2/tickets/60.json",
"id": 60,
"external_id": null,
"via": {
"channel": "api",
"source": {
"from": {},
"to": {},
"rel": null
}
},
"created_at": "2016-05-05T13:30:01Z",
"updated_at": "2016-05-05T13:30:01Z",
"type": null,
"subject": "test subject",
"raw_subject": "test subject",
"description": "test body",
"priority": null,
"status": "open",
"recipient": null,
"requester_id": 5917202647,
"submitter_id": 5917202647,
"assignee_id": 5917202647,
"organization_id": 3740197137,
"group_id": 28473587,
"collaborator_ids": [],
"forum_topic_id": null,
"problem_id": null,
"has_incidents": false,
"due_at": null,
"tags": [],
"custom_fields": [],
"satisfaction_rating": null,
"sharing_agreement_ids": [],
"fields": [],
"brand_id": 1090897,
"allow_channelback": false
}
Am I doing something wrong?
So it turns out that if you assign a ticket to a group with only one member/agent, the ticket automatically gets assigned to that lone agent. As such, any ticket that's already assigned to a specific agent, is automatically set to open instead of new.
Because I was using a test subdomain, I was the only agent and so it was auto-updating the status from "new" to "open".
Hmm, I made the same API call (with my creds/subdomain) and it worked, setting the "status" to any option I choose (new, pending, solved).
Have you confirmed that all your credentials and subdomain are correctly inputted? Also, check the API settings in your Zendesk account. Try creating a new token and Save the settings.
If the API token still doesn't work, maybe try authenticating with password or making the request by means other than curl.

Replace specific string in file which is not part of the search pattern but in follow up file

I have a config file like in following example.
{
"view": "pagelogfile",
"text": "menu_logFile",
"disabled": false,
"icon": null,
"cls": "treelevel2",
"iconCls": null,
"users": ["admin", "guest"],
"expanded": null,
"leaf": true,
"data": []
},
{
"view": "pagemyfeature",
"text": "menu_myFeature",
"disabled": false,
"icon": null,
"cls": "treelevel2",
"iconCls": null,
"users": ["admin", "guest"],
"expanded": null,
"leaf": true,
"data": []
}
Depending on a HardwareId certain config options are not available to certain users. For example the view: pagemyfeature is not available for user guest. I would have to remove the user "guest" from the "users array. Since the same array of users appears several time in the config file and i only want to change it in one place to "users": ["admin"], the only searchpattern i have is menu_myFeature to get in the correct config array for that menu. How to do the change in the subseqent line of this array?
This is how i want it to look like
{
"view": "pagelogfile",
"text": "menu_logFile",
"disabled": false,
"icon": null,
"cls": "treelevel2",
"iconCls": null,
"users": ["admin", "guest"],
"expanded": null,
"leaf": true,
"data": []
},
{
"view": "pagemyfeature",
"text": "menu_myFeature",
"disabled": false,
"icon": null,
"cls": "treelevel2",
"iconCls": null,
"users": ["admin"],
"expanded": null,
"leaf": true,
"data": []
}
Grepping for pagemyfeature and also string replacement with sed is not a problem. But how to make sure to replace the string 6 lines later on?
If you really want to do in sed, then try this. There many tools are available to work with json.
sed '/{/{:loop; N; /pagemyfeature/{:loop2; N; /users/s/,\? *"guest"//g; b loop2;}; /}/b; b loop;}' yourfile
More readable format:
/{/{
:loop;
N;
/pagemyfeature/{
:loop2;
N;
/users/s/,\? *"guest"//g; b loop2;
};
/}/b;
b loop;
}