Symfony/Silex dispatch events async - symfony

I've setup Silex to receive webhooks from, for example, Github. In this case Github sends all the pull requests to a specific Silex API endpoint. According to the Github best practices (https://developer.github.com/v3/guides/best-practices-for-integrators/#favor-asynchronous-work-over-synchronous):
GitHub expects that integrations respond within 10 seconds of
receiving the webhook payload. If your service takes longer than that
to complete, then GitHub terminates the connection and the payload is
lost.
We should send an answer directly to Github, and process the POST request later.
I've setup my code like this:
$app->post('/github/receive/pullreq', function (\Symfony\Component\HttpFoundation\Request $request) use ($app) {
$body = $request->request->all();
$app->after(function() use ($body) {
$listener = new \Api\Github\Events\PullRequestEvent();
$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
$dispatcher->addListener('github.pullrequest.made', array($listener, 'sendTestMail'));
$dispatcher->dispatch('github.pullrequest.made');
});
return new \Symfony\Component\HttpFoundation\Response('PR Received', 201);
});
And the listener class like this:
class PullRequestEvent extends Event
{
public function sendTestMail()
{
sleep(60);
// the message
$msg = "First line of text\nSecond line of text";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
mail("myemail#gmail.com","My subject",$msg);
}
}
I want to send the 201 response to Github first, and then execute the sendTestMail function. Is this possible with Silex events? If yes, how should I set this up?

Related

How can I ensure an API call response completes before an operation in Blazor WASM

I've scoured stackoverflow looking for ways to make synchronous API calls in Blazor WASM, and come up empty. The rest is a fairly length explanation of why I think I want to achieve this, but since Blazor WASM runs single-threaded, all of the ways I can find to achieve this are out of scope. If I've missed something or someone spots a better approach, I sincerely appreciate the effort to read through the rest of this...
I'm working on a Blazor WASM application that targets a GraphQL endpoint. Access to the GraphQL endpoint is granted by passing an appropriate Authorization JWT which has to be refreshed at least every 30 minutes from a login API. I'm using a 3rd party GraphQL library (strawberry-shake) which utilizes the singleton pattern to wrap an HttpClient that is used to make all of the calls to the GraphQL endpoint. I can configure the HttpClient using code like this:
builder.Services
.AddFxClient() // strawberry-shake client
.ConfigureHttpClient((sp, client) =>
{
client.BaseAddress =
new Uri(
"https://[application url]/graphql"); // GraphQL endpoint
var token = "[api token]"; // token retrieved from login API
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
});
The trick now is getting the API token from the login API at least every 30 minutes. To accomplish this, I created a service that tracks the age of the token and gets a new token from the login API when necessary. Pared down, the essential bits of the code to get a token look like this:
public async Task<string> GetAccessToken()
{
if ((_expirationDateTime ?? DateTime.Now).AddSeconds(-300) < DateTime.Now)
{
try
{
var jwt = new
{
token =
"[custom JWT for login API validation]"
};
var payload = JsonSerializer.Serialize(jwt);
var content = new StringContent(payload, Encoding.UTF8, "application/json");
var postResponse = await _httpClient.PostAsync("https://[login API url]/login", content);
var responseString = await postResponse.Content.ReadAsStringAsync();
_accessToken = JsonSerializer.Deserialize<AuthenticationResponse>(responseString).access_token;
_expirationDateTime = DateTime.Now.AddSeconds(1800);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
return _accessToken;
}
So, now I need to wire this up to the code which configures the HttpClient used by the GraphQL service. This is where I'm running into trouble. I started with code that looks like this:
// Add login service
builder.Services.AddSingleton<FxAuthClient>();
// Wire up GraphQL client
builder.Services
.AddFxClient()
.ConfigureHttpClient(async (sp, client) =>
{
client.BaseAddress =
new Uri(
"https://[application url]/graphql");
var token = await sp.GetRequiredService<FxAuthClient>().GetAccessToken();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
});
This "works" when the application is loaded [somewhat surprisingly, since notice I'm not "await"ing the GetAccessToken()]. But the behavior if I let the 30 minute timer run out is that the first attempt I make to access the GraphQL endpoint uses the expired token and not the new token. I can see that GetAccessToken() refreshes expired token properly, and is getting called every time I utilize the FxClient, but except for the first usage of FxClient, the GetAccessToken() code actually runs after the GraphQL request. So in essence, it always uses the previous token.
I can't seem to find anyway to ensure that GetAccessToken() happens first, since in Blazor WASM you are confined to a single thread, so all of the normal ways of enforcing synchronous behavior fails, and there isn't an asynchronous way to configure the FxClient's HttpClient.
Can anyone see a way to get this to work? I'm thinking I may need to resort to writing a wrapper around the strawberry FxClient, or perhaps an asynchronous extension method that wraps the ConfigureHttpClient() function, but so far I've tried to avoid this [mostly because I kept feeling like there must be an "easier" way to do this]. I'm wondering if anyone knows away to force synchronous behavior of the call to the login API in Blazor WASM, sees another approach that would work, or can offer any other suggestion?
Lastly, it occurs to me that it might be useful to see a little more detail of the ConfigureHttpClient method. It is autogenerated, so I can't really change it, but here it is:
public static IClientBuilder<T> ConfigureHttpClient<T>(
this IClientBuilder<T> clientBuilder,
Action<IServiceProvider, HttpClient> configureClient,
Action<IHttpClientBuilder>? configureClientBuilder = null)
where T : IStoreAccessor
{
if (clientBuilder == null)
{
throw new ArgumentNullException(nameof(clientBuilder));
}
if (configureClient == null)
{
throw new ArgumentNullException(nameof(configureClient));
}
IHttpClientBuilder builder = clientBuilder.Services
.AddHttpClient(clientBuilder.ClientName, (sp, client) =>
{
client.DefaultRequestHeaders.UserAgent.Add(
new ProductInfoHeaderValue(
new ProductHeaderValue(
_userAgentName,
_userAgentVersion)));
configureClient(sp, client);
});
configureClientBuilder?.Invoke(builder);
return clientBuilder;
}

Can I use Dart async/await for post requests in browser?

I am using this example https://webdev.dartlang.org/articles/get-data/json-web-service as a starting point to develop Dart app consuming API endpoints data:
void saveData() {
HttpRequest request = new HttpRequest(); // create a new XHR
// add an event handler that is called when the request finishes
request.onReadyStateChange.listen((_) {
if (request.readyState == HttpRequest.DONE &&
(request.status == 200 || request.status == 0)) {
// data saved OK.
print(request.responseText); // output the response from the server
}
});
// POST the data to the server
var url = "http://127.0.0.1:8080/programming-languages";
request.open("POST", url, async: false);
String jsonData = '{"language":"dart"}'; // etc...
request.send(jsonData); // perform the async POST
}
I see this as a traditional callback running when something happens. Here it is executed when the response is received.
Though, I want to try the other approach, like using Futures/Promises or async/await.
Is it possible to turn this example into any of these alternatives in browser?
If so, can you please show the example how it looks when implemented as a Future or async/await?
I concur with #Pacane about using the http package. It provides a much cleaner API for working with http requests, which allow you to easily use async/await.
However, you could write saveData using just the core libraries as follows (dartpad sample here: https://dartpad.dartlang.org/2ed9e39fd887b58532d42a70697ce9cd)
Future<Null> saveData() async {
var response = await HttpRequest.postFormData(
'http://127.0.0.1:8080/programming-languages',
{'language': 'Dart'});
print(response.responseText);
}

'Google_Exception' with message 'Cant add services after having authenticated'

I am working on a WP plugin with Google Analytics, using Oauth 2.0.
All of my authentication & data pulls work fine, with the exception of this one issue: the first time I get a new Google authorization code (ex: "4/-xbSbg...." ) & authenticate, then try to call a new Google_AnalyticsService() object, it tosses back the error:
'Google_Exception' with message 'Cant add services after having authenticated'
This is on line 109: http://code.google.com/p/google-api-php-client/source/browse/trunk/src/apiClient.php?r=258
Once I refresh the page that calls this code, it works fine - ie, the first branch of the check_login() is ok, but the authentication call is not working correctly.
You see that the code seems to be complaining because I DID authenticate first, and the message says I shouldn't do that. The comment & code really have me confused what my issue is (login code not very clean yet, I realize).
IMPORTANT NOTE: I am using the Google Auth for Installed Apps, and so we are asking for an auth code from user, and using that to obtain the auth token.
get_option(), set_option() & update_option() are WP native functions that are not a part of the problem
Here is my code:
class GoogleAnalyticsStats
{
var $client = false;
function GoogleAnalyticsStats()
{
$this->client = new Google_Client();
$this->client->setClientId(GOOGLE_ANALYTICATOR_CLIENTID);
$this->client->setClientSecret(GOOGLE_ANALYTICATOR_CLIENTSECRET);
$this->client->setRedirectUri(GOOGLE_ANALYTICATOR_REDIRECT);
$this->client->setScopes(array(GOOGLE_ANALYTICATOR_SCOPE));
// Magic. Returns objects from the Analytics Service instead of associative arrays.
$this->client->setUseObjects(true);
}
function checkLogin()
{
$ga_google_authtoken = get_option('ga_google_authtoken');
if (!empty($ga_google_authtoken))
{
$this->client->setAccessToken($ga_google_authtoken);
}
else
{
$authCode = get_option('ga_google_token');
if (empty($authCode)) return false;
$accessToken = $this->client->authenticate($authCode);
$this->client->setAccessToken($accessToken);
update_option('ga_google_authtoken', $accessToken);
}
return true;
}
function getSingleProfile()
{
$analytics = new Google_AnalyticsService($this->client);
}
}
You will need to move $analytics = new Google_AnalyticsService($this->client); inside function GoogleAnalyticsStats(), and preferably turn $analytics into a member variable.
class GoogleAnalyticsStats
{
var $client = false;
var $analytics = false;
function GoogleAnalyticsStats()
{
$this->client = new Google_Client();
$this->client->setClientId(GOOGLE_ANALYTICATOR_CLIENTID);
$this->client->setClientSecret(GOOGLE_ANALYTICATOR_CLIENTSECRET);
$this->client->setRedirectUri(GOOGLE_ANALYTICATOR_REDIRECT);
$this->client->setScopes(array(GOOGLE_ANALYTICATOR_SCOPE));
// Magic. Returns objects from the Analytics Service instead of associative arrays.
$this->client->setUseObjects(true);
$this->analytics = new Google_AnalyticsService($this->client);
}
...
Now, you can make calls to the analytics API from within getSingleProfile.

SignalR recording when a Web Page has closed

I am using MassTransit request and response with SignalR. The web site makes a request to a windows service that creates a file. When the file has been created the windows service will send a response message back to the web site. The web site will open the file and make it available for the users to see. I want to handle the scenario where the user closes the web page before the file is created. In that case I want the created file to be emailed to them.
Regardless of whether the user has closed the web page or not, the message handler for the response message will be run. What I want to be able to do is have some way of knowing within the response message handler that the web page has been closed. This is what I have done already. It doesnt work but it does illustrate my thinking. On the web page I have
$(window).unload(function () {
if (event.clientY < 0) {
// $.connection.hub.stop();
$.connection.exportcreate.setIsDisconnected();
}
});
exportcreate is my Hub name. In setIsDisconnected would I set a property on Caller? Lets say I successfully set a property to indicate that the web page has been closed. How do I find out that value in the response message handler. This is what it does now
protected void BasicResponseHandler(BasicResponse message)
{
string groupName = CorrelationIdGroupName(message.CorrelationId);
GetClients()[groupName].display(message.ExportGuid);
}
private static dynamic GetClients()
{
return AspNetHost.DependencyResolver.Resolve<IConnectionManager>().GetClients<ExportCreateHub>();
}
I am using the message correlation id as a group. Now for me the ExportGuid on the message is very important. That is used to identify the file. So if I am going to email the created file I have to do it within the response handler because I need the ExportGuid value. If I did store a value on Caller in my hub for the web page close, how would I access it in the response handler.
Just in case you need to know. display is defined on the web page as
exportCreate.display = function (guid) {
setTimeout(function () {
top.location.href = 'GetExport.ashx?guid=' + guid;
}, 500);
};
GetExport.ashx opens the file and returns it as a response.
Thank you,
Regards Ben
I think a better bet would be to implement proper connection handling. Specifically, have your hub implementing IDisconnect and IConnected. You would then have a mapping of connectionId to document Guid.
public Task Connect()
{
connectionManager.MapConnectionToUser(Context.ConnectionId, Context.User.Name);
}
public Task Disconnect()
{
var connectionId = Context.ConnectionId;
var docId = connectionManager.LookupDocumentId(connectionId);
if (docId != Guid.Empty)
{
var userName = connectionManager.GetUserFromConnectionId(connectionId);
var user = userRepository.GetUserByUserName(userName);
bus.Publish( new EmailDocumentToUserCommand(docId, user.Email));
}
}
// Call from client
public void GenerateDocument(ClientParameters docParameters)
{
var docId = Guid.NewGuid();
connectionManager.MapDocumentIdToConnection(Context.ConnectionId, docId);
var command = new CreateDocumentCommand(docParameters);
command.Correlationid = docId;
bus.Publish(command);
Caller.creatingDocument(docId);
}
// Acknowledge you got the doc.
// Call this from the display method on the client.
// If this is not called, the disconnect method will handle sending
// by email.
public void Ack(Guid docId)
{
connectionManager.UnmapDocumentFromConnectionId(connectionId, docId);
Caller.sendMessage("ok");
}
Of course this is from the top of my head.

Problem with null object reference in Url.Action in MVC3 project

I am trying to set up a mocking scenario for my payment processor on a web site. Normally, my site redirects to the processor site, where the user pays. The processor then redirects back to my site, and I wait for an immediate payment notification (IPN) from the processor. The processor then posts to my NotifyUrl, which routes to the Notify action on my payments controller (PayFastController). To mock, I redirect to a local action, which after a conformation click, spawns a thread to post the IPN, as if posted by the processor, and redirects back to my registration process.
My mock processor controller uses the following two methods to simulate the processor's response:
[HttpGet]
public RedirectResult Pay(string returnUrl, string notifyUrl, int paymentId)
{
var waitThread = new Thread(Notify);
waitThread.Start(new { paymentId, ipnDelay = 1000 });
return new RedirectResult(returnUrl);
}
public void Notify(dynamic data)
{
// Simulate a delay before PayFast
Thread.Sleep(1000);
// Delegate URL determination to the model, vs. directly to the config.
var notifyUrl = new PayFastPaymentModel().NotifyUrl;
if (_payFastConfig.UseMock)
{
// Need an absoluate URL here just for the WebClient.
notifyUrl = Url.Action("Notify", "PayFast", new {data.paymentId}, "http");
}
// Use a canned IPN message.
Dictionary<string, string> dict = _payFastIntegration.GetMockIpn(data.paymentId);
var values = dict.ToNameValueCollection();
using (var wc = new WebClient())
{
// Just a reminder we are posting to Trocrates here, from PayFast.
wc.UploadValues(notifyUrl, "POST", values);
}
}
However, I get an 'Object reference not set to an instance of an object.' exception on the following line:
notifyUrl = Url.Action("Notify", "PayFast", new {data.paymentId}, "http");
data.paymentId has a valid value, e.g. 112, so I'm not passing any null references to the Url.Action method. I suspect I have lost some sort of context somewhere by calling Notify on a new thread. However, if I use just notifyUrl = Url.Action("Notify", "PayFast");, I avoid the exception, but I get a relative action URL, where I need the overload that takes a protocol parameter, as only that overload gives me the absolute URL that WebClient.UploadValues says it needs.
When you are inside the thread you no longer have access to the HttpContext and the Request property which the Url helper relies upon. So you should never use anything that relies on HttpContext inside threads.
You should pass all the information that's needed to the thread when calling it, like this:
waitThread.Start(new {
paymentId,
ipnDelay = 1000,
notifyUrl = Url.Action("Notify", "PayFast", new { paymentId }, "http")
});
and then inside the thread callback:
var notifyUrl = new PayFastPaymentModel().NotifyUrl;
if (_payFastConfig.UseMock)
{
// Need an absoluate URL here just for the WebClient.
notifyUrl = data.notifyUrl;
}

Resources