mirror of
https://github.com/Shchoholiev/shopping-assistant-web-client.git
synced 2025-04-12 01:48:49 +00:00
Merge pull request #9 from Shchoholiev/feature/SA-148-SSE-processing
SA-148 sse processing method created
This commit is contained in:
commit
e5496ccbfa
@ -0,0 +1,9 @@
|
|||||||
|
namespace ShoppingAssistantWebClient.Web.Models.Enums;
|
||||||
|
|
||||||
|
public enum SearchEventType
|
||||||
|
{
|
||||||
|
Wishlist = 0,
|
||||||
|
Message = 1,
|
||||||
|
Suggestion = 2,
|
||||||
|
Product = 3
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace ShoppingAssistantWebClient.Web.Models.Input;
|
||||||
|
|
||||||
|
public class MessageCreateDto
|
||||||
|
{
|
||||||
|
public required string Text { get; set; }
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
using ShoppingAssistantWebClient.Web.Models.Enums;
|
||||||
|
|
||||||
|
namespace ShoppingAssistantWebClient.Web.Models.ProductSearch;
|
||||||
|
|
||||||
|
public class ServerSentEvent
|
||||||
|
{
|
||||||
|
public SearchEventType Event { get; set; }
|
||||||
|
|
||||||
|
public string Data { get; set; }
|
||||||
|
}
|
@ -3,6 +3,9 @@ using GraphQL;
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using ShoppingAssistantWebClient.Web.Models.GlobalInstances;
|
using ShoppingAssistantWebClient.Web.Models.GlobalInstances;
|
||||||
|
using ShoppingAssistantWebClient.Web.Models.ProductSearch;
|
||||||
|
using System.Text;
|
||||||
|
using ShoppingAssistantWebClient.Web.Models.Enums;
|
||||||
|
|
||||||
namespace ShoppingAssistantWebClient.Web.Network;
|
namespace ShoppingAssistantWebClient.Web.Network;
|
||||||
|
|
||||||
@ -93,6 +96,50 @@ public class ApiClient
|
|||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async IAsyncEnumerable<ServerSentEvent> GetServerSentEventStreamed(string url, Object obj, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
await SetAuthenticationAsync();
|
||||||
|
|
||||||
|
var requestUrl = $"{_httpClient.BaseAddress}{url}";
|
||||||
|
var response = await _httpClient.PostAsJsonAsync(requestUrl, obj);
|
||||||
|
using var responseStream = await response.Content.ReadAsStreamAsync();
|
||||||
|
using var reader = new StreamReader(responseStream, Encoding.UTF8);
|
||||||
|
|
||||||
|
SearchEventType eventType = SearchEventType.Message;
|
||||||
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
var jsonChunk = await reader.ReadLineAsync(cancellationToken);
|
||||||
|
if (jsonChunk == null) continue;
|
||||||
|
if (jsonChunk.StartsWith("event: "))
|
||||||
|
{
|
||||||
|
var type = jsonChunk.Substring("event: ".Length);
|
||||||
|
switch(type)
|
||||||
|
{
|
||||||
|
case "Message":
|
||||||
|
eventType = SearchEventType.Message;
|
||||||
|
break;
|
||||||
|
case "Suggestion":
|
||||||
|
eventType = SearchEventType.Suggestion;
|
||||||
|
break;
|
||||||
|
case "Product":
|
||||||
|
eventType = SearchEventType.Product;
|
||||||
|
break;
|
||||||
|
case "Wishlist":
|
||||||
|
eventType = SearchEventType.Wishlist;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (jsonChunk.StartsWith("data: "))
|
||||||
|
{
|
||||||
|
yield return new ServerSentEvent
|
||||||
|
{
|
||||||
|
Event = eventType,
|
||||||
|
Data = jsonChunk.Substring("data: ".Length),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private MultipartFormDataContent MapIFormCollectionToForm(IFormCollection formCollection)
|
private MultipartFormDataContent MapIFormCollectionToForm(IFormCollection formCollection)
|
||||||
{
|
{
|
||||||
var formDataContent = new MultipartFormDataContent();
|
var formDataContent = new MultipartFormDataContent();
|
||||||
|
Loading…
Reference in New Issue
Block a user