Vortac
Vortac
CC#
Created by Vortac on 1/25/2024 in #help
How to use Server Sent Events with HttpClient
I'm working with an API framework (https://docs.mistral.ai/api/#operation/createChatCompletion) in which you send a chat request. If you set the stream parameter to true:
tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message. Otherwise, the server will hold the request open until the timeout or until completion, with the response containing the full result as JSON.
tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message. Otherwise, the server will hold the request open until the timeout or until completion, with the response containing the full result as JSON.
To handle this, I created an SssEvent class with the following properties:
public string EventType { get; set; }
public string Data { get; set; }
public string EventType { get; set; }
public string Data { get; set; }
I have the code working for non-streaming calls, but I'm confused as to how to send my initial message and then stream back the response. My code currently looks like this: https://paste.mozilla.org/BD3gMX5o
6 replies
CC#
Created by Vortac on 1/24/2024 in #help
Interface Design Question
I have an interface IAuthenticatable with one method: Task<Tokens> GetAccessTokensAsync() that I'd like to use for two different API authentication classes. However, the two authenticated API responses vary, so I'm just wondering what I should do. Use separete interfaces for each API?
14 replies
CC#
Created by Vortac on 12/17/2023 in #help
✅ Streaming From REST API?
I'm building an SDK to interface with a REST API, and for one of then endpoints you can supply a boolean to specify if you want streaming or not. In particular, it mentions:
// Default: false
// Whether to stream back partial progress. If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message. Otherwise, the server will hold the request open until the timeout or until completion, with the response containing the full result as JSON.
// Default: false
// Whether to stream back partial progress. If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message. Otherwise, the server will hold the request open until the timeout or until completion, with the response containing the full result as JSON.
I'm just wondering, do I use HTTPClient for this? Or is there another way to handle streams?
15 replies
CC#
Created by Vortac on 11/25/2023 in #help
ASP .NET 7 - Testing a JWT Token Generator
I have an ASP .NET web api which calls a CreateToken() method inside a TokenService to generate a JWT when a user logs in. The public implementation looks like this:
public string CreateToken(ApplicationUser user)
{
var expiration = DateTime.UtcNow.AddMinutes(ExpirationMinutes);
var token = CreateJwtToken(
CreateClaims(user),
CreateSigningCredentials(),
expiration
);
var tokenHandler = new JwtSecurityTokenHandler();

_logger.LogInformation("JWT Token created");

return tokenHandler.WriteToken(token);
}
public string CreateToken(ApplicationUser user)
{
var expiration = DateTime.UtcNow.AddMinutes(ExpirationMinutes);
var token = CreateJwtToken(
CreateClaims(user),
CreateSigningCredentials(),
expiration
);
var tokenHandler = new JwtSecurityTokenHandler();

_logger.LogInformation("JWT Token created");

return tokenHandler.WriteToken(token);
}
There's also 3 private helper methods inside TokenService which are used to help generate the token. I should only be testing the public method correct? Also, what should I be testing for here? I've done basic unit tests before (like testing an AddNumbers(int 1, int 2)), but am I supposed to generate the token in the test and then compare it to a "correctly" generated version?
1 replies
CC#
Created by Vortac on 11/24/2023 in #help
✅ ASP Net 7 - Return list of objects as an object with a nested list
I have an endpoint with the following code:
public async Task<List<Page>> ListPages()
{
var pages = await _dbContext.Pages.ToListAsync();

return pages;
}
public async Task<List<Page>> ListPages()
{
var pages = await _dbContext.Pages.ToListAsync();

return pages;
}
This returns a list like:
[
{
"id": 1,
"title": "Hello ASP!",
"body": "<p>Testing</p>",
"author": "M"
},
{
"id": 2,
"title": "Second Page",
"body": "<p>Cool!</p>",
"author": "M"
}
]
[
{
"id": 1,
"title": "Hello ASP!",
"body": "<p>Testing</p>",
"author": "M"
},
{
"id": 2,
"title": "Second Page",
"body": "<p>Cool!</p>",
"author": "M"
}
]
I want to return:
```
{ "data":
[{
"id": 1,
"title": "Hello ASP!",
"body": "<p>Testing</p>",
"author": "M"
},
{
"id": 2,
"title": "Second Page",
"body": "<p>Cool!</p>",
"author": "M"
}]
}
```
{ "data":
[{
"id": 1,
"title": "Hello ASP!",
"body": "<p>Testing</p>",
"author": "M"
},
{
"id": 2,
"title": "Second Page",
"body": "<p>Cool!</p>",
"author": "M"
}]
}
```
6 replies
CC#
Created by Vortac on 11/20/2023 in #help
Dynamically enable/disable a route in ASP.NET 7?
Is there a way to dynamically enable/disable a route in ASP.NET 7? I'd like to be able to enable/disable a registration endpoint of an API depending on whether or not an admin selects.
17 replies