Class: Wave::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/wave/client.rb,
lib/wave.rb

Overview

HTTP client for the WAVE gateway. Handles auth, retries, and error parsing.

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.wave.online/v1"
USER_AGENT =
"wave-sdk-ruby/0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(api_key, base_url: DEFAULT_BASE_URL, max_retries: 3, timeout: 30) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
# File 'lib/wave/client.rb', line 13

def initialize(api_key, base_url: DEFAULT_BASE_URL, max_retries: 3, timeout: 30)
  raise ArgumentError, "api_key is required" if api_key.nil? || api_key.to_s.empty?

  @api_key = api_key
  @base_url = base_url.to_s.chomp("/")
  @max_retries = max_retries
  @timeout = timeout
end

Instance Method Details

#captionsObject

Access the Captions product.



36
37
38
# File 'lib/wave.rb', line 36

def captions
  Wave::Captions.new(self)
end

#chaptersObject

Access the Chapters product.



41
42
43
# File 'lib/wave.rb', line 41

def chapters
  Wave::Chapters.new(self)
end

#clipsObject

Access the Clips product.



26
27
28
# File 'lib/wave.rb', line 26

def clips
  Wave::Clips.new(self)
end

#collabObject

Access the Collab product.



56
57
58
# File 'lib/wave.rb', line 56

def collab
  Wave::Collab.new(self)
end

#editorObject

Access the Editor product.



46
47
48
# File 'lib/wave.rb', line 46

def editor
  Wave::Editor.new(self)
end

#phoneObject

Access the Phone product.



51
52
53
# File 'lib/wave.rb', line 51

def phone
  Wave::Phone.new(self)
end

#podcastObject

Access the Podcast product.



61
62
63
# File 'lib/wave.rb', line 61

def podcast
  Wave::Podcast.new(self)
end

#request(method, path, query: nil, body: nil) ⇒ Object

Perform a request. Returns parsed JSON (Hash) or nil for 204/no body. Raises Wave::Error / Wave::RateLimitError on failure.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wave/client.rb', line 24

def request(method, path, query: nil, body: nil)
  uri = URI("#{@base_url}#{path}")
  uri.query = URI.encode_www_form(compact(query)) if query && !compact(query).empty?

  attempt = 0
  loop do
    response = perform(method, uri, body)
    status = response.code.to_i
    request_id = response["x-request-id"]

    if status == 429
      retry_after = (response["Retry-After"] || "1").to_f
      if attempt < @max_retries
        sleep(retry_after)
        attempt += 1
        next
      end
      raise RateLimitError.new("rate limit exceeded", retry_after: retry_after, request_id: request_id)
    end

    if status >= 400
      err = parse_error(response, status, request_id)
      if err.retryable && attempt < @max_retries
        sleep(backoff(attempt))
        attempt += 1
        next
      end
      raise err
    end

    return nil if status == 204 || response.body.nil? || response.body.empty?

    content_type = response["Content-Type"].to_s
    return JSON.parse(response.body) if content_type.start_with?("application/json")

    return nil
  rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT, Net::OpenTimeout, Net::ReadTimeout, SocketError => e
    if attempt < @max_retries
      sleep(backoff(attempt))
      attempt += 1
      retry
    end
    raise Error.new(e.message, code: "NETWORK_ERROR", status_code: 0)
  end
end

#searchObject

Access the Search product.



81
82
83
# File 'lib/wave.rb', line 81

def search
  Wave::Search.new(self)
end

#sentimentObject

Access the Sentiment product.



76
77
78
# File 'lib/wave.rb', line 76

def sentiment
  Wave::Sentiment.new(self)
end

#studio_aiObject

Access the Studio AI product.



66
67
68
# File 'lib/wave.rb', line 66

def studio_ai
  Wave::StudioAi.new(self)
end

#transcribeObject

Access the Transcribe product.



71
72
73
# File 'lib/wave.rb', line 71

def transcribe
  Wave::Transcribe.new(self)
end

#voiceObject

Access the Voice product.



31
32
33
# File 'lib/wave.rb', line 31

def voice
  Wave::Voice.new(self)
end