Building a RESTful API with ASP.NET Core 3 Flashcards
Is rest a standard? is it protocol agnostic?
NO. an arch style… it is protocol agnostico
What are the 6 contraintaints (design decisions) that REST has?
1 - Uniform Interfaces: API and consumers share one single technical interface: API;
2 - Client-Server: They are completelly separated;
3 - Statelessness: state is contained within the request;
4 - Layered system: layers can be added or removed in a transparent way to other layers;
5 - Cacheable: each RESPONSE message must explicitly state if it can be cached or not (ETag, lat-modified and etc)
6 - Code on demand (optional): server can extend client functionality (usually applicable for webapps).
What is the payload?
Is the data that is sent and/or retrieved along with the request/response.
What is the Richardson Maturity Model (RMM) and how it relates to REST?
It is a set of maturity levels that dictates how much you actually use from the REST standards, or how mature it is.
What is the level 0 - (swamp of POX - plain old XML) of RMM?
You use a single endpoint with a set of statefull calls to achieve a goal; usually using xml
What is the level 1 - Resources of RMM?
Each resource is mapped to a URI; using post verb only
What is the level 2 - Verbs of RMM?
Correct HTTP verbs and status codes are used;
What is the level 3 - Hypermedia of RMM? Which benefit does it bring?
The API supports Hypermedia as the engine of the application state (HATEOAS). It brings links to other actions/resources (discoverability).
Which API level can be considered a precondition to a RESTful api?
Level 3.
Can the URI api/authors be followed by another noun?
NO. should be followed with an ID.
Where to add filters, sorting orders?
As a query string.. api/customer?orderby=name
What a simple controller must have to behave like an endpoint?
data anotaded as [APIController], extending ControllerBase, defining the Route[(“api/authors”)] data anotation to the controller and defining the data anotation verb to the action [HTTPGet]
What is a convention-based routing? When is it used the most?
endpoints are added to actions on a controller following a convention (explicitly defined). usually used for web applications.
What is attribute-based routing?When is it used the most?
The routing is defined via the combinations of the controller name and data anotations in the action methods inside the controllers. used the most for APIs
How to interact with resources via HTTP methods?
https://pasteboard.co/JstVwBx.png
How to create two http gets to the same source with disambiguation?
[HTTPGet(“{authorId:guid}”)]
[HTTPGet(“{authorId:int}”)]
What are the 3 most important 200 status codes which verb uses it?
200 - Ok - Get, Success
201 - Ok - Post, Created
204 - Ok - Delete, No content
What are the 9 most important 400 status codes?
400 - Bad request - Generic
401 - Unauthorized (authentication issues)
403 - Forbidden (authorization issues)
404 - not found
405 - method not allow (e.g send a post to the authors endpoint)
406 - not acceptable (the payload response format asked is not supported)
409 - Conflict (used to handle concurrency issues: the data has been changed by someone else)
415 - same as 406, but the issue is with the REQUEST payload
422 - unprocessable entity - semantic mistakes (validation)…
What is the most comon 500 hundred status code?what does it mean?
internal error - something bad happened to the server - try again
What is the difference between errors and faults?
errors are correctly thrown when something is wrong with the request. and faults means that the API failed to process a correct request.
What is the benefits of using Ok() instead of new JasonResult()?
Ok is more readable and deals with other formats of data other than the jason
What formatters and content negotiation mean?
the client can demand the response in a specific format, and the server can deman the request in a specific format
How to proper handle unsupported formats in a way that the api returns a 406 error?
via services.addcontroller(s => s.ReturnHttpNotAcceptable = true);
how to add xml as a supported fortmat in asp?
s.addcontroller().addxmldatacontractserializerformatters();
What outer facing (DTO) vs entity model means?
means that our internal entity model should NOT be directly exposed to the external world. instead we should use outer facing models and translate into them via automapper
Should I always return IActionResult? What’s better for things like swagger?
return ActionResult>
What to use to map entity properties to dto object?
AutoMapper
Should i simply install automapper? why not?
not… install automapper.extensions.microsoft.dependencyinjection because it plays better with aspnet DI system
Where mappings are stored? How to add custom mappings (aka projections)?
in the profiles folder, create one for each dto object… inheriting the dto object from map and doing a forMember (if custom)
How to handle exceptions in the API in a customized way othen than just return a status code 500?
by doing an app.UseExceptionHandler(appbuilder => {… appbuilder.run…})
What is HEAD verb used for? when is it particularly useful?
HEAD is used to check if a resource exists, equal to get but without body. Very useful when ETags and other similar multi-use validations are used
Can I verb/route the same method twice on aspnet? how?
yes… just add extra verbs
[httpget]
[head]
Where the data from the request can come from?
body, form, header, query, route and service
Can data from the request be found by inferring types? What is the default source when we work with primitive types?
yes. query string (FromQuery)
What is the difference between filtering and searching?
Filtering means the consumer define what are the filters to apply via query string. Search is simply a term that the api defines on how to search it.
Are only the fields part of the request filterable?
yes
What to do when you need to receive many inputs via query string? What happens to the inferred query string? How to overcome this?
It’s best to create a complex type (class) to handle this. The inferred query string will no longer work. Solved by add a [FromQuery] data annotation.
Will the same DTO always be used for all verbs?
No. it’s common to have different dtos for get and post, for example.
Will the BaseController deal automatically with wrong conversions from the request body to the complex types?
Yes. will return 400’s return code
During a post, how to express the location where the resource is created? Where the location appears? How to refer to existing endpoints without concatenating manually URIs?
by returning a CreatedAtRoute(location, bodypayload). by naming the endpoints [httpget(“{asd}”, name = “asd123”)
What is the first check that needs to be done in the API when a consumer wants to add a resource that is child of another one?
check if the parent resource (defined in the url) exists, otherwise 404
What to do when you want to post authors along with its courses in one go?
The authorDTO must contain in its definition an ICollection of courseDTO (initialized) and the repository needs to also be also to handle that (needs to be able to create guid for the author and for the courses when added… entity framework takes care of the rest.