About "PUT" and "DELETE" query - loopbackjs

I just started using loopback and I have 'Profile' model in which addresses are embedded
{
"addresses":
[{
"type": "office",
"street": "2",
"city": "Noida",
"state": "UP",
"country": "india",
"zipCode": "201301",
"id": "b6268532-96a9-427e-87a5-df04750af2a0"
},
{
"type": "home",
"street": "1",
"city": "Noida",
"state": "UP",
"country": "india",
"zipCode": "201301",
"id": "6812b958-afb2-4e91-bdde-0977545c2e46"
}
]
}
this is an array of address and i want to delete the record of "id:6812b958-afb2-4e91-bdde-0977545c2e46" and update the record of "id:b6268532-96a9-427e-87a5-df04750af2a0".
Used methods like destroyById, updateById,remove,updateAttributes and updateAll but nothing worked.
How can i perform this?
i am using loopback with mongodb.
code for delete
code for update
what is wrong in this code? and which other methods can be used instead?
Thankyou in advance.

Its very simple. You need to provide id with the url in 'delete' request.
For Example:
var req = url + "/" + id
//url is your loopback endpoint url like api/user
$.ajax({
type: 'delete',
contentType: "application/json",
contentLength: "0",
url: req,
//data: id,
traditional: true,
success: function (response) {
//your function after deleting record
}
});
Hope it works!

Related

Dynamics 365 API v2: Adding sales lines to an existing sales order

I am trying to add sales lines to an existing sales order using the standard API included with dynamics 365 business central. However I cannot find the correct syntax on the api request to accomplish this.
I have tried posting to the companies({id})/salesOrderLines endpoint, and the companies({id})/salesOrder({id})/salesOrderLines endpoint but no matter how I try to work my request I always get the "Invalid Request Body" error.
Here is an example of the request I sent to the companies({id})/salesOrderLines endpoint:
Here is my request body:
[
{
"id": "e92c39cb-f552-4d4f-b680-ad7ded2949d0",
"documentId": "e80573b0-9c8b-ed11-bfba-001dd8b71ee3",
"lineType": "Item",
"lineObjectNumber": "H10-110013",
"quantity": 1
},
{
"id": "e92c39cb-f552-4d4f-b680-ad7ded2949d0",
"documentId": "e80573b0-9c8b-ed11-bfba-001dd8b71ee3",
"lineType": "Item",
"lineObjectNumber": "H10-112117",
"quantity": 1
}
]
Here is the response I get back:
"{"error":{"code":"BadRequest","message":"Invalid Request
Body CorrelationId: 241e540a-5af5-4516-83f2-fbc035f80389."}}"
I am able to post a sales order and its lines simultaneously using deep requests but with larger orders they hit the request limit and I need a way of splitting up the lines.
salesOrderLines endpoint expects an object and does not accept collections, so best you can do here is post each line in a separate request.
Request body would look like this:
{
"lineType": "Item",
"lineObjectNumber": "H10-110013",
"quantity": 1
}
id and documentId can be added to the request, but not really required.
Another option is to send the request on the $batch endpoint as described in the docs here: https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/webservices/use-odata-batch
POST http://bc21-dev:7048/bc/api/v2.0/$batch
{
"requests": [
{
"method": "POST",
"url": "http://bc21-dev:7048/bc/api/v2.0/salesOrders(dd3585b3-dd6c-ed11-81b4-6045bd8e5172)/salesOrderLines",
"headers": {
"Company": "CRONUS International Ltd.",
"Content-Type": "application/json"
},
"body": {
"lineType": "Item",
"lineObjectNumber": "1920-S",
"quantity": 2,
"unitPrice": 420.4
}
},
{
"method": "POST",
"url": "http://bc21-dev:7048/bc/api/v2.0/salesOrders(dd3585b3-dd6c-ed11-81b4-6045bd8e5172)/salesOrderLines",
"headers": {
"Company": "CRONUS International Ltd.",
"Content-Type": "application/json"
},
"body": {
"lineType": "Item",
"lineObjectNumber": "1952-W",
"quantity": 1,
"unitPrice": 183.12
}
}
]
}

Printful django connection for creating orders

i want to make a link between django and printful via API to create an order within the printful store.
I am looking at the documentation site but I think it is done badly, could someone who is familiar with or has already done this thing could help me?
I'm not super expert in API and connections I'm trying to learn, so please if you can show me how you do it or link me some tutorials or useful resources you would do me a great favor
#login_required
def test(request):
# variabili per connessione
token = "**************************"
url = "https://api.printful.com/"
# dati ordine
{
"external_id": "4235234213",
"shipping": "STANDARD",
"recipient": {
"name": "John Smith",
"address1": "Via milano 55",
"city": "Milano",
"country_code": "IT",
"country_name": "Italy",
"zip": 20020,
"email": "email#gmail.com",
},
"items": [
{
"id": 1,
"quantity": 1,
"price": "13.00",
"name": "Enhanced Matte Paper Poster 18×24",
}
],
"retail_costs": {
"currency": "EUR",
"subtotal": "10",
"shipping": "5.00",
"total": "15"
},
}
header = {'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'}
test = requests.get(url + 'orders', headers = header)
print('---------------', test, '------------')
context = {'test': test.json()}

List users as non admin with custom fields

As per the documentation, I should be able to get a list of users with a custom schema as long as the field in the schema has a value of ALL_DOMAIN_USERS in the readAccessType property. That is the exact set up I have in the admin console; Moreover, when I perform a get request to the schema get endpoint for the schema in question, I get confirmation that the schema fields are set to ALL_DOMAIN_USERS in the readAccessType property.
The problem is when I perform a users list request, I don't get the custom schema in the response. The request is the following:
GET /admin/directory/v1/users?customer=my_customer&projection=full&query=franc&viewType=domain_public
HTTP/1.1
Host: www.googleapis.com
Content-length: 0
Authorization: Bearer fakeTokena0AfH6SMD6jF2DwJbgiDZ
The response I get back is the following:
{
"nextPageToken": "tokenData",
"kind": "admin#directory#users",
"etag": "etagData",
"users": [
{
"externalIds": [
{
"type": "organization",
"value": "value"
}
],
"organizations": [
{
"department": "department",
"customType": "",
"name": "Name",
"title": "Title"
}
],
"kind": "admin#directory#user",
"name": {
"fullName": "Full Name",
"givenName": "Full",
"familyName": "Name"
},
"phones": [
{
"type": "work",
"value": "(999)999-9999"
}
],
"thumbnailPhotoUrl": "https://photolinkurl",
"primaryEmail": "user#domain.com",
"relations": [
{
"type": "manager",
"value": "user#domain.com"
}
],
"emails": [
{
"primary": true,
"address": "user#domain.com"
}
],
"etag": "etagData",
"thumbnailPhotoEtag": "photoEtagData",
"id": "xxxxxxxxxxxxxxxxxx",
"addresses": [
{
"locality": "Locality",
"region": "XX",
"formatted": "999 Some St Some State 99999",
"primary": true,
"streetAddress": "999 Some St",
"postalCode": "99999",
"type": "work"
}
]
}
]
}
However, if I perform the same request with a super admin user, I get an extra property in the response:
"customSchemas": {
"Dir": {
"fieldOne": false,
"fieldTwo": "value",
"fieldThree": value
}
}
My understanding is that I should get the custom schema with a non admin user as long as the custom schema fields are set to be visible by all domain users. This is not happening. I opened a support ticket with G Suite but the guy that provided "support", send me in this direction. I believe this is a bug or maybe I overlooked something.
I contacted G Suite support and in fact, this issue is a domain specific problem.
It took several weeks for the issue to be addressed by the support engineers at Google but it was finally resolved. The behaviour is the intended one now.

Google People API detect merged contacts with syncToken - previousResourceNames not included

I am using the people API to allow users to create entities in my system from their google contacts, via the people API, and am storing the resourceName (i.e 'people/c7760106965272617307') to keep track of the google contact the entity was created from.
I want to be able periodically update the entities to match what is in google. i.e. if the contact updates the phone number the entity gets the updated phone number. So am a calling the list API passing the sync token to get the contacts that have changed since the last call. This works for updates, edits and deletes but I can't find a way to detect when two contacts have been merged in google contacts.
The docs state:
https://developers.google.com/people/api/rest/v1/people#Person.PersonMetadata
previousResourceNames[] Any former resource names this person has had.
Populated only for connections.list requests that include a sync
token.
So if I:
- Call the list API requesting a sync token
- Create Contact A and Contact B
- Call the list API passing the sync token, then I get just the two created contacts and a new sync token:
{
"resourceName": "people/c1465347538402693914",
"etag": "%EgcBAj0JPjcuGgQBAgUHIgxab0lZTFBvUU43bz0=",
"metadata": {
"sources": [
{
"type": "CONTACT",
"id": "1455f5d28afc531a",
"etag": "#ZoIYLPoQN7o=",
"updateTime": "2020-02-26T15:35:34.021Z"
}
],
"objectType": "PERSON"
},
"names": [
{
"metadata": {
"primary": true,
"source": {
"type": "CONTACT",
"id": "1455f5d28afc531a"
}
},
"displayName": "Contact A",
"familyName": "A",
"givenName": "Contact",
"displayNameLastFirst": "A, Contact"
}
]
},
{
"resourceName": "people/c4555919836853785218",
"etag": "%EgcBAj0JPjcuGgQBAgUHIgx2WmJHUUtjNTcxQT0=",
"metadata": {
"sources": [
{
"type": "CONTACT",
"id": "3f39e0f40cd35282",
"etag": "#vZbGQKc571A=",
"updateTime": "2020-02-26T15:35:44.056Z"
}
],
"objectType": "PERSON"
},
"names": [
{
"metadata": {
"primary": true,
"source": {
"type": "CONTACT",
"id": "3f39e0f40cd35282"
}
},
"displayName": "Contact B",
"familyName": "B",
"givenName": "Contact",
"displayNameLastFirst": "B, Contact"
}
}
If I then merge the two contacts, and then call the API passing the new sync token i get:
{
"resourceName": "people/c4555919836853785218",
"etag": "%EgcBAj0JPjcuGgQBAgUHIgxqNlFVYnIwaU9vVT0=",
"metadata": {
"sources": [
{
"type": "CONTACT",
"id": "3f39e0f40cd35282"
}
],
"deleted": true,
"objectType": "PERSON"
}
}
So TDLR; I can find out one of the contacts were deleted, but not that it was merged into another contact.
It seems like the previousResourceNames[] field would do exactly what I want, but I can't seem to make it return in the data, either on the try the API function on the docs:
https://developers.google.com/people/api/rest/v1/people.connections/list
or using the below nodjs code:
const service = google.people({version: 'v1', auth: authClient});
const result = await service.people.connections.list({
resourceName: 'people/me',
personFields: 'names,emailAddresses,phoneNumbers,metadata',
//requestSyncToken: true
syncToken: "insert token here"
});
console.info("Google Returned", JSON.stringify(result.data, null, 4));
I wonder if i need to grant extra scopes, or something else in the requested person fields.
Scopes Requested:
'https://www.googleapis.com/auth/contacts',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'

Delete record Libcloud (GoDaddy api)

I try to implement delete method for Record delate-record, but its my first time to use python and this api.
The GoDaddy API doesn't have a delete record method, so this functionality is not exposed in the driver.
https://developer.godaddy.com/doc#!/_v1_domains/recordReplace
The driver could offer the 'replace records in zone' method, which would allow you to fetch the current list of records, and then set the new list minus the record you want to remove. But that feature is not implemented and quite risky.
First,
Send a GET request to https://api.godaddy.com/v1/domains/{DOMAIN}/records
Then, Enumerate over all records of API Response (JSON Array) and prepare new data by removing the one that needs to be deleted.
API Response (SAMPLE)
[
{
"data": "192.168.1.1",
"name": "#",
"ttl": 600,
"type": "A"
},
{
"data": "ns1.example.com",
"name": "#",
"ttl": 3600,
"type": "NS"
},
{
"data": "#",
"name": "www",
"ttl": 3600,
"type": "CNAME"
},
{
"data": "mail.example.com",
"name": "#",
"ttl": 3600,
"priority": 1,
"type": "MX"
}
]
New Data (After deleting record) (SAMPLE)
[
{
"data": "192.168.1.1",
"name": "#",
"ttl": 600,
"type": "A"
},
{
"data": "ns1.example.com",
"name": "#",
"ttl": 3600,
"type": "NS"
},
{
"data": "#",
"name": "www",
"ttl": 3600,
"type": "CNAME"
}
]
Now,
Send a PUT request to https://api.godaddy.com/v1/domains/{DOMAIN}/records with new data.
The most important thing is how you identify the records in above array which needs to be deleted. This would not be a difficult task, assuming you have good programming skills.
I managed to worked around it in kind of a hacky - we had bunch of records we wanted to delete, doing it manually seemed weird so I added a Javascript into the Chrome Developer Console, running on an authenticated session from the DNS manage page:
function deleteGoDaddyRecords(recordId) {
$.ajax({
url: 'https://dcc.godaddy.com/api/v3/domains/<YOUR-DOMAIN.com>/records?recordId='+recordId,
type: 'DELETE',
success: function(result) {
console.log(result)
}
});
}
which let me use the same call the UI is calling when you ask to delete a record.
the only thing you need to provide is the AttributeUid which is not available with the public API, but it is in the front-end API:
https://dcc.godaddy.com/api/v2/domains/runahr.com/records
So I managed to create a script that will generate bunch of
deleteGoDaddyRecords('<RECORD-UUID>');
deleteGoDaddyRecords('<RECORD-UUID>');
copy & paste the generated script into the Developers Console and that solved it for now.
I hope GoDaddy will add a public DELETE endpoint to their API in the future :)