Class: TiktokBusinessApi::Client
- Inherits:
-
Object
- Object
- TiktokBusinessApi::Client
- Defined in:
- lib/tiktok_business_api/client.rb
Overview
Main client for interacting with the TikTok Business API
Instance Attribute Summary collapse
-
#auth ⇒ TiktokBusinessApi::Auth
readonly
Authentication handler.
-
#config ⇒ TiktokBusinessApi::Config
readonly
Client configuration.
Instance Method Summary collapse
-
#adgroups ⇒ TiktokBusinessApi::Resources::Adgroup
Access to ad group resource.
-
#ads ⇒ TiktokBusinessApi::Resources::Ad
Access to ad resource.
-
#campaigns ⇒ TiktokBusinessApi::Resources::Campaign
Access to campaign resource.
-
#identities ⇒ TiktokBusinessApi::Resources::Identity
Access to identity resource.
-
#images ⇒ TiktokBusinessApi::Resources::Image
Access to image resource.
-
#initialize(options = {}) ⇒ Client
constructor
Initialize a new client.
-
#request(method, path, params = {}, headers = {}) ⇒ Hash
Make an HTTP request to the TikTok Business API.
-
#resource(resource_name) ⇒ BaseResource
Get or create a resource instance.
Constructor Details
#initialize(options = {}) ⇒ Client
Initialize a new client
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tiktok_business_api/client.rb', line 15 def initialize( = {}) @config = TiktokBusinessApi.config.dup || Config.new # Override config with options .each do |key, value| @config.send("#{key}=", value) if @config.respond_to?("#{key}=") end @auth = Auth.new(self) @resources = {} end |
Instance Attribute Details
#auth ⇒ TiktokBusinessApi::Auth (readonly)
Returns Authentication handler.
10 11 12 |
# File 'lib/tiktok_business_api/client.rb', line 10 def auth @auth end |
#config ⇒ TiktokBusinessApi::Config (readonly)
Returns Client configuration.
7 8 9 |
# File 'lib/tiktok_business_api/client.rb', line 7 def config @config end |
Instance Method Details
#adgroups ⇒ TiktokBusinessApi::Resources::Adgroup
Access to ad group resource
103 104 105 |
# File 'lib/tiktok_business_api/client.rb', line 103 def adgroups resource(:adgroup) end |
#ads ⇒ TiktokBusinessApi::Resources::Ad
Access to ad resource
110 111 112 |
# File 'lib/tiktok_business_api/client.rb', line 110 def ads resource(:ad) end |
#campaigns ⇒ TiktokBusinessApi::Resources::Campaign
Access to campaign resource
96 97 98 |
# File 'lib/tiktok_business_api/client.rb', line 96 def campaigns resource(:campaign) end |
#identities ⇒ TiktokBusinessApi::Resources::Identity
Access to identity resource
124 125 126 |
# File 'lib/tiktok_business_api/client.rb', line 124 def identities resource(:identity) end |
#images ⇒ TiktokBusinessApi::Resources::Image
Access to image resource
117 118 119 |
# File 'lib/tiktok_business_api/client.rb', line 117 def images resource(:image) end |
#request(method, path, params = {}, headers = {}) ⇒ Hash
Make an HTTP request to the TikTok Business API
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/tiktok_business_api/client.rb', line 51 def request(method, path, params = {}, headers = {}) url = File.join(@config.api_base_url, path) # Set up default headers default_headers = { 'Content-Type' => 'application/json' } # Add access token if available if @config.access_token default_headers['Access-Token'] = @config.access_token end # Merge with custom headers headers = default_headers.merge(headers) # Log the request log_request(method, url, params, headers) # Build the request response = connection.run_request(method, url, nil, headers) do |req| case method when :get, :delete req.params = params when :post, :put if headers['Content-Type'] == 'multipart/form-data' # For multipart form data, let Faraday handle it req..timeout = 120 # Extend timeout for file uploads req.body = {} # Initialize the body as an empty hash params.each do |key, value| req.body[key.to_sym] = value end else req.body = JSON.generate(params) unless params.empty? end end end # Parse and handle the response handle_response(response) end |
#resource(resource_name) ⇒ BaseResource
Get or create a resource instance
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/tiktok_business_api/client.rb', line 31 def resource(resource_name) @resources[resource_name] ||= begin # Convert resource_name to class name (e.g., :campaign => Campaign) class_name = resource_name.to_s.split('_').map(&:capitalize).join # Get the resource class resource_class = TiktokBusinessApi::Resources.const_get(class_name) # Create an instance resource_class.new(self) end end |