her icon indicating copy to clipboard operation
her copied to clipboard

Problems with Endpoint that returns a JSON Array

Open McSlow opened this issue 13 years ago • 5 comments

Hi, i'm currently working with the Puppetdb-API (some configuration management software) which has several endpoints, all of them work great with HER, except one. Strange thing is, this single endpoint doesn't return anything that looks wrong.

This is the endpoint: http://docs.puppetlabs.com/puppetdb/1.5/api/query/v3/fact-names.html setup is this: Rails 4, Ruby 2, Puppetdb 1.5, Her 0.68 Model is this one:

class Factname
        include Her::Model
        collection_path '/v3/fact-names'
end

(all other models look similar and are using the same initializer, so same target URL, same API etc.)

If I call the endpoint manually with curl, it returns a valid json array:

curl -X GET http://localhost:8080/v3/fact-names 
[ "apache.config.ok", "apache.vhosts.list", "architecture", "augeasversion",  ...a lot more.... ] 

So, nothing unusual here. Only thing is that it is quite loing (7000 chars) in one single line, other endpoints return multi-line responses.

Now, when I fetch the relation ( for example in the rails console) I get the following:

2.0.0-p247 :001 > y Factname.all
TypeError: no implicit conversion of Symbol into String
        from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/her-0.6.8/lib/her/model/attributes.rb:22:in `delete'
        from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/her-0.6.8/lib/her/model/attributes.rb:22:in `initialize'
...

All other Endpoints from the puppetdb api ( for example /v3/nodes ) work fine:

  2.0.0-p247 :002 > y Node.all
  --- !ruby/object:Her::Model::Relation
  parent: !ruby/class 'Node'
  params: {}
  _fetch: !ruby/array:Her::Collection
  internal:
  - !ruby/object:Node
    metadata: {}
    response_errors: {}
    destroyed: false
    attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
      name: my.server.name
      deactivated:
      catalog_timestamp: '2013-11-05T12:05:23.334Z'
      facts_timestamp: '2013-11-05T12:05:22.913Z'
      report_timestamp:
      ---snip---

Most of them do return json-AOHs, AOAs, etc. while this one seems to be the only one that returns a plain (JSON-)array.

Is this a Bug or am I doing something wrong?

best regards, Björn

McSlow avatar Nov 05 '13 12:11 McSlow

Digged a bit deeper: This works, so problem has to be a bit deeper:

2.0.0-p247 :056 > y MultiJson.load(Faraday.get('http://localhost:8080/v3/fact-names').env[:body])
---
- apache.config.ok
- apache.vhosts.list
- architecture
- augeasversion
- bios_release_date
- bios_vendor
- bios_version
...

McSlow avatar Nov 05 '13 13:11 McSlow

What is your middleware stack? Can you paste your Her::API.setup code?

remi avatar Nov 05 '13 13:11 remi

Sure:

#
# Initializes the HER API to the PuppetDB API Interface
#
ssl_options = { verify: false }
Her::API.setup url: "http://localhost:8080", ssl: ssl_options, headers: {:accept =>  'application/json'} do |c|
  c.use Faraday::Request::UrlEncoded
  c.use Her::Middleware::DefaultParseJSON
  c.use Faraday::Adapter::NetHttp
end

``

McSlow avatar Nov 05 '13 13:11 McSlow

I think I know where the problem is. The model constructor expects to be called with an attributes hash as its single argument.

Since the data returned by the endpoint is an array of strings (instead of an array of hashes), it tries to build an array of Factname objects by passing the string as the argument (eg. Factname.new("apache.config.ok")).

Since Her is built to deal with attributes hash, what I suggest you do is overwrite the initialize method for the model and do something like this:

class Factname
  include Her::Model
  collection_path '/v3/fact-names'

  def initialize(fact)
    super(fact: fact)
  end
end

Calling Factname.all will now return an array of Factname objects with a single fact attribute.

Factname.all
# => [#<Factname fact="apache.config.ok">, #<Factname fact="apache.vhosts.list">, …]

Let me know if that works for you!

remi avatar Nov 05 '13 13:11 remi

2.0.0-p247 :050 > y Factname.all
--- !ruby/object:Her::Model::Relation
parent: !ruby/class 'Factname'
params: {}
_fetch: !ruby/array:Her::Collection
  internal:
  - !ruby/object:Factname
    metadata: {}
    response_errors: {}
    destroyed: false
    attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
      fact: apache.config.ok
  - !ruby/object:Factname
    metadata: {}
    response_errors: {}
    destroyed: false
    attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
      fact: apache.vhosts.list
  - !ruby/object:Factname
    metadata: {}
    response_errors: {}
    destroyed: false
    attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
      fact: architecture
  - !ruby/object:Factname
    metadata: {}
    response_errors: {}
    destroyed: false

You were absolutely right! Thanks a lot!!!

McSlow avatar Nov 05 '13 13:11 McSlow