ucwa

How can I properly send a batch request using UCWA 2.0?


I am writing a UCWA application to receive each user's presence and note. At the moment, I subscribe to all my desired contacts, I initiate my event stream and then I receive around 200 events. I loop through them to receive my contacts presence and notes using a for loop, meaning I send around 100 requests, which, according to Microsoft documentation, can drain battery on mobile devices or impact performance. I would like to use batching to fix this problem.

onEvent(events) {
    for (var i in events) {
        const event = events[i]
        switch (event.link.rel) { // 250 events filtered down to around 100
            case 'contactPresence':
            case 'presence':
                this.setPresence(event.link.href, this.getUser(event))
                break
            case 'contactNote':
            case 'note':
                this.setNote(event.link.href, this.getUser(event))
                break
            case 'presenceSubscription':
                ...
                break
        }
    }
}

After searching through Microsoft's documentation, I couldn't find any help on how to format a batch request. I tried following one of the examples provided, but I received a 400 error like this:

{
    "code":"BadRequest",
    "message":"Your request couldn\u0027t be completed."
}

Eventually I tried sending a batch following formatting that I've seen from this post, like so:

batch() {
    const boundary = Date.now()
    fetch(this.hub + this.response._links.batch.href, {
        method: 'POST',
        headers: {
            Accept: 'multipart/batching',
            Authorization: `Bearer ${this.token}`,
            'Content-Type': `multipart/batching;boundary=${boundary}`
        },
        body: `--${boundary}\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET ${this.response._links.self.href + '/people/contacts'} HTTP/1.1\r\nAccept: application/json\r\nHost: ${this.hub}\r\n\r\n--${boundary}--`
    }).then(r => r.json())
    .then(data => console.log(data))
}

Here is the request payload:

--1557482296198
Content-Type: application/http; msgtype=request

GET /ucwa/oauth/v1/applications/103357029549/people/contacts HTTP/1.1
Accept: application/json
Host: https://webpoolam41e02.infra.lync.com

--1557482296198--

This returns a 500 error, however, like this:

{
    "code":"ServiceFailure","message":"Your request couldn\u0027t be completed.",
    "debugInfo":{
        "errorReportId":"8d6499597a54443495627bd2b3e3c5b6"
    },
    "reasonId":"1000005"
}

I have spent a long time searching for an answer but I cannot find one that works.

Does anyone know how to properly format a batch request?


Solution

  • I have found an answer to my own question. It turns out that the final batch requires 3 line breaks:

    \r\n\r\n\r\n
    

    Rather than 2:

    \r\n\r\n