Python will not patch json in ConnectWise. - python

I am a little confused why the following will not work.
I am connecting to our ConnectWise API via requests. I get the response and then I parse through to find the tickets I am looking for. I am then trying to PATCH the ticket to change certain information.
Sample json that is returned via API.
{
"id": 12345,
"summary": "[CompanyName][ComputerName] Disk Space Check - drive G:",
"recordType": "ServiceTicket",
"board": {
"id": 1,
"name": "Board1",
},
"status": {
"id": 5,
"name": "NewTicket",
},
"owner": {
"id": 1,
"identifier": "",
Once I have identified the ticket I need to work with. I try to patch it.
def assign_ticket(self, ticket):
add_resource = [
{'op': 'replace', 'path': '/board/name', 'value': 'Board2'},
{'op': 'replace', 'path': '/status/name', 'value': 'NewTicket2'},
{'op': 'replace', 'path': '/owner/identifier', 'value': 'MyBrainHurts'}
]
r = requests.patch(self.url + self.url_ticket + str(ticket), json=add_resource, headers=self.header)
print(r.status_code)
It returns a 200 status code to indicate everything completely correctly, but only the /owner/identifier field is updated. The other two are not. I have Google'd for several days and tried multiple variations of the code but I do not see why it will not change the board or status. Any ideas?

I've just checked some of my other code and I'm patching successfully using an array, but I'm using the ID of the resource rather than the name. Try that; instead of /status/name use /status/id and a numerical value.
What is the result of r.text? That should either return the configuration in its new, patched, form or it will tell you why the patch didn't work.

Related

Jira API : when using PUT call getting "no single-String constructor/factory method"

I was trying to update a custom field on a Jira ticket using Jira API, following this Jira Documentation. however, I am getting the below error.
{'errorMessages': ['Can not instantiate value of type [simple type, class com.atlassian.jira.rest.v2.issue.IssueUpdateBean] from JSON String; no single-String constructor/factory method']}
this is my code:
data = {'update': {'customfield_25305': [{'set': [{'value': '1c1a07d49af1b1cde8a1a7bd93cbbeef8efd50c9'}, {'value': 'c6f1e31ce0138cba658f769accaac729bebc42d6'}]}]}}
data = json.dumps(json.dumps(data)) #because the API accepts only strings enclosed in double quotes
upload = requests.put(url, headers=headers, data=data)
print(upload.json())
as per the documentation, I tried "/editmeta" the custom field I am trying to update is editable and has the following attributes.
{'required': False, 'schema': {'type': 'string', 'custom': 'com.atlassian.jira.plugin.system.customfieldtypes:textfield', 'customId': 25305}, 'name': 'Commit ID(s)', 'fieldId': 'customfield_25305', 'operations': ['set']}
Not sure what I am doing wrong here, any help would be appreciated!
Tried jira documentation
and searched through the Jira community none of the answers helped, everything points to malformed data but the data that I am passing is as per documentation.
the end result would be a 204 status code.
I think is a matter of single quotes.
As far as I remember, for valid JSON it should be double quotes.
{
"update": {
"customfield_25305": [{
"set": [{
"value": "1c1a07d49af1b1cde8a1a7bd93cbbeef8efd50c9"
}, {
"value": "c6f1e31ce0138cba658f769accaac729bebc42d6"
}]
}]
}
}
I use to do a online validator tool to my JSON strings when I got stuck.
E.g. https://jsonlint.com/ (first result on google)
I was able to resolve the issue, the proper formatting for data is as below:
{"fields": {"customfield_25305": "\"1c1a07d49af1b1cde8a1a7bd93cbbeef8efd50c9\", \"c6f1e31ce0138cba658f769accaac729bebc42d6\", \"1c1a07d49af1b1cde8a1a7bd93cbbeef8efd50c9\""}}

How to post an object to sonicwall api with python?

recently my admin asked me if I can create an app to input some data into our Sonicwall.
I found some API, created an account, and ran some "get" methods, which worked just fine. I am trying now to post a new object into our pv4 rules and cannot get over it.
The problem is when I write something like this:
def postIpv4Object(session):
body = {
"address_objects": [{
"ipv4": {
"name": "Test 1",
"zone": "LAN",
"host": {
"ip": "192.168.168.10"
}
}
}
]
}
resp = session.post(fw + '/api/sonicos/address-objects/ipv4', headers=good_headers,params=body, verify=False)
I am still getting this error:
{'status': {'info': [{'code': 'E_INVALID_API_CALL',
'level': 'error',
'message': 'Expected a request body.'}],
'success': False}}
I am reading docs but cannot really figure this out. Does anyone tried it and help me out a little?
After a couple of days of trying, I figured that "body" should be converted into JSON type, and instead of "params I need to use "data".

How can I add attributes to my product using woocommerce api in python

I find difficulty on how to pass attributes to a product using the woocommerce rest api.
I successfully took the attribute id.
my function:
def assign_attribute_to_products(wcapi_yachtcharterapp,post_id,attribute_id):
#the post_id argument is the product id
data = {
"attributes": [
{
"id": attribute_id,
},
],
}
wcapi_yachtcharterapp.put("products/"+str(post_id), data).json()
The product is updated without passing the information of attribute_id.
Any idea how to fix this?
You are missing some data, take a look here
This works for me:
data = {'attributes': [{'id': 7,
'options': ['term01', 'term02'],
'variation': 'true',
'visible': 'true'}]}
even though the data type for visible and variation is a boolean in the docs, you have to set 'true' or 'false' with a string.
You have to include the options for variations.
Hope this helps.

Create a Drive File with a set locale in international settings through Drive API

I need to create a file using the Google Drive API (I'm using v3, the latest at the moment). Using python if it matters.
My code is below,
drive_service.files().create(supportsTeamDrives=True, body={
'name': 'test-file',
'mimeType': 'application/vnd.google-apps.spreadsheet',
'parents': [folder_id],
'properties': {'locale': 'en_GB',
'timeZone': 'Europe/Berlin'}
})
Following the documentation #here, I tried to set the properties key with the locale set to the wanted one but it keeps creating the file with the default locale of my account.
How can I make it work at the creation time? is there another parameter I can fill?
Your problem is that you are mixing up two different "properties".
The properties you are setting are user-defined properties which are only ever consumed by you yourself. They are of no significance to Google.
The properties you want to set are part of the Spreadsheet API. See https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#SpreadsheetProperties
The simplest solution is to not use the Drive API to create your spreadsheet. Instead use the Spreadsheet API as descibed https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/create
I just tested this in the Apis Explorer
Create file Request
POST https://www.googleapis.com/drive/v3/files?key={YOUR_API_KEY}
{
"properties": {
"test": "test"
},
"name": "Hello"
}
Response
{
"kind": "drive#file",
"id": "1CYFI5rootSO5cndBD2gFb1n8SVvJ7_jo",
"name": "Hello",
"mimeType": "application/octet-stream"
}
File get request
GET https://www.googleapis.com/drive/v3/files/1CYFI5rootSO5cndBD2gFb1n8SVvJ7_jo?fields=*&key={YOUR_API_KEY}
Response
"kind": "drive#file",
"id": "1CYFI5rootSO5cndBD2gFb1n8SVvJ7_jo",
"name": "Hello",
"mimeType": "application/octet-stream",
"starred": false,
"trashed": false,
"explicitlyTrashed": false,
"parents": [
"0AJpJkOVaKccEUk9PVA"
],
"properties": {
"test": "test"
},
It appears to be working just fine i suggest you try checking the following:
The file id that is returned in the response from creating the file. To ensure that you are checking the one you just uploaded. Every time you run that you are going t create a new file.
Also remember to add fields=* with file.get if that's what you are using to check the result of your properties.

Fiware-orion Remove all entities of the same type

I dont know what command pass to the Fiware Orion API(v2) to remove all element of an specific type. In this example fake.
I tried this, but it doesn't works :
response = self.PM.request(
method='post', url='http://127.0.0.1:1026/v2/op/update', headers={'Content-Type': 'application/json'},
body=json.dumps({
"actionType": "delete",
"entities": [{
"idPattern": ".*",
"type": "Fake",
}]
}))
I can remove the elements by ID. It is only a work around, but tells me that I am connecting correctly to the API. This is the code that works:
response = self.PM.request(
method='post', url='http://127.0.0.1:1026/v2/op/update', headers={'Content-Type': 'application/json'},
body=json.dumps({
"actionType": "delete",
"entities": [{
"id": "Fake1",
},
{
"id": "Fake2",
}
]}))
According to the docs this feature does not exist. Also, if you search the source code for "delete type" I don't see anything that matches. You'd need to do a select first and iterate through all the ids deleting them.
You are using some client in python to acess Orion Broker?

Categories

Resources