{"id":21650683,"url":"https://github.com/czy1121/httpapi","last_synced_at":"2025-04-11T20:22:10.099Z","repository":{"id":199057606,"uuid":"345523479","full_name":"czy1121/httpapi","owner":"czy1121","description":"使用 Coroutine + Retrofit 打造的最简单HTTP请求库","archived":false,"fork":false,"pushed_at":"2024-12-30T09:04:36.000Z","size":124,"stargazers_count":11,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-11T20:21:47.191Z","etag":null,"topics":["coroutine","http","jetpack","kotlin","retrofit2"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/czy1121.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-08T03:50:33.000Z","updated_at":"2024-12-30T09:04:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"3584c20d-57e6-4ec5-b47e-2b4806f3b165","html_url":"https://github.com/czy1121/httpapi","commit_stats":null,"previous_names":["czy1121/httpcall","czy1121/httpapi"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czy1121%2Fhttpapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czy1121%2Fhttpapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czy1121%2Fhttpapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czy1121%2Fhttpapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/czy1121","download_url":"https://codeload.github.com/czy1121/httpapi/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248473079,"owners_count":21109629,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coroutine","http","jetpack","kotlin","retrofit2"],"created_at":"2024-11-25T07:42:01.259Z","updated_at":"2025-04-11T20:22:10.089Z","avatar_url":"https://github.com/czy1121.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# httpapi\n \n使用 Coroutine + Retrofit 打造的最简单HTTP请求库\n\n## Gradle\n\n``` groovy\nrepositories {\n    maven { url \"https://gitee.com/ezy/repo/raw/cosmo/\"}\n}\ndependencies {\n    implementation \"me.reezy.cosmo:httpapi:0.10.1\"\n}\n```\n\n## 使用\n\n```kotlin\ndata class HttpBin(\n    val origin: String,\n    val url: String,\n)\n\ninterface TestService {  \n    @GET(\"https://httpbin.org/get\")\n    fun call(): Call\u003cHttpBin\u003e\n}\n```\n\n初始化\n\n```kotlin\nprivate val okhttp by lazy {\n    OkHttpClient.Builder().cache(Cache(File(cacheDir, \"okhttp\"), 100 * 1024 * 1024)).build()\n}\n\nprivate val moshi by lazy {\n    Moshi.Builder().add(KotlinJsonAdapterFactory()).build()\n}\n\nprivate val retrofit by lazy { \n    Retrofit.Builder().client(okhttp)\n        .baseUrl(\"http://httpbin.org\")\n        .addConverterFactory(MoshiConverterFactory.create(moshi).asLenient())\n        .addCallAdapterFactory(ResultCallAdapterFactory {\n            it.printStackTrace()\n        })\n        .build() \n}\n\n\n// 提供 Retrofit 实例\nApi.setRetrofitProvider {\n    retrofit\n}\n\n// 设置全局错误处理\nApi.setErrorHandler {\n    it.printStackTrace()\n}\n```\n\n普通请求\n\n```kotlin\n// 普通请求\napi\u003cTestApi\u003e().call().onSuccess(this) {\n    Log.e(\"OoO\", \"call =\u003e $it\")\n}.onFailure {\n    Log.e(\"OoO\", \"call onFailure  =\u003e $it\")\n}.onFinally {\n    Log.e(\"OoO\", \"call onFinally\")\n}\n\n```\n\n在协程上下文中发起请求\n\n```kotlin\n// 在协程上下文中发起请求\nlifecycleScope.launch {\n\n    // 自己处理异常\n    try {\n        val data = api\u003cTestApi\u003e().call().await()\n        Log.e(\"OoO\", \"call =\u003e $data\")\n    } catch (ex: Throwable) {\n        Log.e(\"OoO\", \"call catch =\u003e $ex\")\n    }\n\n    // 使用全局异常处理，可能返回 null\n    val result = api\u003cTestApi\u003e().call().catch() \n    Log.e(\"OoO\", \"call =\u003e $result\")\n\n}\n```\n\n\n## LICENSE\n\nThe Component is open-sourced software licensed under the [Apache license](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczy1121%2Fhttpapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fczy1121%2Fhttpapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczy1121%2Fhttpapi/lists"}