|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -require "net/http" |
| 3 | +require 'httparty' |
4 | 4 |
|
5 | 5 | # Version |
6 | | -require "version" |
7 | | - |
8 | | -puts PodcastApi::VERSION |
| 6 | +require 'version' |
| 7 | +require 'errors' |
9 | 8 |
|
10 | 9 | module PodcastApi |
| 10 | + |
| 11 | + class Client |
| 12 | + include HTTParty |
| 13 | + |
| 14 | + @@BASE_URL_PROD = 'https://listen-api.listennotes.com/api/v2' |
| 15 | + @@BASE_URL_TEST = 'https://listen-api-test.listennotes.com/api/v2' |
| 16 | + |
| 17 | + def initialize(api_key: nil, user_agent: nil) |
| 18 | + @api_key = api_key |
| 19 | + @base_url = api_key ? @@BASE_URL_PROD : @@BASE_URL_TEST |
| 20 | + @headers = { |
| 21 | + 'X-ListenAPI-Key' => @api_key, |
| 22 | + 'User-Agent' => user_agent ? user_agent : "podcasts-api-ruby #{VERSION}" |
| 23 | + } |
| 24 | + puts @headers |
| 25 | + end |
| 26 | + |
| 27 | + protected |
| 28 | + def get_response(response:) |
| 29 | + if response.code == 200 |
| 30 | + return response |
| 31 | + elsif response.code == 400 |
| 32 | + raise InvalidRequestError.new 'something wrong on your end (client side errors), e.g., missing required parameters' |
| 33 | + elsif response.code == 401 |
| 34 | + raise AuthenticationError.new 'wrong api key or your account is suspended' |
| 35 | + elsif response.code == 404 |
| 36 | + raise NotFoundError.new 'endpoint not exist, or podcast / episode not exist' |
| 37 | + elsif response.code == 429 |
| 38 | + raise RateLimitError.new 'you are using FREE plan and you exceed the quota limit' |
| 39 | + else |
| 40 | + raise PodcastApiError.new 'something wrong on our end (unexpected server errors)' |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + def send_http_request(http_method, *args) |
| 45 | + begin |
| 46 | + response = HTTParty.public_send(http_method, *args) |
| 47 | + rescue SocketError |
| 48 | + raise APIConnectionError.new 'Failed to connect to Listen API servers' |
| 49 | + else |
| 50 | + return get_response(response: response) |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + public |
| 55 | + def search(**kwargs) |
| 56 | + return send_http_request('get', "#{@base_url}/search", {query: kwargs, headers: @headers}) |
| 57 | + end |
| 58 | + |
| 59 | + def typeahead(**kwargs) |
| 60 | + return send_http_request('get', "#{@base_url}/typeahead", {query: kwargs, headers: @headers}) |
| 61 | + end |
| 62 | + |
| 63 | + def fetch_best_podcasts(**kwargs) |
| 64 | + return send_http_request('get', "#{@base_url}/best_podcasts", {query: kwargs, headers: @headers}) |
| 65 | + end |
| 66 | + |
| 67 | + def fetch_podcast_by_id(**kwargs) |
| 68 | + id = kwargs.delete(:id) |
| 69 | + return send_http_request('get', "#{@base_url}/podcasts/#{id}", {query: kwargs, headers: @headers}) |
| 70 | + end |
| 71 | + |
| 72 | + def fetch_episode_by_id(**kwargs) |
| 73 | + id = kwargs.delete(:id) |
| 74 | + return send_http_request('get', "#{@base_url}/episodes/#{id}", {query: kwargs, headers: @headers}) |
| 75 | + end |
| 76 | + |
| 77 | + def batch_fetch_podcasts(**kwargs) |
| 78 | + return send_http_request('post', "#{@base_url}/podcasts", {body: kwargs, headers: @headers}) |
| 79 | + end |
| 80 | + |
| 81 | + def batch_fetch_episodes(**kwargs) |
| 82 | + return send_http_request('post', "#{@base_url}/episodes", {body: kwargs, headers: @headers}) |
| 83 | + end |
| 84 | + |
| 85 | + def fetch_curated_podcasts_list_by_id(**kwargs) |
| 86 | + id = kwargs.delete(:id) |
| 87 | + return send_http_request('get', "#{@base_url}/curated_podcasts/#{id}", {query: kwargs, headers: @headers}) |
| 88 | + end |
| 89 | + |
| 90 | + def fetch_curated_podcasts_lists(**kwargs) |
| 91 | + id = kwargs.delete(:id) |
| 92 | + return send_http_request('get', "#{@base_url}/curated_podcasts", {query: kwargs, headers: @headers}) |
| 93 | + end |
| 94 | + |
| 95 | + def fetch_podcast_genres(**kwargs) |
| 96 | + return send_http_request('get', "#{@base_url}/genres", {query: kwargs, headers: @headers}) |
| 97 | + end |
| 98 | + |
| 99 | + def fetch_podcast_regions(**kwargs) |
| 100 | + return send_http_request('get', "#{@base_url}/regions", {query: kwargs, headers: @headers}) |
| 101 | + end |
| 102 | + |
| 103 | + def fetch_podcast_languages(**kwargs) |
| 104 | + return send_http_request('get', "#{@base_url}/languages", {query: kwargs, headers: @headers}) |
| 105 | + end |
| 106 | + |
| 107 | + def just_listen(**kwargs) |
| 108 | + return send_http_request('get', "#{@base_url}/just_listen", {query: kwargs, headers: @headers}) |
| 109 | + end |
| 110 | + |
| 111 | + def fetch_recommendations_for_podcast(**kwargs) |
| 112 | + id = kwargs.delete(:id) |
| 113 | + return send_http_request('get', "#{@base_url}/podcasts/#{id}/recommendations", {query: kwargs, headers: @headers}) |
| 114 | + end |
| 115 | + |
| 116 | + def fetch_recommendations_for_episode(**kwargs) |
| 117 | + id = kwargs.delete(:id) |
| 118 | + return send_http_request('get', "#{@base_url}/episodes/#{id}/recommendations", {query: kwargs, headers: @headers}) |
| 119 | + end |
| 120 | + |
| 121 | + def fetch_playlist_by_id(**kwargs) |
| 122 | + id = kwargs.delete(:id) |
| 123 | + return send_http_request('get', "#{@base_url}/playlists/#{id}", {query: kwargs, headers: @headers}) |
| 124 | + end |
| 125 | + |
| 126 | + def fetch_my_playlists(**kwargs) |
| 127 | + return send_http_request('get', "#{@base_url}/playlists", {query: kwargs, headers: @headers}) |
| 128 | + end |
| 129 | + |
| 130 | + def submit_podcast(**kwargs) |
| 131 | + return send_http_request('post', "#{@base_url}/podcasts/submit", {body: kwargs, headers: @headers}) |
| 132 | + end |
| 133 | + |
| 134 | + def delete_podcast(**kwargs) |
| 135 | + id = kwargs.delete(:id) |
| 136 | + return send_http_request('delete', "#{@base_url}/podcasts/#{id}", {query: kwargs, headers: @headers}) |
| 137 | + end |
| 138 | + end |
| 139 | + |
11 | 140 | end |
0 commit comments