How to respond for partial failure in micro-services gateway? - api

I am seeking your opinion on the following scenario. Imagine following microservices system.
Microservice - A: This does some work/task A
responseA: { key: value}
Microservice - B: This does some work/task B
responseB: { key: value}
Gateway/Aggregator: This invokes Microservices A & B in parallel and combines the results.
gatewayResponse: { responseA: { key: value}, responseB: {key: value}}
In an ideal scenario where both services are successful, then the above response "gatewayResponse" makes sense, i.e. HTTP STATUS CODE 200
My question is more regarding partial failure. Let's say service B is down or while executing a request it failed due to any reason, but service A is totally okay. In such a case how "gatewayResponse" should look like and what should be the HTTP STATUS CODE?
Solution - HTTP STATUS CODE 200 / should it be some other STATUS CODE??
gatewayResponse: {
responseA: {
key: value
},
responseB: {
error: {
"some details about error"
}
}
}
I would appreciate your response on this. Thank you in advance!

Related

How to design a payload for a POST REST-API call?

Assume I have a function in the backend server like this:
function get_bot_id(bot_name) { ... }
[A] I design a payload for a POST REST-API request like this:
{
"get_bot_id": "my_beautyful_bot"
}
[B] My co-workers claim that this payload below has more security.
{
"action": "get_bot_id",
"value": "my_beautyful_bot"
}
However, as for security claims, I don't see that [B] is better than [A].
I believe we just make the payload unnecessarily and larger without other benefits.
Does anyone have any opinions? Thank you!

kucoin websocket api, how to "subscribe" to their public channel, they say no authorization required, but they ask for a token :(

The question is about kucoin websocket public channel (not trades) just last trades
I just want a live feed of trades like other crypto exchanges...
but when I want to connect to "wss://ws-api-futures.kucoin.com/endpoint" I get WebSocketError: Received unexpected status code (401 Unauthorized)
the documentation https://docs.kucoin.com/futures/#create-connection lack explications :(
normally with other exchanges I can just do this in javascript
bybit_market_ws = new WebSocket("wss://stream.bybit.com/spot/quote/ws/v2");
bybit_market_ws.onmessage = event => bybit_trades(event.data);
bybit_market_ws.onopen = event => bybit_market_ws.send(JSON.stringify({"topic":"trade","params":{"symbol":"BTCUSDT","binary":false},"event":"sub"}));
function bybit_trades (jsonx) { console.log(JSON.parse(jsonx)); }
so how can I do that with kucoin websocket ?
according to the documentation i would need a "public token"...
but there is no explication on how to get that token :(
does someone knows how I would retrieve the last trades via websocket (public) channel ?
Note that the following steps may be changed when the API is updated.
All information can be found at https://docs.kucoin.com/#apply-connect-token
Get the public token
Send a empty http POST (GET will not work) message to https://api.kucoin.com/api/v1/bullet-public.
Response:
{
"code": "200000",
"data": {
"token": "2neAiuYvAU61ZD...",
"instanceServers": [
{
"endpoint": "wss://ws-api.kucoin.com/endpoint",
"encrypt": true,
"protocol": "websocket",
"pingInterval": 18000,
"pingTimeout": 10000
}
]
}
}
Connect to the Websocket
With the data of the repsonse above:
websocket: endpoint + "?token=" + token
Example: wss://ws-api.kucoin.com/endpoint?token=2neAiu....
Get all supported trading pairs
send a http GET message to https://api.kucoin.com/api/v1/symbols
{
"code": "200000",
"data": [
{
"symbol": "REQ-ETH",
"name": "REQ-ETH",
"baseCurrency": "REQ",
"quoteCurrency": "ETH",
...
},
{
"symbol": "BTC-USDC",
"name": "BTC-USDC",
"baseCurrency": "BTC",
"quoteCurrency": "USDC",
...
},
...
Get trading data
When the websocket connection is established send a http POST message:
{
"type": "subscribe", //subscribe or unsubscribe
"topic": "/market/ticker:BTC-USDT,BTC-USDC"
}
maybe this answer will not please you at all, but i will try, most of the people who work from the API in KuCoin do it with python, in fact the SDK for Nodejs is out of date, your best bet is to ask in the telegram channel https://t.me/KuCoin_API, there are KuCoin engineers who always help, although most of them use python, there is also the academy channel https://t.me/kucoin_learning, where there are examples, in short I can only mention references because I was also where you are, and the best I could do was that and review the SDk code and from there intuit and create my own adjustments
PD: the datafeed.js file is your best option, check it out https://github.com/Kucoin/kucoin-futures-node-sdk/blob/master/src/lib/datafeed.js

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?
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

JSON RPC Documentation Tool

Are there any Documentation Tools for JSON RPC API's?
I found a lot which are perfect for RESTful API's (Slate, Spotlight, Swagger) but sadly none suitable for JSON RPC API's.
Ideal would be a Tool that can handle both.
Are there any?
Thanks a lot!
Found a couple:
https://github.com/mzernetsch/jrgen
https://github.com/contributte/anabelle
Looking at using jrgen for my project.
Check this one: https://jsight.io
JSight has HTTP REST and also JSON-RPC 2.0 support, look here for details: https://jsight.io/docs/jsight-api-0-3-quick-tutorial
This is the JSON-RPC definition example from the site:
JSIGHT 0.3
URL /api/rpc
Protocol json-rpc-2.0
Method createCat // Create a cat.
Params
{
"cat": #cat
}
Result
{
"id": 1 // Cat’s id.
}
Method getCat // // Get a cat by its id.
Params
{
"id": 1 // Cat’s id.
}
Result
#cat
TYPE #cat
{
"id": 1,
"name": "Tom"
}
The same example in the cloud: https://editor.jsight.io/r/qjxRR6a/1

Uber sandbox'ed /requests endpoint uses production environment and call real drivers

This behavior started today (11/10/2015).
When I make a call to sandboxed Uber endpoint a real driver accept and comes.
I've spent 5 hours trying to understand what is happening.
I've been using Uber API for last 1.5 months and haven't had this issue before.
I make this request (now I do it with Postman, so seems like there is nothing else can affect it):
URL: https://sandbox-api.uber.com/v1/requests
Method: POST
Headers:
Authorization Bearer AvwkOYENiYRS... (Got it from Uber)
Content-Type: application/json
body:
{
"product_id": "04a497f5-380d-47f2-bf1b-ad4cfdcb51f2", // got from uber before that
"start_latitude":37.4237323,
"start_longitude":-122.09827279999999,
"end_latitude":37.4243272,
"end_longitude":-122.09491579999997
}
I receive successful response from Uber.
And after some time server start receiving events from Uber:
X-Environment: production
{
...
"event_type": "requests.status_changed",
"meta": {
"resource_id": "...", // resource_id that I get when requested sandboxed api
"status": "accepted" // and then arriving, etc
},
...
}
This was a bug on Uber's side. This was resolved this morning. Let us know if you still see the issue.