public async Task Current_ShouldReceiveCurrent() { using var client = CreateClient(); var nonSubs = new List <CalledBase> { client.OnTrade.SubscribeCalled(), client.OnVolume.SubscribeCalled() }; var subscribe = client.OnCurrent.SubscribeCalled(c => { TestOutputHelper.WriteLine(c.ToString()); }); await client.StartAsync(); var sub = new CurrentSubscription("Bitfinex", "BTC", "USD"); _ = client.Subscribe(sub); await subscribe.AssertAtLeastOnceAsync(TimeSpan.FromMilliseconds(500)); nonSubs.ForEach(s => s.AssertNever()); _ = client.Unsubscribe(sub); }
/// <summary> /// Get current storage account from azure subscription /// </summary> /// <returns>A storage account</returns> private CloudStorageAccount GetStorageAccountFromSubscription() { string CurrentStorageAccountName = CurrentSubscription.CurrentStorageAccountName; if (string.IsNullOrEmpty(CurrentStorageAccountName)) { throw new ArgumentException(Resources.DefaultStorageCredentialsNotFound); } else { WriteDebugLog(String.Format(Resources.UseCurrentStorageAccountFromSubscription, CurrentStorageAccountName, CurrentSubscription.SubscriptionName)); try { //The service channel initialized by subscription return(CurrentSubscription.GetCloudStorageAccount()); } catch (ServiceModel.CommunicationException e) { WriteVerboseWithTimestamp(Resources.CannotGetSotrageAccountFromSubscription); if (e.IsNotFoundException()) { //Repack the 404 error string errorMessage = String.Format(Resources.CurrentStorageAccountNotFoundOnAzure, CurrentStorageAccountName, CurrentSubscription.SubscriptionName); ServiceModel.CommunicationException exception = new ServiceModel.CommunicationException(errorMessage, e); throw exception; } else { throw; } } } }
public async Task Subscribe([QueueTrigger("twitch-channel-subscription", Connection = "TwitchChatStorage")] string msg, ILogger logger) { var twitchEndPoint = "https://api.twitch.tv/helix/webhooks/hub"; // could end up like configuration["Twitch:HubEndpoint"] //#if DEBUG var leaseInSeconds = 864000; // = 10 days //#endif var channelId = long.TryParse(msg, out var _) ? msg : await GetChannelIdForUserName(msg); var callbackUrl = new Uri(Configuration["EndpointBaseUrl"]); var payload = new TwitchWebhookSubscriptionPayload { callback = new Uri(callbackUrl, $"?channelId={channelId}").ToString(), mode = "subscribe", topic = $"https://api.twitch.tv/helix/streams?user_id={channelId}", lease_seconds = leaseInSeconds, secret = TWITCH_SECRET }; logger.LogDebug($"Posting with callback url: {payload.callback}"); var stringPayload = JsonConvert.SerializeObject(payload); logger.Log(LogLevel.Information, $"Subscribing to Twitch with payload: {stringPayload}"); using (var client = GetHttpClient(twitchEndPoint)) { var token = await GetAccessToken(); client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.AccessToken}"); var responseMessage = await client.PostAsync("", new StringContent(stringPayload, Encoding.UTF8, @"application/json")); if (!responseMessage.IsSuccessStatusCode) { var responseBody = await responseMessage.Content.ReadAsStringAsync(); logger.Log(LogLevel.Error, $"Error response body: {responseBody}"); } else { var sub = new CurrentSubscription { ChannelId = channelId, ChannelName = msg, ExpirationDateTimeUtc = DateTime.UtcNow.AddSeconds(leaseInSeconds).AddDays(-1) }; var repo = new CurrentSubscriptionsRepository(Configuration); await repo.AddSubscription(sub); } } }