REST API vs. the Ruby API

AI-generated summary

In a discussion titled ‘REST API vs. the Ruby API’ under the Dev category, kimardenmiller asked whether the Ruby API fully mirrors the REST API, noting that certain functionality—like updating a user’s group notification level—was missing. After experimenting, kimardenmiller found that adding custom methods directly into the Ruby API’s codebase worked but felt messy. simon confirmed that the Ruby API is not yet feature-complete, citing the discourse_api README, and suggested either reopening the relevant module to extend it or submitting a pull request to contribute the missing functionality upstream. simon later provided a working code example demonstrating how to extend the DiscourseApi::API::Notifications module with a set_group_user_notification_level method. kimardenmiller confirmed the module-reopening approach worked and ultimately submitted a pull request to add the endpoint and an example script to the official gem, aiming to set users’ group notification levels to the group default. The thread highlights best practices for extending Ruby APIs and contributing back to open-source projects.

Is it safe to assume there is not a 1 to 1 mapping, i.e. the Ruby API does not do everything the REST API can do?

I did not see a way to update a user’s group notification level, so tried to write the method:

Am I on the right track here? If so, where do I place the method so it has access to the post method? Any examples I could reference on how this works, as I’m somewhat new to RoR.

Ok, looks as if placing my method in the api code and calling thought the client works:

response = client.set_group_user_note_lev(@group_name, user['id'], 2)
puts response

Though the response did not seem to print as it does in Postman, e.g.:

{
  "success": "OK"
}

It seems messy adding my code to the published Ruby API’s code. Best practices?

From the discourse_api README:

Over time this project intends to have a full Discourse API. At the moment there are only a few endpoints available

Reopening the module where you are placing your code and adding your method there might work. Have a look at the second example here: modules_and_classes - Documentation for Ruby 2.3.0. Another option would be to make a pull request to have your code added to the gem.

Since I posted about reopening the module, I figured I should try it out with your method to make sure it’s possible. Adding this code to a file and running it from the command line is working for me.

require 'discourse_api'

module DiscourseApi
  module API
    module Notifications
    	def set_group_user_notification_level group, user_id, notification_level
    		post("/groups/#{group}/notifications?user_id=#{user_id}&notification_level=#{notification_level}")
    	end
    end
  end
end

client = DiscourseApi::Client.new("http://localhost:3000")
client.api_key = "<your-api-key>"
client.api_username = "system"

response = client.set_group_user_notification_level 'running', 1, 3

puts "Response: #{response}"

Thanks for that guidance on reopening modules, which worked for me too. But in the end I submitted a pull request to add the endpoint & an example script to update users Group Notifications to the Group Default.

https://github.com/discourse/discourse_api/pull/167