Project

General

Profile

Fetching additional data with REST api

Added by Jan Vacek almost 11 years ago

Hello,

I have two servers with redmine. One of them should be able to fetch information from the other one. I use REST API for it, but I can't get all information with it. I use it like this:

addr = "https://www.redmine.com/issues/" + params[:my_id] + ".json" 
@log = "OK"
begin
res = RestClient.get addr, :accept => :json, :params => { :key => key }, :timeout => 5, :open_timeout => 5
rescue => e
@log = "error"
end

It is working fine, but I don't get all the information. In one plugin I am using, there is migration of database, which add some columns to the issues table. There is following line for example:

add_column :issues, :country, :string

so I have information about country for all issues.

But using my previous request, I don't receive information about country.

I also tried to modify my request according [[http://www.redmine.org/projects/redmine/wiki/Rest_api]]. It means that it looks like this:

addr = "https://www.redmine.com/issues/" + params[:my_id] + ".json?include=country"

but it returns error.

Do I have to enable this feature somehow? Is there some better approach to fetch all the information? Do I have to modify that previous plugin somehow?


Replies (3)

RE: Fetching additional data with REST api - Added by Jan Vacek almost 11 years ago

Ok, I found out, that the best way is to modify REST API. So I decided to overwrite this file:

/app/views/issues/show.api.rsb

But there is a problem with different plugin which overwrites this file as well. I placed hook in there, so that it looks like this:

call_hook(:my_hook, {:api => api, :issue => @issue})

But when I try to use this hook, it does not respond at all. It behaves the same way all the time.

I requered hooks.rb in the init.rb and I added this to hooks.rb:

render_on :my_hook, :partial => 'my_plugin/rest_api_patch'

And I created file _rest_api_patch.api.rsb with this content:

api.array :extra_values do
api.new_value :country => @issue.country
end if include_in_api_response?('extra_values')

If I put that directly to the show.api.rsb file, it works nice, but when I try it with this hook, it does not react at all.

Do you have any idea what can cause that error? Is it possible to use hooks in this case?

Thank you very much.

RE: Fetching additional data with REST api - Added by Andrew Reshetov over 3 years ago

Hope that answer will be helpful for someone because I couldn't find decision in another place.
To implement for API hook need to pass api variable with another name (api will be rewrote on partial rendering)
Example:
In main API rsb

call_hook(:my_hook, {:root_api => api, :issue => @issue})

then in hook partial
root_api.array :foo do
  @bar.each do |bar_object|
    root_api.bar_value bar_object.value
  end
end

Regards

RE: Fetching additional data with REST api - Added by Txinto Vaz about 3 years ago

Hi, I have been a looong time trying and trying until I got it working.
I will rewrite the procedure again, for people like me, which are not so used to rails/redmine/plugin programming science.

First stage

Let's say I have a new issue attribute called "identifier", added to the attributes of the issue instances

I can add it to the Issue show REST API by just adding:

  api.identifier @issue.identifier

to the app/views/issues/show.api.rsb, just below the line
  api.subject @issue.subject

Second stage

I can copy this app/views/issues/show.api.rsb to plugins/myplugin/app/views/issues/show.api.rsb and undo the change done in app/views/issues/show.api.rsb and it works too.
Now the redmine code is not hacked, but I am overwriting all the API from my plugin.

Third stage

Now I want to use a simple hook patch, like the one we are discussing here and in #15986, which seems to me a more elegant solution.

Step by step, from the Redmine root folder.

1. nano app/views/issues/show.api.rsb
2. Add call_hook(:my_hook, {:root_api => api, :issue => @issue}) where? at any place? I added it near to the end of the file, just before the last end as the #15986 does.
3. nano plugins/myplugin/init.rb (replace 'myplugin' with your plugin name)
4. Add require_dependency 'path/to/my_hook' (replace 'path/to' with the path to your hook code file, starting from your plugin lib folder).
5. nano plugins/myplugin/lib/path/to/my_hook.rb

module Myplugin
  module Hooks
    class MyHook < Redmine::Hook::ViewListener
      render_on :my_hook, :partial => 'issues/rest_api_patch'
    end
  end
end

6. nano plugins/myplugin/app/views/issues/_rest_api_patch.api.rsb
root_api.array :extra_values do
    root_api.identifier @issue.identifier
end

And it works for me!

    (1-3/3)