LukeHagar / plex-api-spec

An open source Plex Media Server API Specification
MIT License
27 stars 8 forks source link

Adding Response format for GET /libraries/sections [getLibraries] #2

Closed johnhebron closed 5 months ago

johnhebron commented 5 months ago

Hi there! Unsure of the format you'd like here, but I found the spec and am hoping to use it to build a Ruby gem! Ty so much for your work! Please let me know if there are any changes that I should make before requesting a merge.

Intent

Add the response body for the getLibraries operationId.

Work completed

  1. Updated /pms/paths/libraries.yaml and added a content for the 200 response.

I crafted this content section using https://swagger-toolbox.firebaseapp.com/ and the raw body from a request to my local Plex server.

  1. Updated the dereferenced yaml file for pms

I did this using the command

swagger-cli bundle --dereference pms/pms-spec.yaml -t yaml -o plex-media-server-spec-dereferenced.yaml

Downstream Ruby SDK Generation Proof

To prove that the sdk generation is working and includes the new changes, I am building a ruby gem and using it in a test project.

Setup Test Ruby Project with Built Gem Files

  1. Build ruby gem files using the command:

    openapi-generator generate -i plex-media-server-spec-dereferenced.yaml -g ruby -o ../pms-api-ruby
  2. Reference built gem in test project Gemfile

I did this by creating a Gemfile as follows:

source 'https://rubygems.org'

gem 'openapi_client', path: '/path/to/pms-api-ruby'
  1. Include gem in project

In my project file, I include the built gem files as such:

# Load the gem
require 'bundler/setup'
require 'openapi_client'
  1. Call the getLibraries endpoint

Example Ruby code used:

OpenapiClient.configure do |config|
  config.host = '192.168.x.x:32400'
  config.debugging = true
  # Configure API key authorization: accessToken
  config.api_key['accessToken'] = 'xxxxxxxxxxxxxxxxxxxxxx'
end

api_instance = OpenapiClient::LibraryApi.new

begin
  section = LIBRARY_SECTIONS[0]
rescue OpenapiClient::ApiError => e
  puts "Exception when calling LibraryApi->get_libraries: #{e}"
end

Test Out With New Gem File Changes

Now, to test if the change (adding the response body) is reflected.

To do this, I ran the example ruby file above before and after the changes and compared the output.

Output before changes

Before the change, the output showed nil for the Data value and a status code of 200

D, [2024-01-18T15:23:33.968923 #30434] DEBUG -- : Calling API: LibraryApi.get_libraries ...
GET /library/sections HTTP/1.1

HTTP/1.1 200 OK
D, [2024-01-18T15:23:34.034092 #30434] DEBUG -- : API called: LibraryApi#get_libraries
Data: nil
Status code: 200

Output after changes

After the change, the output showed the expected body data for the Data value and a status code of 200

D, [2024-01-18T15:23:33.968923 #30434] DEBUG -- : Calling API: LibraryApi.get_libraries ...
GET /library/sections HTTP/1.1

HTTP/1.1 200 OK
D, [2024-01-18T15:23:34.034092 #30434] DEBUG -- : API called: LibraryApi#get_libraries
Data: #<OpenapiClient::GetLibraries200Response:0x000000015944d810 @media_container=# ...... shortened for brevity, but the data is here! >
Status code: 200
johnhebron commented 5 months ago

tagging @LukeHagar. Thank you!

LukeHagar commented 5 months ago

Hey @johnhebron, This is so fantastic to see, and Thank you so much for the PR!!!

A couple of things to note that may make it easier, I built out a tool for easily converting JSON responses to Open API schemas, I just throw my responses from my server in there and then we still get the example values populated in the schema as well.

Additionally, I am ALSO working on a ruby gem, and mine would also be auto-generated, but it would be through the AMAZING platform Speakeasy has built, that I am a huge fan of so far.

I welcome input and feedback on that as it becomes available, which will be hosted in this repo

I'm going to make a small change to this PR, just to adjust the formatting and add the examples, and them I'm going to merge it in.

LukeHagar commented 5 months ago

And after fixes to my fixes (lol)

Its live: https://plexapi.dev/docs/plex/get-libraries