Skip to content

Commit

Permalink
Add user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Swimburger committed Jun 20, 2024
1 parent 7fd737a commit d35c40c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .fernignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ test/
.rubocop.yml
.github
Gemfile
assemblyai.gemspec

lib/gemconfig.rb
lib/requests.rb
lib/user_agent.rb

<!-- Manual customized files client -->
lib/assemblyai/files/client.rb
Expand Down
2 changes: 1 addition & 1 deletion assemblyai.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions lib/gemconfig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 10 additions & 5 deletions lib/requests.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative "environment"
require_relative "user_agent"
require "faraday"
require "faraday/retry"
require "async/http/faraday"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions lib/user_agent.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d35c40c

Please sign in to comment.