Comments (19)
@Fivell I do have plans to support file uploads, but at this point I've done no work towards that goal.
from jsonapi-resources.
Similarly what's the standard for overriding a crud action and allowing other parameters. I have an object which takes file upload which obviously is using multipart form-data so I don't want to use the default controller action.
from jsonapi-resources.
I think one approach that has potential would be to check the Content-Type header. If the Content-Type is application/vnd.api+json
you could treat the request as a JSONAPI request, and otherwise you could process it as a file upload.
The content type is not currently being enforced through the controller, but #69 is almost ready.
Once #69 is merged you could try some code like (I have not tested this, so no promises):
class PostsController < JSONAPI::ResourceController
skip_before_filter :setup_request, only: [:create]
skip_before_filter :ensure_correct_media_type, only: [:create]
def create
if request.content_type == JSONAPI::MEDIA_TYPE
setup_request
process_request_operations
elsif request.content_type == your type here
#Handle file upload
else
raise JSONAPI::Exceptions::UnsupportedMediaTypeError.new(request.content_type)
end
end
end
from jsonapi-resources.
@lgebhardt Did you start working with it? I need so much this feature! 😄
from jsonapi-resources.
Would it be possible to use all the goodness (attributes validation, errors, etc) of jsonapi-resources for different methods?
I have a publish
method which behaves very similar to the update
method, but it validates a different set of attributes and sets some other extra info in the model.
How can I use the process_request_operations
which validates that the attributes are correct, the error, the response, etc, but behaving like the update
method? or even better calling a different method than update_attributes
/save
(eg publish
)
from jsonapi-resources.
You're often better to recast those non-RESTful actions as REST resources. In the case of a publish
method, consider exposing a publication
resource that has an associated article
or document
that gets 'published' when the publication
is created.
from jsonapi-resources.
@lgebhardt how we solved this @envoy for file uploading is we have an endpoint that only accepts a Content-Type: multipart
for the body of the file and other values and the Accept: application/vnd+jsonapi
as the response format. This is well within the ability for JsonAPI content negotiation.
from jsonapi-resources.
Have you tried adding the method to your controller?
from jsonapi-resources.
@lgebhardt yeah, I have. I'm thinking about stuff like what if my controller action raises an exception, it would be nice to have a way to make it render errors in the same format as jsonapi-resources, etc...
from jsonapi-resources.
You could reuse the handle_exceptions
method. For example:
class FoosController < JSONAPI::ResourceController
def bar
#handle bar stuff
rescue => e
handle_exceptions(e)
end
end
You would need to make sure all your exceptions are derived from JSONAPI::Exceptions::Error
, or you could override the handle_exceptions
class to account for your custom exceptions.
from jsonapi-resources.
Hi @xdmx @adamworrall ,
I have similar use case as @xdmx. The hack I have is, instead of resitfy actions into resources, add a virtual attribute on the model say command
. When I wish to "publish", i would do the following with Ember Data:
model.set('command', 'publish');
model.save()
then in rails, I would use a before_validation
, or before_update
call back to dispatch to the right method do all the work and reset command
back to nil. This way, I can check whether the command
is valid given the current state of the model and leverage all the goodness in rails/jsonapi-resource/ember-data to handle the usual errors and updates. And if you want to give additional parameters to the special action/command, just add another virtual attr to the model, e.g. command_params
(this is almost equivalent to a rpc call).
I find this approach a bit dirty but workable especially given the constraints of Ember data. We don't have to hack around Ember data model urls, pushpayload, and create additional resource files.
What do you think?
from jsonapi-resources.
@lgebhardt , are there some plans to support other media types ( in case of upload ) officially ?
from jsonapi-resources.
related json-api/json-api#246
from jsonapi-resources.
@lgebhardt, any update on this?
from jsonapi-resources.
@Fivell Sorry, I still haven't looked into uploads.
from jsonapi-resources.
@gigorok No, I haven't started any work on non CRUD operations. Have you tried modifying your controller to handle this? See my above suggestion on switching based on the content type.
from jsonapi-resources.
@warmwaffles actually per the spec, it requires client to request application/vnd+jsonapi
as the content-type as well. I was going to do that, but it's also out of spec.
from jsonapi-resources.
@TrevorHinesley it's not out of spec, it's the whole part of HTTP content negotiation.
Clients MUST send all JSON API data in request documents with the header
Content-Type: application/vnd.api+json
without any media type parameters.
If the request does not have a json api in the content-type, then it will not be parsed as such. However if the client requests that the response be Json Api, we should honor that. This is a grey area of the spec, and should be left alone honestly. Allows more freedom.
from jsonapi-resources.
@warmwaffles ahh good point. I stand corrected--that's great! Thanks a ton. Makes my life easier haha.
from jsonapi-resources.
Related Issues (20)
- Resource option `always_include_linkage_data: true` not working HOT 5
- Repeating the same sort attribute with reverse direction overwrites the previous direction for that attribute
- Documentation for ResourceSerializer with includes is incorrect HOT 3
- What's the status of version 0.11.0?? IS THIS PROJECT DEAD? HOT 1
- Alternative to ActiveRelationResource which does not produce extra DB queries HOT 2
- Adopt the `frozen_string_literal` magic comment to optimize string storage
- sort by country that is related to resource not directly but through the third relation is not work properly HOT 3
- Rails Namespace Conflict with jsonapi.rb
- unable to build w/o specifying rails version in ENV
- add support for rubocop
- Test for missing inverse relationships HOT 4
- Change ResourceIdentity <=> to take resource_klass into account when sorting.
- Class caching is too aggressive in dev HOT 1
- Aliased Attribute Names Are Not Used In Error Responses
- get_join_arel_node fails with include_optional_linkage_data if there is already a join
- RFC: Guide for handling generic filter/fieldset errors, such as empty inputs and 'undefined'
- Polymorphic Relationships Not Found On Aliased Resource HOT 1
- Basic Usage guide includes deprecated Faker method HOT 1
- Problem with deprecated config value: `default_processor_klass`. HOT 1
- `:unprocessable_entity` deprecated in newest Rack, and causing a `0` response
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonapi-resources.