Class: JPSClient::Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API::Workflow

#create_create, #create_delete, #create_sort, #create_update, #get_list, #get_project_workflows

Methods included from API::Webhook

#create_callback, #create_event

Methods included from API::VideoCover

#create_insert_cover

Methods included from API::Util

#create_translate

Methods included from API::User

#delete_user, #get_user_detail, #get_users, #update_user

Methods included from API::UdId

#create_create, #create_delete, #create_update, #get_detail, #get_list

Methods included from API::Trending

#create_trendings

Methods included from API::Tool

#create_tool, #create_tools, #delete_tool, #get_tool_detail, #update_tool

Methods included from API::Template

#copy_template, #create_template, #delete_template, #get_template_detail, #get_template_list, #get_templates_by_ids, #get_templates_by_type, #search_templates, #update_template

Methods included from API::Tag

#batch_create_tags, #batch_update_weights, #copy_tag, #create_tag, #delete_tag, #get_tag_applications, #get_tag_ideas, #get_tag_resources, #get_tags, #get_tags_by_color, #get_tags_by_type, #get_tags_statistics, #search_tags, #update_tag, #update_tag_weights

Methods included from API::Survey

#create_create, #delete_survey, #get_list, #get_types, #update_survey

Methods included from API::Store

#create_application

Methods included from API::Statistics

#create_rankings

Methods included from API::Sov

#create_sovs

Methods included from API::Sketch

#create_sketch, #create_sketches

Methods included from API::SimpleSearch

#get_application, #get_asset, #get_collection, #get_idea, #get_publisher, #get_publisher_group, #get_tag

Methods included from API::Role

#create_menu, #create_role, #create_user, #delete_role, #get_role_detail, #get_roles, #update_role

Methods included from API::Requirements

#create_requirements, #delete_requirements, #get_list, #update_requirements

Methods included from API::PublisherGroup

#create_create, #create_delete, #create_detail, #create_update, #get_list

Methods included from API::Publisher

#create_detail, #create_publishers, #create_stream, #create_weights, #delete_publisher, #get_store_applications_detail, #update_publisher

Methods included from API::ProjectPackage

#bind_commit_to_package, #get_project_package_bind_list, #get_project_package_list, #preview_project_package, #send_project_package_message, #sign_project_package, #update_project_package, #upload_nuget_package, #upload_project_package

Methods included from API::Project

#get_project_detail, #get_project_list

Methods included from API::NuGet

#get_nuget_service_index

Methods included from API::ImageSearch

#search_image_by_image, #search_image_by_text

Methods included from API::Permission

#create_permission, #delete_permission, #get_permission_detail, #get_permission_list, #update_permission

Methods included from API::Menu

#create_menu, #delete_menu, #get_menu_detail, #get_menus, #get_role, #update_menu

Methods included from API::M3u8

#get_to_mp4

Methods included from API::Login

#create_lark_login, #create_logout, #get_get_user_by_uuid, #get_swagger_lark_login

Methods included from API::LarkLeaveApproval

#get_lark_leave_approval_calendar

Methods included from API::LarkDepartment

#get_lark_department_list

Methods included from API::LarkFile

#get_lark_file_url_by_image_key

Methods included from API::LarkMessage

#get_lark_message_detail

Methods included from API::LarkCardMessage

#get_lark_card_message_history

Methods included from API::LarkWikiSpace

#get_list

Methods included from API::LarkWikiNode

#get_list

Methods included from API::LarkUser

#get_list

Methods included from API::LarkTaskSection

#create_create, #get_detail, #get_list

Methods included from API::LarkTaskList

#get_all, #get_create, #get_custom_fields, #get_delete, #get_list

Methods included from API::LarkTask

#create_create, #create_delete, #create_update, #get_detail, #get_list, #get_type_info_list

Methods included from API::LarkComment

#create_create, #create_delete, #create_sync, #create_update, #get_list

Methods included from API::LarkChatGroup

#get_search

Methods included from API::LarkBitable

#create_create

Methods included from API::Jssdk

#get_auth

Methods included from API::Idea

#create_idea, #create_ideas, #delete_idea, #get_idea_detail, #update_idea

Methods included from API::Healthy

#get_healthy

Methods included from API::GameAssets

#create_delete, #create_detail, #create_game_asset, #create_game_assets, #create_update

Methods included from API::File

#complete_file_multipart, #get_file_sign_url, #get_simple_sign_url, #init_file_multipart

Methods included from API::FguiExport

#create_detail, #create_import

Methods included from API::DocumentText

#get_text

Methods included from API::CustomApplicationWeb

#create_html_website, #get_all_websites

Methods included from API::CustomApplication

#create_check_website, #create_custom_application, #create_icons, #create_stories, #create_websites, #delete_custom_application, #get_custom_application, #get_snapshots_list, #get_story_info, #update_custom_application

Methods included from API::Creative

#create_creatives, #get_top_publishers

Methods included from API::CommitLog

#get_commit_log_list, #get_commit_log_preview, #get_commit_log_simple, #send_commit_log_message, #update_commit_log

Methods included from API::Collection

#create_bind, #create_create, #create_delete, #create_detail, #create_update, #create_upload_zip, #get_list

Methods included from API::Collect

#create_collect, #delete_collect

Methods included from API::Cert

#get_cert_list

Methods included from API::Category

#batch_create_categories, #create_category, #delete_category, #disable_category, #enable_category, #get_categories_by_type, #get_category_list, #get_category_path, #get_category_statistics, #get_category_tree, #get_root_categories, #get_subcategories, #move_category, #sort_categories, #update_category

Methods included from API::Bug

#create_create, #create_delete, #create_update, #get_detail, #get_fields, #get_list

Methods included from API::ApplicationVersion

#create_appversions, #get_change_record_detail, #get_history_version_detail

Methods included from API::ApplicationSales

#get_range, #get_sales

Methods included from API::ApplicationIncome

#create_income_download, #get_income_download_detail

Methods included from API::Design

#check_design, #get_app_design_versions, #get_app_designs, #get_category_designs, #import_design

Methods included from API::Application

#create_application, #delete_application, #fast_bind_application, #get_application_detail, #get_application_rankings, #get_appstore_categories, #get_category_applications, #get_designers, #get_lark_task_apps_list, #refresh_application, #update_application, #update_application_weights

Methods included from API::AppleBundleId

#get_apple_bundle_id_list

Methods included from API::AppleProfile

#get_apple_profile_list

Methods included from API::AppleCert

#get_apple_cert_list

Methods included from API::AppleAccount

#create_bind, #create_create, #create_delete, #create_update, #get_detail, #get_list

Methods included from API::AppResource

#copy_or_transfer_resource, #create_app_resource, #create_app_resource_by_zip, #delete_app_resource, #get_app_elements, #get_category_elements, #update_app_resource

Methods included from API::AppLevel

#create_app_level, #delete_app_level, #get_all_category_levels, #get_app_levels, #update_app_level, #upload_app_level_zip

Methods included from API::AndroidJks

#create_android_jks, #delete_android_jks, #get_android_jks_detail, #get_android_jks_list, #rename_android_jks, #rewrite_apk, #update_android_jks

Constructor Details

#initialize(config_file: nil, config: nil) ⇒ Client

Returns a new instance of Client.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/jpsclient/base/client.rb', line 196

def initialize(config_file: nil, config: nil)
  begin
    # 支持两种初始化方式:从文件或直接配置
    if config
      # 直接使用提供的配置(主要用于测试)
      @config_json = config
      @config_file = nil
    elsif config_file
      # 从文件加载配置
      @config_file = config_file
      raise ExceptionError, "配置文件不存在: #{@config_file}" unless File.exist?(@config_file)
      @config_json = JSON.parse(File.read(@config_file))
    else
      raise ExceptionError, "必须提供配置文件路径或配置对象"
    end

    @baseurl = @config_json["pgyerapps_base_url"]
    @request_config = @config_json["api_path_config"]

    # 初始化API配置管理器
    @api_config = ApiConfig.new(@config_json)

    # 从 auth_config 获取配置
    auth_config = @config_json["auth_config"]
    @pgyer_aes_key = auth_config["aes_key"]

    # 加载 JPS 登录配置
    @jps_config = LoginConfig.from_json(auth_config)

    # 初始化 token 管理器
    @token_manager = Token.new(@jps_config)

    # 尝试加载已保存的 token
    if @token_manager.load && @token_manager.loaded?
      @token = @token_manager.to_h
    end

    # 从 common_http_config 获取 HTTP 配置
    common_http_config = @config_json["common_http_config"] || {}
    default_timeout = common_http_config["timeout_seconds"]
    max_retry_times = common_http_config["max_retry_times"] || 3

    # 初始化共享的 HTTP 客户端
    @http_client = HttpClient.new(
      base_url: @baseurl,
      default_timeout: default_timeout,
      max_retry_times: max_retry_times
    )
    @http_client.update_token(@token["token"]) if @token && @token["token"]

  rescue JSON::ParserError => error
    raise ExceptionError.new("配置文件格式错误 (#{@config_file}): #{error.message}")
  rescue ExceptionError
    raise  # 重新抛出 JPS 异常
  rescue => error
    raise ExceptionError.new("加载配置文件失败 (#{@config_file}): #{error.message}")
  end
end

Instance Attribute Details

#api_configObject

Returns the value of attribute api_config.



192
193
194
# File 'lib/jpsclient/base/client.rb', line 192

def api_config
  @api_config
end

#baseurlObject

Returns the value of attribute baseurl.



189
190
191
# File 'lib/jpsclient/base/client.rb', line 189

def baseurl
  @baseurl
end

#config_fileObject (readonly)

配置文件路径



194
195
196
# File 'lib/jpsclient/base/client.rb', line 194

def config_file
  @config_file
end

#config_jsonObject (readonly)

暴露配置供其他模块使用



193
194
195
# File 'lib/jpsclient/base/client.rb', line 193

def config_json
  @config_json
end

#http_clientObject

Returns the value of attribute http_client.



191
192
193
# File 'lib/jpsclient/base/client.rb', line 191

def http_client
  @http_client
end

#request_configObject

Returns the value of attribute request_config.



190
191
192
# File 'lib/jpsclient/base/client.rb', line 190

def request_config
  @request_config
end

#tokenObject

Returns the value of attribute token.



188
189
190
# File 'lib/jpsclient/base/client.rb', line 188

def token
  @token
end

Instance Method Details

#do_login(force_login: false) ⇒ Object

核心登录功能token 有效性由服务端 401 响应判断,本地不做过期检查



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/jpsclient/base/client.rb', line 262

def (force_login: false)
  if 
    @token_manager.clear
  else
    # 本地存在 token,直接使用
    if @token_manager.load && @token_manager.loaded?
      update_token_everywhere
      return true
    end
  end

  # 需要重新登录
  auth = Auth.new(@jps_config)
  result = auth.

  if result == :user_cancelled
    Logger.instance.fancyinfo_error("用户取消了登录操作")
    return false
  elsif result == true
    unless @token_manager.save(auth.get_token_data)
      Logger.instance.fancyinfo_error("Token 保存失败,下次启动需要重新登录")
    end
    update_token_everywhere
    return true
  else
    Logger.instance.fancyinfo_error("登录失败,未能获取有效token")
    return false
  end
end

#request_with_auth(method, path, **opts) ⇒ Hash

带 401 重试的统一请求方法所有 API 模块应使用此方法发送请求

Parameters:

  • method (Symbol)

    HTTP 方法 (:get, :post, :put, :delete)

  • path (String)

    请求路径

  • opts (Hash)

    传递给 http_client 的参数 (params:, body:, timeout: 等)

Returns:

  • (Hash)

    响应数据



298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/jpsclient/base/client.rb', line 298

def request_with_auth(method, path, **opts)
  response = @http_client.send(method, path, **opts)
  result = JPSClient::Response.new(response)

  if result.need_login?
    if (force_login: true)
      response = @http_client.send(method, path, **opts)
      result = JPSClient::Response.new(response)
    end
  end

  result.to_h
end

#shared_http_clientObject

提供共享的HTTP客户端给其他模块



256
257
258
# File 'lib/jpsclient/base/client.rb', line 256

def shared_http_client
  @http_client
end