Failing at API dev

Yeah we're all bliksems lazy, and driving more than 2kms is just a hack :p

But same as @Kosmik , hit me up via PM for a coffee sometime.

I'm not all into the Dev space, but I can over complicate your life with Infrastructure architecture and governance/compliance crap if you want to have your head implode :D
Oh don't mention governance, popia is a headache of note, especially when we arent in the exact path between Customer and Business.
 
Yeah we're all bliksems lazy, and driving more than 2kms is just a hack :p

But same as @Kosmik , hit me up via PM for a coffee sometime.

I'm not all into the Dev space, but I can over complicate your life with Infrastructure architecture and governance/compliance crap if you want to have your head implode :D
We can play with python and then the python programming code.



iirc you are in the Hillcrest area?
 
Oh don't mention governance, popia is a headache of note, especially when we arent in the exact path between Customer and Business.

Fsck popia... bloody little chihuahua. GDPR is 100x worse because its actually enforced.

We can play with python and then the python programming code.



iirc you are in the Hillcrest area?

I claim to live in lower Kloof :P
 
Hillcrest aka Chillcrest. You guys up on that mountain are in a different world. It's lekker there. Have an aunt living there.
 
Fire away with questions at any time :).

Chances are you will get a "WTF, I dunno wtf you're trying to do" but hey...
Then quick one, what is the best to use in your experience in formatting eg:

Python:
print("This is a string {}".format("xxx"))

#or

print(f"This is a string {}")
 
Honest answer, I dunno. I never work on formatting strings at all.. I just get data and then fsck around with it in other places.

My generalised "coding" answer would be use whichever method suits the entire use case better.
 
Honest answer, I dunno. I never work on formatting strings at all.. I just get data and then fsck around with it in other places.

My generalised "coding" answer would be use whichever method suits the entire use case better.

I have one which has me a bit confused on that regard. On the API topic. I've seen someone return API results as such. No action results nothing. Any thoughts on this?

C#:
// POST api/v1/identity/login
[HttpPost(ApiRoutes.Identity.Login)]
public async Task<AuthenticationResult> Login([FromBody] UserLoginRequest request)
{
    return await _identityService.LoginAsync(request.Email, request.Password);
}

C#:
public class AuthenticationResult
{
    public string Token { get; set; }

    public string RefreshToken { get; set; }

    public bool Success { get; set; }

    public IEnumerable<string> Errors { get; set; }
}
 
I have one which has me a bit confused on that regard. On the API topic. I've seen someone return API results as such. No action results nothing. Any thoughts on this?

C#:
// POST api/v1/identity/login
[HttpPost(ApiRoutes.Identity.Login)]
public async Task<AuthenticationResult> Login([FromBody] UserLoginRequest request)
{
    return await _identityService.LoginAsync(request.Email, request.Password);
}

C#:
public class AuthenticationResult
{
    public string Token { get; set; }

    public string RefreshToken { get; set; }

    public bool Success { get; set; }

    public IEnumerable<string> Errors { get; set; }
}
Fugly. The method will return with a 200 OK if successful but will break otherwise. The api controller will handle by default as a object to return.
 
Fugly. The method will return with a 200 OK if successful but will break otherwise. The api controller will handle by default as a object to return.

It didn't look right yeah. Well I'm going with a bit of a mixture here. I'm really just trying to get a template/design going at this point that I can use project wide. As always, criticism/feedback great appreciated!

Also regard to model validation. I don't know if inputting a simple string email like this is correct. The validation for fields like that could be better I think.

C#:
// GET /api/v1/users/find-by-email/{email}
[AllowAnonymous]
[HttpGet(ApiRoutes.Users.FindByEmail)]
public async Task<IActionResult> FindByEmail(string email)
{
    try
    {
        if(email == null)
        {
            return BadRequest(new RequestResult<User>
            {
                Success = false,
                Errors = new[] { "Required input missing." }
            });
        }

        var user = await _userManager.FindByEmailAsync(email);

        if (user == null)
        {
            return NotFound(new RequestResult<User>
            {
                Errors = new[] { "No records found." }
            });
        }

        return Ok(new RequestResult<User>
        {
            Data = user
        });
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, ex.Message);

        return StatusCode(StatusCodes.Status500InternalServerError, new RequestResult<User>
        {
            Success=false,
            Errors = new[] { "An unexpected error occurred.  Please try again later." }
        });
    }
}

C#:
public class RequestResult<T>
{
    public T Data { get; set; }
    public bool Success { get; set; } = true;
    public IEnumerable<string> Errors { get; set; }
}
 

in your example above, you will never have "email == null", because your route won't match.

the only way for that to happen would be if the route was defined as `/api/v1/users/find-by-email/{email?}` (notice the ? after email)

Code:
        if(email == null)
        {
            return BadRequest(new RequestResult<User>
            {
                Success = false,
                Errors = new[] { "Required input missing." }
            });
        }

        var user = await _userManager.FindByEmailAsync(email);

        if (user == null)
        {
            return NotFound(new RequestResult<User>
            {
                Errors = new[] { "No records found." }
            });
        }

        return Ok(new RequestResult<User>
        {
            Data = user
        });

this is really confusing.

so if the response is a 404, it is still considered a "success"?
 
I just feel like there is a lot of reinventing the wheel going on :)

want validation errors? .NET already supports that nicely.
want to handle unexpected exceptions in your controllers? .NET already provides hooks to handle them and return a consistent response, without handling them in every controller method
 
Top
Sign up to the MyBroadband newsletter
X