Loopback js connector rest with async / await - async-await

I am using the loopback-connector-rest (version 5.2.0) in order to invoke a third party API.
Even though i successfully invoke the API, it seems that i cannot get the response using async await operation.
res = await app.models.coupon.invoke({...});
I couldn't find an example with async await in the current docs.
Am i missing something?
Thanks.
Update
As it turns out everything was working as expected. The problem arised from the fact that i was invoking the third party API inside an async loop using await Promise.all() but i was trying to get the promises before all promises got resolved/rejected.
Thanks everyone for your time.

Related

How to allow discord bot to respond to webhook. Python. Discord.py

I have a problem trying to get a webhook to interact with a bot
For some reason, the bot doesn't respond to commands initiated by the webhook
Is there a way to make sure it initiates?
It works normally otherwise
My command:
# registering new commands that can be called by the flask webhook
#client.command()
async def new_message_received(ctx, trade_hash: str = ''):
print(f'New Trade Received! {trade_hash}')
paxful_cogs = client.get_cog('Paxful_Cogs')
await paxful_cogs.new_trade_received(trade_hash=trade_hash)
await ctx.send(f'{trade_hash} executed.')
print(f'Trade Done Execution! {trade_hash}')
My webhooks are sent successfully, but the bot does not respond to them.
Bot Responds to Me, but not to webhook.
Okay, figured it out with a lot of help from the python discord server.
#client.event
async def on_message(message):
# Manually get the invocation context from the message
ctx = await client.get_context(message)
# Verify that the context has a command and can be used
if ctx.valid:
# Invoke the command using the earlier defined bot/client/command
await client.invoke(ctx)
https://i.gyazo.com/d23edb65efeeaa834c5de33d70f00484.png
Basically, we're overriding the on_message fx, checking to see if the message matches our prefix/command structure with ctx.valid and then forcing the command to be processed with client.invoke(ctx) regardless if it's from a bot/webhook or not.

Background processing on C# web api controller

I'm new on .NET technology and come into some problem. Currenlty i'm trying to build a REST API that handle long processing before sending the result to client.
What i'm trying to achieve is, i would like to do a background processing after receiving request from client. But, i would also like to send a response to client.
In short, it would be something like this.
Client Request -> Handled by controller ( doing some processing ) -> send response directly, ignoring the background that still running.
On Java, i can do this using Runnable Thread. How can i achieve this on C# Web API ?
Thank you.
In short, don't do this.
The job of an API is not to perform heavy duty, long running tasks.
You could simply let the API receive the request to perform something, then delegate that to another service. The API can then send a 200 response to show it received the request and maybe a URL to another resource which allows a user to track the progress.
The API needs to be available and responsive at all times. It needs to serve a number of users and if a number of them all request something that uses a lot of resources and takes a lot of time, chances are the API will simply go down and not serve anyone.
This is why you do not do such things in an API. Let other services do the heavy lifting.
Your api can call another async method and return 200/OK response without waiting for the request to complete.
You can learn more about async programing in c#.
static async Task Main(string[] args)
{
Console.WriteLine("coffee is ready");
var toastTask = MakeToastWithButterAndJamAsync(2);
async Task<Toast> MakeToastWithButterAndJamAsync(int number)
{
//Do something here.
}
}
This can be achieve this using loosed coupled architecture, by introducing service bus or blob storage, once you receive request in web api you can save it to blob/service bus and return acknowlegement response from web api. From service bus/blob storage use webjob/function/ durable function app to process the message using event.

Detect when client closes a connection from aiohttp request handler

I have a long running request during which I push data down to a client as it is received. However, the request requires some resources that are created server side that I'd like to clean up whenever the client disconnects. I've looked through the docs, but I can't seem to find a way to detect when that happens. Any ideas?
This isn't super obvious looking at the docs, but the key here is that the asyncio server will throw a CancelledError into handler coroutine when the connection is closed. You can catch the CancelledError wherever you wait for an asynchronous operation to complete.
Using this, I clean up after a connection with something like this:
async def passthrough_data_until_disconnect():
await create_resources()
while True:
try:
await get_next_data_item()
except (concurrent.futures.CancelledError,
aiohttp.ClientDisconnectedError):
# The request has been cancelled, due to a disconnect
await do_cleanup()
# Re-raise the cancellation error so the handler
# task gets cancelled for real
raise
else:
await write_data_to_client_response()

Multiple subscribe not working with enableProtocol:true

I am using atmosphere jersey with redis broadcaster.
When I keep enableProtocol:true in javascript, the first subscribe request is successful.
But when I send next subscribe request I get Continuation Frame warning. I tested on Google chrome. I have attached the snapshot.
What could the issue be?
It works when I keep enableProtocol:false. But then onDisconnect is not called in long-polling.
After some observation I found that the X-Atmosphere-tracking-id=0 in first request and in subsequent requests I get it as the tracking id of previous request.
How do I avoid this?
You can try by manually setting the tracking-id to 0 before each subscribe.
$.atmosphere.uuid = 0

How to test that async controller is really running async

I have created simple async controllers that call into async methods that then call PostAsync on the HttpClient to retrieve various REST service endpoints. All works well, but how can I test to insure that the controller is really calling a background thread and releasing the primary thread back to the pool? I want to insure that I do have all the async sweetness working correctly and that I am not inadvertently running synchronous methods despite all my work to make everything async.
I found that System.Threading.Thread.CurrentThread.IsThreadPoolThread will provide whether the current thread is still a threadpool thread or not.

Resources