From d35c40cf7bc9c0ba59a438f97f1811166aea713f Mon Sep 17 00:00:00 2001 From: Niels Swimberghe <3382717+Swimburger@users.noreply.github.com> Date: Thu, 20 Jun 2024 21:30:36 +0300 Subject: [PATCH] Add user agent --- .fernignore | 5 ++++ assemblyai.gemspec | 2 +- lib/gemconfig.rb | 9 +++--- lib/requests.rb | 15 ++++++---- lib/user_agent.rb | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 lib/user_agent.rb diff --git a/.fernignore b/.fernignore index 862e2a3..50b8fc8 100644 --- a/.fernignore +++ b/.fernignore @@ -6,6 +6,11 @@ test/ .rubocop.yml .github Gemfile +assemblyai.gemspec + +lib/gemconfig.rb +lib/requests.rb +lib/user_agent.rb lib/assemblyai/files/client.rb diff --git a/assemblyai.gemspec b/assemblyai.gemspec index b970352..9334e48 100644 --- a/assemblyai.gemspec +++ b/assemblyai.gemspec @@ -4,7 +4,7 @@ require_relative "lib/gemconfig" Gem::Specification.new do |spec| spec.name = "assemblyai" - spec.version = "1.0.0" + spec.version = AssemblyAI::Gemconfig::VERSION spec.authors = AssemblyAI::Gemconfig::AUTHORS spec.email = AssemblyAI::Gemconfig::EMAIL spec.summary = AssemblyAI::Gemconfig::SUMMARY diff --git a/lib/gemconfig.rb b/lib/gemconfig.rb index 7e272bd..b32d58b 100644 --- a/lib/gemconfig.rb +++ b/lib/gemconfig.rb @@ -2,11 +2,12 @@ module AssemblyAI module Gemconfig - VERSION = "" + VERSION = "1.0.0" AUTHORS = [""].freeze - EMAIL = "" - SUMMARY = "" - DESCRIPTION = "" + EMAIL = "support@assemblyai.com" + SUMMARY = "AssemblyAI Ruby SDK" + DESCRIPTION = "The AssemblyAI Ruby SDK provides an easy-to-use interface for interacting with the AssemblyAI API, which supports async, audio intelligence models, as well as the latest LeMUR models. +The Ruby SDK does not support Streaming STT at this time." HOMEPAGE = "/~https://github.com/AssemblyAI/assemblyai-ruby-sdk" SOURCE_CODE_URI = "/~https://github.com/AssemblyAI/assemblyai-ruby-sdk" CHANGELOG_URI = "/~https://github.com/AssemblyAI/assemblyai-ruby-sdk/blob/master/CHANGELOG.md" diff --git a/lib/requests.rb b/lib/requests.rb index 21576bf..3a6e5f6 100644 --- a/lib/requests.rb +++ b/lib/requests.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative "environment" +require_relative "user_agent" require "faraday" require "faraday/retry" require "async/http/faraday" @@ -16,21 +17,23 @@ class RequestClient # @return [String] attr_reader :default_environment + # @param api_key [String] # @param environment [AssemblyAI::Environment] # @param base_url [String] # @param max_retries [Long] The number of times to retry a failed request, defaults to 2. # @param timeout_in_seconds [Long] - # @param api_key [String] + # @param user_agent [AssemblyAI::UserAgent] # @return [AssemblyAI::RequestClient] def initialize(api_key:, environment: AssemblyAI::Environment::DEFAULT, base_url: nil, max_retries: nil, - timeout_in_seconds: nil) + timeout_in_seconds: nil, user_agent: nil) @default_environment = environment @base_url = environment || base_url @headers = { "X-Fern-Language": "Ruby", "X-Fern-SDK-Name": "assemblyai", "X-Fern-SDK-Version": "1.0.0", - "Authorization": api_key.to_s + "Authorization": api_key.to_s, + "User-Agent": AssemblyAI::UserAgent.merge(AssemblyAI::DefaultUserAgent.instance.user_agent, user_agent).serialize } @conn = Faraday.new(headers: @headers) do |faraday| faraday.request :json @@ -62,16 +65,18 @@ class AsyncRequestClient # @param max_retries [Long] The number of times to retry a failed request, defaults to 2. # @param timeout_in_seconds [Long] # @param api_key [String] + # @param user_agent [AssemblyAI::UserAgent] # @return [AssemblyAI::AsyncRequestClient] def initialize(api_key:, environment: AssemblyAI::Environment::DEFAULT, base_url: nil, max_retries: nil, - timeout_in_seconds: nil) + timeout_in_seconds: nil, user_agent: nil) @default_environment = environment @base_url = environment || base_url @headers = { "X-Fern-Language": "Ruby", "X-Fern-SDK-Name": "assemblyai", "X-Fern-SDK-Version": "1.0.0", - "Authorization": api_key.to_s + "Authorization": api_key.to_s, + "User-Agent": AssemblyAI::UserAgent.merge(AssemblyAI::DefaultUserAgent.instance.user_agent, user_agent).serialize } @conn = Faraday.new(headers: @headers) do |faraday| faraday.request :json diff --git a/lib/user_agent.rb b/lib/user_agent.rb new file mode 100644 index 0000000..1d50794 --- /dev/null +++ b/lib/user_agent.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +require "singleton" + +module AssemblyAI + class UserAgentItem + attr_reader :name, :value, :version + + def initialize(name:, version:) + @name = name + @version = version + end + end + + class UserAgent + attr_reader :user_agent_items + + def initialize + @user_agent_items = {} + end + + # @param key [String] + # @param user_agent_item [AssemblyAI::UserAgentItem] + def add_item(key, user_agent_item) + if user_agent_item.nil? + @user_agent_items.delete(key) + else + @user_agent_items[key] = user_agent_item + end + end + + # @param user_agent1 [AssemblyAI::UserAgent] + # @param user_agent2 [AssemblyAI::UserAgent] + # @return [AssemblyAI::UserAgent] + def self.merge(user_agent1, user_agent2) + merged_user_agent = UserAgent.new + + user_agent1&.user_agent_items&.each do |key, item| + merged_user_agent.add_item(key, item) + end + + user_agent2&.user_agent_items&.each do |key, item| + merged_user_agent.add_item(key, item) + end + + merged_user_agent + end + + # @return [String] + def serialize + serialized_items = @user_agent_items.map do |key, item| + "#{key}=#{item.name}/#{item.version}" + end.join(" ") + + "AssemblyAI/1.0 (#{serialized_items})" + end + end + + class DefaultUserAgent + include Singleton + + attr_reader :user_agent + + def initialize + @user_agent = UserAgent.new + @user_agent.add_item("sdk", UserAgentItem.new(name: "Ruby", version: AssemblyAI::Gemconfig::VERSION)) + end + end +end