Giter Club home page Giter Club logo

Comments (19)

lgebhardt avatar lgebhardt commented on July 29, 2024 5

@Fivell I do have plans to support file uploads, but at this point I've done no work towards that goal.

from jsonapi-resources.

7h3kk1d avatar 7h3kk1d commented on July 29, 2024 2

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.

lgebhardt avatar lgebhardt commented on July 29, 2024 2

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.

gigorok avatar gigorok commented on July 29, 2024 2

@lgebhardt Did you start working with it? I need so much this feature! 😄

from jsonapi-resources.

xdmx avatar xdmx commented on July 29, 2024 1

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.

adamworrall avatar adamworrall commented on July 29, 2024 1

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.

warmwaffles avatar warmwaffles commented on July 29, 2024 1

@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.

lgebhardt avatar lgebhardt commented on July 29, 2024

Have you tried adding the method to your controller?

from jsonapi-resources.

hderms avatar hderms commented on July 29, 2024

@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.

lgebhardt avatar lgebhardt commented on July 29, 2024

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.

gniquil avatar gniquil commented on July 29, 2024

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.

Fivell avatar Fivell commented on July 29, 2024

@lgebhardt , are there some plans to support other media types ( in case of upload ) officially ?

from jsonapi-resources.

Fivell avatar Fivell commented on July 29, 2024

related json-api/json-api#246

from jsonapi-resources.

dmitry-sinina avatar dmitry-sinina commented on July 29, 2024

@lgebhardt, any update on this?

from jsonapi-resources.

lgebhardt avatar lgebhardt commented on July 29, 2024

@Fivell Sorry, I still haven't looked into uploads.

from jsonapi-resources.

lgebhardt avatar lgebhardt commented on July 29, 2024

@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.

TrevorHinesley avatar TrevorHinesley commented on July 29, 2024

@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.

warmwaffles avatar warmwaffles commented on July 29, 2024

@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.

TrevorHinesley avatar TrevorHinesley commented on July 29, 2024

@warmwaffles ahh good point. I stand corrected--that's great! Thanks a ton. Makes my life easier haha.

from jsonapi-resources.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.