Restful api custom types - api

So I am building a restful api allowing users to send events.
There are "standard" events like birthday, wedding, etc that each have their separate properties. So if someone sends data for type = birthday they can also specify the parameters date_of_birthday, new_age, etc. If they send type = wedding, for example, they have to specify different properties.
So basically when they make /event/create api call, they specify a type and a list of properties based on that type. If they want to specify a "custom" type they can. In that case the properties they specify are up to them.
How best to build this api so that it is true to rest?

To create an event, clients should make a POST request like
HTTP POST: /event
The post parameters should contain the event details and can be of any structure that the application understands.

Create events using POST, this request it self stands for "Create new" event. I would also suggest following structure of your API URIs:
POST /event/birthday //Create new birthday event
POST /event/wedding //Create new wedding event
...
generally:
POST /event/{event_name}
this will help you provide clear structure of the RESTful API. E.g. getting all event types can be retrieved with GET /event, getting all birthdays with GET /event/birthday, getting weddings on 1.1.2014 GET /event/wedding/1-1-2014 etc
Think of your resource URI structure in the same way, you will define a folders and files structure.

Related

Fetch data that is not a registered react-admin resource

Using react-admin, I have build an ExpressJS API including a GraphQL endpoint. All my resources in react-admin are using the graphql data provider. For a specific resource, I'm looking to fetch some configuration from my API endpoint to generate the form based on the context. I will try to create a mock example as I can't use the actual application's lingo.
For example, if I edit resource Post, and I assign a specific type "Photo", then I want some specific metadata to be saved in the form. My issue is the specific metadata object is coming from the API configuration for type "Photo". So if I select type "Photo", I want to save "Capture Date", "Camera Make", "Camera Model", etc.
The backend configuration would probably be in the form of a JSON schema describing the fields, the label, the key and the type of input (text or password).
TLDR: I want to fetch a configuration in the API and use it in the app to generate a list of form inputs. How can I do this without registering the call as a resource, since I don't feel like it is?
Edit
I haven't tried anything yet as I was asking a theoretical question to see if anybody had a "proper way" to suggest. My two options were the following:
Create a Resource for the PostType and it would simply return the list of fields as a JSONschema when called with GET_ONE. This would make use of the dataProvider to do the fetching.
Simply use fetch in my form and call my API endpoint directly.

Zapier Webhooks add data to default payload

I am setting up a Zap on Zapier and i have been trying to send a payload from Shopify(create order) to a Webhook which is sending data to an API. The problem i am having is adding data to the default payload(all data). I want to send all the data from the payload plus some additional attributes which i am configuring on the Data section, but this replaces the default-all data.
Is there a way to add a value to all the payload? I know i have the option to add the value by header or query string but i would like to add to the body instead. I am currently viewing the custom request but it seems complicated to configure the whole request to just add one value.
Thanks in advance.
David here, from the Zapier Platform team. This is a great question!
The short answer is that it's not possible to do quite what you want. You'll need to map each property into that data box like you're doing and add the custom properties you want.
That said, we track all feature requests that come in through tickets, so if you'd like to voice your support and get notified if/when this does get implemented, I'd suggest emailing in to contact#zapier.com.

Consuming TFS Web Hooks (Post via HTTP)

Is there a specific request format that the webhooks have? I am using the Pull Request Updated event, and I am trying to deserialize into a class, but I do not know what fields I will need. Is there a class or example of the request content details for the different events?
As far as I know, there isn't a generic template for the HTTP post request/response from TFS.
The TFS HTTP post JSON request/response format is based on the specific action.
For example, for the web hooks whenever a work item is updated.If I update a description field, I am getting one format and if I add a child work item, I am getting another format.
However for the specific event, you can check the response format, then get the useful information which you needed.
Please refer to Send JSON representation to a service for more information.
And at the end of the Q&A part there is a sample JSON.

Multiple endpoints to expose different views of the same resource

I'm struggling to find an answer to this (perhaps because I'm not asking the question properly) ...
I'm building API to expose resources for a basic social networking service I'm creating.
My understanding is that the structure of URLs in an API is essentially a hierarchy, directory like structure. I think that means I can have multiple endpoints to reach the same resources or collections of resource links.
For example:
I have an endpoint
www.domain.api.org/users/{:uid}/posts
Which will return all posts sent by the user or that the user is tagged in. Seems ok, but what if I have an endpoint such as:
www.domain.api.org/posts
Which when hit with a http GET will return all public posts (i.e. all users' posts plus his friends' and public posts).
The difference is that the first URL points to user owned resources whereas the second to public ones (of which the users posts are included of course)
Are these okay or am I doing it the wrong / less sensible way?
To reiterate, can I have multiple endpoints which point to different contexts/views of the same resource?
Basically multiple endpoints for the same resources should be avoided. However in this particular case it does make sense.
What you can do is to introduce optional query param userId to the following endpoint:
www.domain.api.org/posts/?userId=<userId>
If this substitutes the first endpoint you mentioned that's the way to go.
I would like to add ontop of #Opal's answer.
Are these okay or am I doing it the wrong / less sensible way?
Ideally, like Opal mentioned, you would use queryParams in your url. For many applications I have build, I don't know the uids returned from the api beforehand, so selecting an item and passing it inside my url as a query parameter makes sense. But it also has the added benefit of having your key inside your url, allowing you to bookmark it, pass the url to another user and they will automatically see the same data you want them to see.
To iterate: Is your current implementation wrong? No, but ideally you would use a combination of both route parameters are query parameters to achieve this
To create an endpoints, you have to be sure that you have these information at once:
Name of the endpoint
Status: activate or not (required) - is the endpoint activated or disable
Service profile (required) - ID of the Service Profile assigned to the endpoint.
Tariff profile (required) - ID of the tariff Profile assigned to the endpoint.
You can add another optional informations, and be sure of the structure of your endpoint.
Hope this helps you.

Passing params to POST API

I am new to designing REST APIs. I have trying to create following APIs for online game play
GET domain/api/games // return all games
POST domain/api/games // create a new game on behalf of current user
Now my issue is that when I try to create game using POST, I want userId to be sent to the API. I am not sure how to do this. Also note that I have another get API to get details of individual game, something like
GET domain/api/games/{gameId}
so I cannot pass userId to POST like domain/api/games/{useID} as it will conflict will above API.
So how do I pass usedId to POST. Also I don't want to use query params. Any suggestions to design this would be great.
When you are making a POST to a service, the parameters you communicate are known as BODY params, they don't go on the query string.
Different technologies have different APIs for interacting with POST params, but the underlying theory is the same, and is described by the W3C HTTP standard
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
The specifics of how to use POST params vary depending on the language and technology you're using. For example, if you are using jquery, there are a couple different ways to do it, with with the $.post('url', data, callback) method or with the $.ajax(...) option.
http://api.jquery.com/jquery.post/
http://api.jquery.com/jquery.ajax/
When reading POST params on the server, you'll generally access them using some some sort of request object, that will store your parameters in memory for you to access. This is highly dependent of the language and framework you're using, but here are links to some common ones:
NodeJS/express: http://expressjs.com/4x/api.html#request
PHP: http://php.net/manual/en/reserved.variables.post.php
ASP.Net: http://msdn.microsoft.com/en-us/library/system.web.httprequest.params(v=vs.110).aspx
Java/Spring: https://spring.io/guides/gs/handling-form-submission/
It should be either part of the context (you can pass it through header) or part of the game object. I prefer the context option, the httpheader can contain some auth bearer token so that you can figure out the user on the backend through the token.