├── .gitignore ├── LICENSE.txt ├── README.md ├── Rapi_Explore.png ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── birdeveloper │ │ └── rapisample │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── birdeveloper │ │ │ └── rapisample │ │ │ ├── App.kt │ │ │ ├── Constants.kt │ │ │ ├── MainActivity.kt │ │ │ └── Model.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── birdeveloper │ └── rapisample │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── rapi ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── birdeveloper │ │ └── rapi │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── com │ │ └── birdeveloper │ │ └── rapi │ │ ├── HttpMethod.kt │ │ ├── Rapi.kt │ │ ├── RapiBuilder.kt │ │ ├── RestCreator.kt │ │ ├── RestService.kt │ │ ├── callback │ │ ├── IError.kt │ │ ├── IFailure.kt │ │ ├── IRequest.kt │ │ ├── ISuccess.kt │ │ └── RequestCallBacks.kt │ │ ├── download │ │ ├── DownloadHandler.kt │ │ └── SaveFileTask.kt │ │ └── utils │ │ ├── FileUtil.java │ │ └── Utils.kt │ └── test │ └── java │ └── com │ └── birdeveloper │ └── rapi │ └── ExampleUnitTest.kt ├── rapi_logo.png └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Görkem KARA 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 25 | [![Contributors][contributors-shield]][contributors-url] 26 | [![Forks][forks-shield]][forks-url] 27 | [![Stargazers][stars-shield]][stars-url] 28 | [![Issues][issues-shield]][issues-url] 29 | [![MIT License][license-shield]][license-url] 30 | [![LinkedIn][linkedin-shield]][linkedin-url] 31 | 32 | 33 | 34 | 35 |
36 |

37 | 38 | Rapi Logo 39 | 40 | 41 |

Rapi

42 | 43 |

44 | We made retrofit more understandable !! RAPI 45 |
46 | Explore the docs » 47 |
48 |
49 | View Demo 50 | · 51 | Report Bug 52 | · 53 | Request Feature 54 |

55 |

56 | 57 | 58 | 59 | 60 | ## Table of Contents 61 | 62 | * [About the Project](#about-the-project) 63 | * [Built With](#built-with) 64 | * [Getting Started](#getting-started) 65 | * [Prerequisites](#prerequisites) 66 | * [Installation](#installation) 67 | * [Usage](#usage) 68 | * [Setup](#setup) 69 | * [GET Request](#get-request) 70 | * [POST Request](#post-request) 71 | * [POST RAW Request](#post-raw-request) 72 | * [PUT Request](#put-request) 73 | * [DELETE Request](#delete-request) 74 | * [DOWNLOAD Request](#download-request) 75 | * [UPLOAD Request](#upload-request) 76 | * [Roadmap](#roadmap) 77 | * [Contributing](#contributing) 78 | * [License](#license) 79 | * [Contact](#contact) 80 | * [Acknowledgements](#acknowledgements) 81 | 82 | 83 | 84 | 85 | ## About The Project 86 | 87 | Rapi Explore Github 88 | 89 | 90 | 91 | Rapi offers you a lot of innovations by shortening the steps that retrofit should take in a **very short** and concise way. It **organizes the network requests in the background** and easily adapts to the **MVVM** structure. 92 | 93 | 94 | ### Built With 95 | 96 | * [okhttp3]() 97 | * [retrofit2]() 98 | * [kotlin]() 99 | 100 | 101 | 102 | 103 | ## Getting Started 104 | 105 | To get a local copy up and running follow these simple steps. 106 | 107 | ### Prerequisites 108 | 109 | This is an example of how to list things you need to use the software and how to install them. (already installed in the library) 110 | * retrofit 111 | ```sh 112 | implementation 'com.squareup.retrofit2:retrofit:2.6.3' 113 | ``` 114 | 115 | ### Installation 116 | To get a Git project into your build: 117 | **Step 1.** Add the JitPack repository to your build file. Add it in your **root build.gradle** at the end of repositories: 118 | ```sh 119 | allprojects { 120 | repositories { 121 | ... 122 | maven { url 'https://jitpack.io' } 123 | } 124 | } 125 | ``` 126 | **Step 2.** Add the dependency 127 | ```sh 128 | dependencies { 129 | implementation 'com.github.birdeveloper:Rapi:1.0.0' 130 | } 131 | ``` 132 | 133 | 134 | 135 | 136 | ## Usage 137 | 138 | ### Setup 139 | 140 | ```sh 141 | val headers: WeakHashMap = WeakHashMap() 142 | headers["Authorization"] = "Bearer vF9dft4qmTFDfd27asWEgf" 143 | Rapi.init(this,Constants.BASE_URL, headers, 120) 144 | // context, base url, headers or null, timeout 145 | ``` 146 | 147 | ### GET Request 148 | 149 | ```sh 150 | Rapi.buider() 151 | .request("https://reqres.in/api/users/") 152 | .params("page",2) 153 | .success(object : ISuccess { 154 | override fun onSuccess(response: String) { 155 | Log.d("onSuccess", response) 156 | } 157 | }) 158 | .failure(object :IFailure{ 159 | override fun onFailure() { 160 | Log.d("onFailure", "yes") 161 | } 162 | }) 163 | .error(object : IError{ 164 | override fun onError(code: Int, msg: String) { 165 | Log.d("onError", "code: $code ----- msg: $msg") 166 | } 167 | 168 | }) 169 | .build() 170 | .get() 171 | ``` 172 | 173 | ### POST Request 174 | 175 | ```sh 176 | Rapi.buider() 177 | .request("https://reqres.in/api/users/") 178 | .params("name","Görkem") 179 | .params("job","Android Developer") 180 | .success(object : ISuccess { 181 | override fun onSuccess(response: String) { 182 | Log.d("onSuccess", response) 183 | } 184 | }) 185 | .failure(object :IFailure{ 186 | override fun onFailure() { 187 | Log.d("onFailure", "yes") 188 | } 189 | }) 190 | .error(object : IError{ 191 | override fun onError(code: Int, msg: String) { 192 | Log.d("onError", "code: $code ----- msg: $msg") 193 | } 194 | 195 | }) 196 | .build() 197 | .post() 198 | ``` 199 | 200 | ### POST RAW Request 201 | 202 | ```sh 203 | val jsonStr = "{\"name\":\"Görkem\",\"job\":\"Android Developer\"}" 204 | Rapi.buider() 205 | .request(URL_USERS) 206 | .raw(jsonStr) 207 | .success(object : ISuccess { 208 | override fun onSuccess(response: String) { 209 | Log.d("onSuccess", response) 210 | } 211 | }) 212 | .failure(object :IFailure{ 213 | override fun onFailure() { 214 | Log.d("onFailure", "yes") 215 | } 216 | }) 217 | .error(object : IError{ 218 | override fun onError(code: Int, msg: String) { 219 | Log.d("onError", "code: $code ----- msg: $msg") 220 | } 221 | 222 | }) 223 | .build() 224 | .post() 225 | ``` 226 | 227 | ### PUT Request 228 | 229 | ```sh 230 | Rapi.buider() 231 | .request(Constants.URL_USER_ID) 232 | .params("name","Resul") 233 | .params("job","Software Developer") 234 | .success(object : ISuccess { 235 | override fun onSuccess(response: String) { 236 | Log.d("onSuccess", response) 237 | } 238 | }) 239 | .failure(object :IFailure{ 240 | override fun onFailure() { 241 | Log.d("onFailure", "yes") 242 | } 243 | }) 244 | .error(object : IError{ 245 | override fun onError(code: Int, msg: String) { 246 | Log.d("onError", "code: $code ----- msg: $msg") 247 | } 248 | 249 | }) 250 | .build() 251 | .put() 252 | ``` 253 | 254 | ### DELETE Request 255 | 256 | ```sh 257 | Rapi.buider() 258 | .request(Constants.URL_USER_ID) 259 | .success(object : ISuccess { 260 | override fun onSuccess(response: String) { 261 | Log.d("onSuccess", response) 262 | } 263 | }) 264 | .failure(object :IFailure{ 265 | override fun onFailure() { 266 | Log.d("onFailure", "yes") 267 | } 268 | }) 269 | .error(object : IError{ 270 | override fun onError(code: Int, msg: String) { 271 | Log.d("onError", "code: $code ----- msg: $msg") 272 | } 273 | 274 | }) 275 | .build() 276 | .delete() 277 | ``` 278 | 279 | ### DOWNLOAD Request 280 | 281 | ```sh 282 | /* 283 | IMPORTANT!!! Do not forget to write and read permission before discarding this request !! 284 | 285 | 286 | There are codes related to the permissions in the sample file. 287 | */ 288 | Rapi.buider() 289 | .request(Constants.URL_DOWNLOAD) 290 | .dir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath) 291 | .extension("doc") 292 | .name("example.doc") 293 | .success(object : ISuccess { 294 | override fun onSuccess(response: String) { 295 | Log.d("onSuccess", response) 296 | } 297 | }) 298 | .failure(object :IFailure{ 299 | override fun onFailure() { 300 | Log.d("onFailure", "yes") 301 | } 302 | }) 303 | .error(object : IError{ 304 | override fun onError(code: Int, msg: String) { 305 | Log.d("onError", "code: $code ----- msg: $msg") 306 | } 307 | }) 308 | .build() 309 | .download() 310 | ``` 311 | 312 | ### UPLOAD Request 313 | 314 | ```sh 315 | /* 316 | IMPORTANT!!! Do not forget to write and read permission before discarding this request !! 317 | 318 | 319 | There are codes related to the permissions in the sample file. 320 | */ 321 | Rapi.buider() 322 | .request(Constants.URL_UPLOAD) 323 | .file(File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath+"/example.jpeg")) 324 | .onRequest(object : IRequest{ 325 | override fun onRequestStart() { 326 | Log.d("onRequestStart", "run") 327 | } 328 | 329 | override fun onRequestEnd() { 330 | Log.d("onRequestEnd", "finish") 331 | } 332 | 333 | }) 334 | .success(object : ISuccess { 335 | override fun onSuccess(response: String) { 336 | Log.d("onSuccess", response) 337 | } 338 | }) 339 | .failure(object :IFailure{ 340 | override fun onFailure() { 341 | Log.d("onFailure", "yes") 342 | } 343 | }) 344 | .error(object : IError{ 345 | override fun onError(code: Int, msg: String) { 346 | Log.d("onError", "code: $code ----- msg: $msg") 347 | } 348 | }) 349 | .build() 350 | .upload() 351 | ``` 352 | 353 | 354 | ## Roadmap 355 | 356 | See the [open issues](https://github.com/birdeveloper/rapi/issues) for a list of proposed features (and known issues). 357 | 358 | 359 | 360 | 361 | ## Contributing 362 | 363 | Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**. 364 | 365 | 1. Fork the Project 366 | 2. Create your Feature Branch (`git checkout -b birdeveloper/Rapi`) 367 | 3. Commit your Changes (`git commit -m 'Add some Rapi'`) 368 | 4. Push to the Branch (`git push origin birdeveloper/Rapi`) 369 | 5. Open a Pull Request 370 | 371 | 372 | 373 | 374 | ## License 375 | 376 | Distributed under the MIT License. See `LICENSE` for more information. 377 | 378 | 379 | 380 | 381 | ## Contact 382 | 383 | Görkem KARA - [@gorkemkara](https://www.linkedin.com/in/gorkemkara/) - [email](mailto:birdeveloper.com@gmail.com) 384 | 385 | Project Link: [https://github.com/birdeveloper/rapi](https://github.com/birdeveloper/rapi) 386 | 387 | 388 | 389 | 390 | ## Acknowledgements 391 | 392 | * []() 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | [contributors-shield]: https://img.shields.io/github/contributors/othneildrew/Best-README-Template.svg?style=flat-square 401 | [contributors-url]: https://github.com/birdeveloper/Rapi/graphs/contributors 402 | [forks-shield]: https://img.shields.io/github/forks/othneildrew/Best-README-Template.svg?style=flat-square 403 | [forks-url]: https://github.com/birdeveloper/Rapi/network/members 404 | [stars-shield]: https://img.shields.io/github/stars/othneildrew/Best-README-Template.svg?style=flat-square 405 | [stars-url]: https://github.com/birdeveloper/Rapi/stargazers 406 | [issues-shield]: https://img.shields.io/github/issues/othneildrew/Best-README-Template.svg?style=flat-square 407 | [issues-url]: https://github.com/birdeveloper/Rapi/issues 408 | [license-shield]: https://img.shields.io/github/license/othneildrew/Best-README-Template.svg?style=flat-square 409 | [license-url]: https://github.com/birdeveloper/Rapi/blob/master/LICENSE.txt 410 | [linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=flat-square&logo=linkedin&colorB=555 411 | [linkedin-url]: https://www.linkedin.com/in/gorkemkara/ 412 | [product-screenshot]: images/screenshot.png 413 | -------------------------------------------------------------------------------- /Rapi_Explore.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/birdeveloper/Rapi/312c8d4b154f28cd288897a20252253c6693d84c/Rapi_Explore.png -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 29 7 | buildToolsVersion "29.0.3" 8 | 9 | defaultConfig { 10 | applicationId "com.birdeveloper.rapisample" 11 | minSdkVersion 19 12 | targetSdkVersion 29 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | 26 | } 27 | 28 | dependencies { 29 | implementation fileTree(dir: 'libs', include: ['*.jar']) 30 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 31 | implementation 'androidx.appcompat:appcompat:1.1.0' 32 | implementation 'androidx.core:core-ktx:1.2.0' 33 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 34 | testImplementation 'junit:junit:4.12' 35 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 36 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 37 | 38 | implementation project(":rapi") 39 | } 40 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/birdeveloper/rapisample/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapisample 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.birdeveloper.rapisample", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/birdeveloper/rapisample/App.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapisample 2 | 3 | import android.app.Application 4 | import com.birdeveloper.rapi.Rapi 5 | import java.util.* 6 | /** 7 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 8 | Created date: 04.05.2020 9 | Author: Görkem KARA - gorkemkara.com.tr 10 | */ 11 | class App : Application(){ 12 | lateinit var headers: WeakHashMap 13 | override fun onCreate() { 14 | super.onCreate() 15 | headers = WeakHashMap() 16 | headers.put("Authorization", "Bearer vF9dft4qmTFDfd27asWEgf") 17 | Rapi.init(this,Constants.BASE_URL, null, 120) 18 | } 19 | } -------------------------------------------------------------------------------- /app/src/main/java/com/birdeveloper/rapisample/Constants.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapisample 2 | /** 3 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 4 | Created date: 04.05.2020 5 | Author: Görkem KARA - gorkemkara.com.tr 6 | */ 7 | class Constants { 8 | 9 | companion object { 10 | const val BASE_URL = "https://reqres.in/" 11 | const val URL_USERS = BASE_URL+"api/users/" 12 | const val URL_USER_ID = URL_USERS+"2" 13 | const val URL_DOWNLOAD = "https://file-examples.com/wp-content/uploads/2017/02/file-sample_100kB.doc" 14 | const val URL_UPLOAD = "https://v2.convertapi.com/upload" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/birdeveloper/rapisample/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapisample 2 | 3 | import android.Manifest 4 | import android.content.pm.PackageManager 5 | import android.os.Build 6 | import android.os.Bundle 7 | import android.os.Environment 8 | import android.util.Log 9 | import androidx.annotation.RequiresApi 10 | import androidx.appcompat.app.AppCompatActivity 11 | import androidx.core.app.ActivityCompat 12 | import com.birdeveloper.rapi.Rapi 13 | import com.birdeveloper.rapi.callback.IError 14 | import com.birdeveloper.rapi.callback.IFailure 15 | import com.birdeveloper.rapi.callback.IRequest 16 | import com.birdeveloper.rapi.callback.ISuccess 17 | import com.birdeveloper.rapisample.Constants.Companion.URL_USERS 18 | import java.io.File 19 | 20 | /** 21 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 22 | Created date: 04.05.2020 23 | Author: Görkem KARA - gorkemkara.com.tr 24 | */ 25 | class MainActivity : AppCompatActivity() { 26 | 27 | override fun onCreate(savedInstanceState: Bundle?) { 28 | super.onCreate(savedInstanceState) 29 | setContentView(R.layout.activity_main) 30 | rapiDelete() 31 | } 32 | fun rapiGet(){ 33 | Log.d("rapiGet", "run") 34 | Rapi.buider() 35 | .request(URL_USERS) 36 | .params("page",2) 37 | .success(object : ISuccess { 38 | override fun onSuccess(response: String) { 39 | Log.d("onSuccess", response) 40 | } 41 | }) 42 | .failure(object :IFailure{ 43 | override fun onFailure() { 44 | Log.d("onFailure", "yes") 45 | } 46 | }) 47 | .error(object : IError{ 48 | override fun onError(code: Int, msg: String) { 49 | Log.d("onError", "code: $code ----- msg: $msg") 50 | } 51 | 52 | }) 53 | .build() 54 | .get() 55 | } 56 | fun rapiPost(){ 57 | Log.d("rapiPost", "run") 58 | Rapi.buider() 59 | .request(URL_USERS) 60 | .params("name","Görkem") 61 | .params("job","Android Developer") 62 | .success(object : ISuccess { 63 | override fun onSuccess(response: String) { 64 | Log.d("onSuccess", response) 65 | } 66 | }) 67 | .failure(object :IFailure{ 68 | override fun onFailure() { 69 | Log.d("onFailure", "yes") 70 | } 71 | }) 72 | .error(object : IError{ 73 | override fun onError(code: Int, msg: String) { 74 | Log.d("onError", "code: $code ----- msg: $msg") 75 | } 76 | 77 | }) 78 | .build() 79 | .post() 80 | } 81 | fun rapiPostRaw(){ 82 | Log.d("rapiPostRaw", "run") 83 | val jsonStr = "{\"name\":\"Görkem\",\"job\":\"Android Developer\"}" 84 | Rapi.buider() 85 | .request(URL_USERS) 86 | .raw(jsonStr) 87 | .success(object : ISuccess { 88 | override fun onSuccess(response: String) { 89 | Log.d("onSuccess", response) 90 | } 91 | }) 92 | .failure(object :IFailure{ 93 | override fun onFailure() { 94 | Log.d("onFailure", "yes") 95 | } 96 | }) 97 | .error(object : IError{ 98 | override fun onError(code: Int, msg: String) { 99 | Log.d("onError", "code: $code ----- msg: $msg") 100 | } 101 | 102 | }) 103 | .build() 104 | .post() 105 | } 106 | fun rapiPut(){ 107 | Log.d("rapiPut", "run") 108 | Rapi.buider() 109 | .request(Constants.URL_USER_ID) 110 | .params("name","Resul") 111 | .params("job","Software Developer") 112 | .success(object : ISuccess { 113 | override fun onSuccess(response: String) { 114 | Log.d("onSuccess", response) 115 | } 116 | }) 117 | .failure(object :IFailure{ 118 | override fun onFailure() { 119 | Log.d("onFailure", "yes") 120 | } 121 | }) 122 | .error(object : IError{ 123 | override fun onError(code: Int, msg: String) { 124 | Log.d("onError", "code: $code ----- msg: $msg") 125 | } 126 | 127 | }) 128 | .build() 129 | .put() 130 | } 131 | fun rapiDelete(){ 132 | Log.d("rapiDelete", "run") 133 | Rapi.buider() 134 | .request(Constants.URL_USER_ID) 135 | .success(object : ISuccess { 136 | override fun onSuccess(response: String) { 137 | Log.d("onSuccess", response) 138 | } 139 | }) 140 | .failure(object :IFailure{ 141 | override fun onFailure() { 142 | Log.d("onFailure", "yes") 143 | } 144 | }) 145 | .error(object : IError{ 146 | override fun onError(code: Int, msg: String) { 147 | Log.d("onError", "code: $code ----- msg: $msg") 148 | } 149 | 150 | }) 151 | .build() 152 | .delete() 153 | } 154 | fun rapiDownload(){ 155 | Log.d("rapiDownload", "run download directory: " + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath) 156 | if (isStoragePermissionGranted()) { 157 | Log.d("permission","Permission is granted"); 158 | Rapi.buider() 159 | .request(Constants.URL_DOWNLOAD) 160 | .dir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath) 161 | .extension("doc") 162 | .name("example.doc") 163 | .success(object : ISuccess { 164 | override fun onSuccess(response: String) { 165 | Log.d("onSuccess", response) 166 | } 167 | }) 168 | .failure(object :IFailure{ 169 | override fun onFailure() { 170 | Log.d("onFailure", "yes") 171 | } 172 | }) 173 | .error(object : IError{ 174 | override fun onError(code: Int, msg: String) { 175 | Log.d("onError", "code: $code ----- msg: $msg") 176 | } 177 | }) 178 | .build() 179 | .download() 180 | }else{ 181 | ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1) 182 | } 183 | 184 | } 185 | 186 | override fun onRequestPermissionsResult( 187 | requestCode: Int, 188 | permissions: Array, 189 | grantResults: IntArray 190 | ) { 191 | super.onRequestPermissionsResult(requestCode, permissions, grantResults) 192 | if(grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ 193 | Log.v("Permission: ",permissions[0]+ "was "+grantResults[0]); 194 | rapiUpload() 195 | //resume tasks needing this permission 196 | } 197 | } 198 | fun isStoragePermissionGranted():Boolean { 199 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 200 | { 201 | if ((checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) === PackageManager.PERMISSION_GRANTED)) 202 | { 203 | Log.d("PERMISSION", "Permission is granted") 204 | return true 205 | } 206 | else 207 | { 208 | Log.d("PERMISSION", "Permission is revoked") 209 | ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1) 210 | return false 211 | } 212 | } 213 | else 214 | { //permission is automatically granted on sdk<23 upon installation 215 | Log.d("PERMISSION", "Permission is granted") 216 | return true 217 | } 218 | } 219 | fun rapiUpload(){ 220 | Log.d("rapiUpload", "run upload file:"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath+"image.jpeg") 221 | if (isStoragePermissionGranted()){ 222 | Rapi.buider() 223 | .request(Constants.URL_UPLOAD) 224 | .file(File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath+"/example.jpeg")) 225 | .onRequest(object : IRequest{ 226 | override fun onRequestStart() { 227 | Log.d("onRequestStart", "run") 228 | } 229 | 230 | override fun onRequestEnd() { 231 | Log.d("onRequestEnd", "run") 232 | } 233 | 234 | }) 235 | .success(object : ISuccess { 236 | override fun onSuccess(response: String) { 237 | Log.d("onSuccess", response) 238 | } 239 | }) 240 | .failure(object :IFailure{ 241 | override fun onFailure() { 242 | Log.d("onFailure", "yes") 243 | } 244 | }) 245 | .error(object : IError{ 246 | override fun onError(code: Int, msg: String) { 247 | Log.d("onError", "code: $code ----- msg: $msg") 248 | } 249 | }) 250 | .build() 251 | .upload() 252 | }else{ 253 | ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1) 254 | } 255 | } 256 | } 257 | -------------------------------------------------------------------------------- /app/src/main/java/com/birdeveloper/rapisample/Model.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapisample 2 | 3 | import java.io.Serializable 4 | /** 5 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 6 | Created date: 04.05.2020 7 | Author: Görkem KARA - gorkemkara.com.tr 8 | */ 9 | data class Model( 10 | var userId: Any, 11 | var id: Any, 12 | var title: Any, 13 | var body:Any 14 | ): Serializable{ 15 | } 16 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/birdeveloper/Rapi/312c8d4b154f28cd288897a20252253c6693d84c/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/birdeveloper/Rapi/312c8d4b154f28cd288897a20252253c6693d84c/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/birdeveloper/Rapi/312c8d4b154f28cd288897a20252253c6693d84c/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/birdeveloper/Rapi/312c8d4b154f28cd288897a20252253c6693d84c/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/birdeveloper/Rapi/312c8d4b154f28cd288897a20252253c6693d84c/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/birdeveloper/Rapi/312c8d4b154f28cd288897a20252253c6693d84c/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/birdeveloper/Rapi/312c8d4b154f28cd288897a20252253c6693d84c/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/birdeveloper/Rapi/312c8d4b154f28cd288897a20252253c6693d84c/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/birdeveloper/Rapi/312c8d4b154f28cd288897a20252253c6693d84c/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/birdeveloper/Rapi/312c8d4b154f28cd288897a20252253c6693d84c/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #6200EE 4 | #3700B3 5 | #03DAC5 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RapiSample 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/birdeveloper/rapisample/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapisample 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext.kotlin_version = '1.3.50' 5 | repositories { 6 | google() 7 | jcenter() 8 | 9 | } 10 | dependencies { 11 | classpath 'com.android.tools.build:gradle:3.6.2' 12 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 13 | 14 | // NOTE: Do not place your application dependencies here; they belong 15 | // in the individual module build.gradle files 16 | } 17 | } 18 | 19 | allprojects { 20 | repositories { 21 | google() 22 | jcenter() 23 | 24 | } 25 | } 26 | 27 | task clean(type: Delete) { 28 | delete rootProject.buildDir 29 | } 30 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | # Kotlin code style for this project: "official" or "obsolete": 21 | kotlin.code.style=official 22 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/birdeveloper/Rapi/312c8d4b154f28cd288897a20252253c6693d84c/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Apr 05 12:25:07 EET 2020 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /rapi/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /rapi/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 29 7 | buildToolsVersion "29.0.3" 8 | 9 | defaultConfig { 10 | minSdkVersion 19 11 | targetSdkVersion 29 12 | versionCode 1 13 | versionName "1.0" 14 | 15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 | consumerProguardFiles 'consumer-rules.pro' 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | compileOptions { 26 | sourceCompatibility JavaVersion.VERSION_1_8 27 | targetCompatibility JavaVersion.VERSION_1_8 28 | } 29 | 30 | kotlinOptions { 31 | jvmTarget = "1.8" 32 | } 33 | 34 | } 35 | 36 | dependencies { 37 | implementation fileTree(dir: 'libs', include: ['*.jar']) 38 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50" 39 | implementation 'androidx.appcompat:appcompat:1.1.0' 40 | implementation 'androidx.core:core-ktx:1.2.0' 41 | testImplementation 'junit:junit:4.12' 42 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 43 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 44 | implementation("com.squareup.okio:okio:2.5.0") 45 | implementation("com.squareup.okhttp3:okhttp:4.4.0") 46 | implementation 'com.squareup.retrofit2:retrofit:2.6.3' 47 | implementation 'com.squareup.retrofit2:converter-scalars:2.3.0' 48 | implementation("com.squareup.okhttp3:logging-interceptor:4.4.0") 49 | implementation 'org.conscrypt:conscrypt-android:2.2.1' 50 | implementation 'com.google.code.gson:gson:2.8.6' 51 | } 52 | -------------------------------------------------------------------------------- /rapi/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/birdeveloper/Rapi/312c8d4b154f28cd288897a20252253c6693d84c/rapi/consumer-rules.pro -------------------------------------------------------------------------------- /rapi/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -dontwarn androidx.core.view.** 23 | -dontwarn android.view -------------------------------------------------------------------------------- /rapi/src/androidTest/java/com/birdeveloper/rapi/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.birdeveloper.rapi.test", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /rapi/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /rapi/src/main/java/com/birdeveloper/rapi/HttpMethod.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi 2 | /** 3 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 4 | Created date: 04.05.2020 5 | Author: Görkem KARA - gorkemkara.com.tr 6 | */ 7 | enum class HttpMethod { 8 | GET, POST, POST_RAW, PUT, PUT_RAW, DELETE, UPLOAD 9 | } 10 | -------------------------------------------------------------------------------- /rapi/src/main/java/com/birdeveloper/rapi/Rapi.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi 2 | 3 | import android.content.Context 4 | import com.birdeveloper.rapi.callback.* 5 | import com.birdeveloper.rapi.download.DownloadHandler 6 | import java.io.File 7 | import java.util.WeakHashMap 8 | import okhttp3.MediaType 9 | import okhttp3.MediaType.Companion.toMediaTypeOrNull 10 | import okhttp3.MultipartBody 11 | import okhttp3.RequestBody 12 | import retrofit2.Call 13 | import retrofit2.Callback 14 | /** 15 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 16 | Created date: 04.05.2020 17 | Author: Görkem KARA - gorkemkara.com.tr 18 | */ 19 | class Rapi internal constructor( 20 | url: String?, 21 | params:WeakHashMap, 22 | downloadDir: String?, 23 | extension: String?, 24 | name: String?, 25 | request: IRequest?, 26 | success: ISuccess?, 27 | failure: IFailure?, 28 | error: IError?, 29 | body: RequestBody?, 30 | file: File?, 31 | context: Context? 32 | ) { 33 | private var URL:String? = null 34 | private var REQUEST:IRequest? = null 35 | private var DOWNLOAD_DIR:String? = null 36 | private var EXTENSION:String? = null 37 | private var NAME:String? = null 38 | private var SUCCESS:ISuccess? = null 39 | private var FAILURE:IFailure? = null 40 | private var ERROR:IError? = null 41 | private var BODY:RequestBody? = null 42 | private var FILE:File? = null 43 | private var CONTEXT:Context? = null 44 | 45 | init{ 46 | if (url != null) { 47 | this.URL = url 48 | } 49 | PARAMS.putAll(params) 50 | if (downloadDir != null) { 51 | this.DOWNLOAD_DIR = downloadDir 52 | } 53 | if (extension != null) { 54 | this.EXTENSION = extension 55 | } 56 | if (name != null) { 57 | this.NAME = name 58 | } 59 | if (request != null) { 60 | this.REQUEST = request 61 | } 62 | if (success != null) { 63 | this.SUCCESS = success 64 | } 65 | if (failure != null) { 66 | this.FAILURE = failure 67 | } 68 | if (error != null) { 69 | this.ERROR = error 70 | } 71 | if (body != null) { 72 | this.BODY = body 73 | } 74 | if (file != null) { 75 | this.FILE = file 76 | } 77 | if (context != null) { 78 | this.CONTEXT = context 79 | } 80 | } 81 | private fun request(method: HttpMethod) { 82 | val service = RestCreator.restService 83 | var call: Call? = null 84 | if (REQUEST != null) 85 | { 86 | REQUEST!!.onRequestStart() 87 | } 88 | when (method) { 89 | HttpMethod.GET -> call = service.get(URL!!, PARAMS) 90 | HttpMethod.POST -> call = service.post(URL!!, PARAMS) 91 | HttpMethod.POST_RAW -> call = service.postRaw(URL!!, BODY!!) 92 | HttpMethod.PUT -> call = service.put(URL!!, PARAMS) 93 | HttpMethod.PUT_RAW -> call = service.putRaw(URL!!, BODY!!) 94 | HttpMethod.DELETE -> call = service.delete(URL!!, PARAMS) 95 | HttpMethod.UPLOAD -> { 96 | val requestBody = RequestBody.create(MultipartBody.FORM.toString().toMediaTypeOrNull(), FILE!!) 97 | val body = MultipartBody.Part.createFormData("file", (FILE as File).getName(), requestBody) 98 | call = RestCreator.restService.upload(URL!!, body) 99 | } 100 | else -> {} 101 | } 102 | if (call != null) 103 | { 104 | call.enqueue(RequestCallBacks(REQUEST, SUCCESS, FAILURE, ERROR)) 105 | } 106 | } 107 | fun get() { 108 | request(HttpMethod.GET) 109 | } 110 | fun post() { 111 | if (BODY == null) 112 | { 113 | request(HttpMethod.POST) 114 | } 115 | else 116 | { 117 | if (!PARAMS.isEmpty()) 118 | { 119 | throw RuntimeException("params must be null") 120 | } 121 | request(HttpMethod.POST_RAW) 122 | } 123 | } 124 | fun put() { 125 | if (BODY == null) 126 | { 127 | request(HttpMethod.PUT) 128 | } 129 | else 130 | { 131 | if (!PARAMS.isEmpty()) 132 | { 133 | throw RuntimeException("params must be null") 134 | } 135 | request(HttpMethod.PUT_RAW) 136 | } 137 | } 138 | fun delete() { 139 | request(HttpMethod.DELETE) 140 | } 141 | fun upload() { 142 | request(HttpMethod.UPLOAD) 143 | } 144 | fun download() { 145 | DownloadHandler(URL, REQUEST, DOWNLOAD_DIR, EXTENSION, NAME, 146 | SUCCESS, FAILURE, ERROR) 147 | .handleDownload() 148 | } 149 | companion object { 150 | private val PARAMS = RestCreator.params 151 | lateinit var appContext:Context 152 | lateinit var baseUrl:String 153 | var getHeaders: WeakHashMap? = null 154 | var timeout: Int? = null 155 | fun init(context:Context, url:String, hashMap: WeakHashMap?, timeout: Int?) { 156 | appContext = context 157 | baseUrl = url 158 | getHeaders = hashMap 159 | this.timeout = timeout 160 | } 161 | fun buider():RapiBuilder { 162 | return RapiBuilder() 163 | } 164 | } 165 | } -------------------------------------------------------------------------------- /rapi/src/main/java/com/birdeveloper/rapi/RapiBuilder.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi 2 | 3 | import android.content.Context 4 | import com.birdeveloper.rapi.callback.IError 5 | import com.birdeveloper.rapi.callback.IFailure 6 | import com.birdeveloper.rapi.callback.IRequest 7 | import com.birdeveloper.rapi.callback.ISuccess 8 | import java.io.File 9 | import java.util.WeakHashMap 10 | import okhttp3.MediaType.Companion.toMediaTypeOrNull 11 | import okhttp3.RequestBody 12 | /** 13 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 14 | Created date: 04.05.2020 15 | Author: Görkem KARA - gorkemkara.com.tr 16 | */ 17 | class RapiBuilder internal constructor() { 18 | private var mUrl: String? = null 19 | private var mIRequest: IRequest? = null 20 | private var mISuccess: ISuccess? = null 21 | private var mIFailure: IFailure? = null 22 | private var mIError: IError? = null 23 | private var mBody: RequestBody? = null 24 | private var mContext: Context? = null 25 | private var mFile: File? = null 26 | private var mDownloadDir: String? = null 27 | private var mExtension: String? = null 28 | private var mName: String? = null 29 | fun request(url:String):RapiBuilder { 30 | this.mUrl = url 31 | return this 32 | } 33 | fun params(params:Map):RapiBuilder { 34 | PARAMS.putAll(params) 35 | return this 36 | } 37 | fun params(key:String, value:Any):RapiBuilder { 38 | PARAMS.put(key, value) 39 | return this 40 | } 41 | fun file(file:File):RapiBuilder { 42 | this.mFile = file 43 | return this 44 | } 45 | fun file(file:String):RapiBuilder { 46 | this.mFile = File(file) 47 | return this 48 | } 49 | fun name(name:String):RapiBuilder { 50 | this.mName = name 51 | return this 52 | } 53 | fun dir(dir:String):RapiBuilder { 54 | this.mDownloadDir = dir 55 | return this 56 | } 57 | fun extension(extension:String):RapiBuilder { 58 | this.mExtension = extension 59 | return this 60 | } 61 | fun raw(raw:String):RapiBuilder { 62 | this.mBody = RequestBody.create("application/json;charset=UTF-8".toMediaTypeOrNull(), raw) 63 | return this 64 | } 65 | fun success(iSuccess:ISuccess):RapiBuilder { 66 | this.mISuccess = iSuccess 67 | return this 68 | } 69 | fun onRequest(iRequest:IRequest):RapiBuilder { 70 | this.mIRequest = iRequest 71 | return this 72 | } 73 | fun failure(iFailure:IFailure):RapiBuilder { 74 | this.mIFailure = iFailure 75 | return this 76 | } 77 | fun error(iError:IError):RapiBuilder { 78 | this.mIError = iError 79 | return this 80 | } 81 | private fun checkParams():Map { 82 | if (PARAMS == null) 83 | { 84 | return WeakHashMap() 85 | } 86 | return PARAMS 87 | } 88 | fun build():Rapi { 89 | return Rapi(mUrl, PARAMS, 90 | mDownloadDir, mExtension, mName, 91 | mIRequest, mISuccess, mIFailure, 92 | mIError, mBody, mFile, mContext) 93 | } 94 | companion object { 95 | private val PARAMS = RestCreator.params 96 | } 97 | } -------------------------------------------------------------------------------- /rapi/src/main/java/com/birdeveloper/rapi/RestCreator.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi 2 | 3 | import java.util.WeakHashMap 4 | import java.util.concurrent.TimeUnit 5 | import okhttp3.OkHttpClient 6 | import okhttp3.logging.HttpLoggingInterceptor 7 | import retrofit2.Retrofit 8 | import retrofit2.converter.scalars.ScalarsConverterFactory 9 | /** 10 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 11 | Created date: 04.05.2020 12 | Author: Görkem KARA - gorkemkara.com.tr 13 | */ 14 | object RestCreator { 15 | val params:WeakHashMap 16 | get() { 17 | return ParamsHolder.PARAMS 18 | } 19 | val restService:RestService 20 | get() { 21 | return RestServiceHolder.REST_SERVICE 22 | } 23 | object ParamsHolder { 24 | val PARAMS = WeakHashMap() 25 | } 26 | private object RetrofitHolder { 27 | private val BASE_URL = Rapi.baseUrl 28 | private var headers: WeakHashMap? = Rapi.getHeaders 29 | private var timeout: Int? = Rapi.timeout 30 | val RETROFIT_CLIENT = Retrofit.Builder() 31 | .baseUrl(BASE_URL) 32 | .client(OKHttpHolder.clientBuilder(headers, timeout!!)) 33 | .addConverterFactory(ScalarsConverterFactory.create()) 34 | .build() 35 | } 36 | private object OKHttpHolder { 37 | 38 | fun clientBuilder(headers: WeakHashMap? , timeout: Int): OkHttpClient{ 39 | val httpLoggingInterceptor = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger.DEFAULT) 40 | val clientBuilder = OkHttpClient.Builder() 41 | if (BuildConfig.DEBUG) { 42 | httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC 43 | clientBuilder.addInterceptor(httpLoggingInterceptor) 44 | } 45 | if (headers != null){ 46 | clientBuilder.addInterceptor { chain -> 47 | val newRequest:okhttp3.Request.Builder = chain.request().newBuilder() 48 | for (item in headers.entries){ 49 | newRequest.addHeader( 50 | item.key, item.value 51 | ).build() 52 | } 53 | chain.proceed(newRequest.build()) 54 | } 55 | } 56 | clientBuilder.readTimeout(timeout.toLong(), TimeUnit.SECONDS) 57 | clientBuilder.writeTimeout(timeout.toLong(), TimeUnit.SECONDS) 58 | clientBuilder.connectTimeout(timeout.toLong(), TimeUnit.SECONDS) 59 | return clientBuilder.build() 60 | 61 | } 62 | } 63 | private object RestServiceHolder { 64 | val REST_SERVICE = RetrofitHolder.RETROFIT_CLIENT.create(RestService::class.java) 65 | } 66 | } -------------------------------------------------------------------------------- /rapi/src/main/java/com/birdeveloper/rapi/RestService.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi 2 | 3 | import java.util.WeakHashMap 4 | import okhttp3.MultipartBody 5 | import okhttp3.RequestBody 6 | import okhttp3.ResponseBody 7 | import retrofit2.Call 8 | import retrofit2.http.Body 9 | import retrofit2.http.DELETE 10 | import retrofit2.http.FieldMap 11 | import retrofit2.http.FormUrlEncoded 12 | import retrofit2.http.GET 13 | import retrofit2.http.Multipart 14 | import retrofit2.http.POST 15 | import retrofit2.http.PUT 16 | import retrofit2.http.Part 17 | import retrofit2.http.QueryMap 18 | import retrofit2.http.Streaming 19 | import retrofit2.http.Url 20 | 21 | /** 22 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 23 | Created date: 04.05.2020 24 | Author: Görkem KARA - gorkemkara.com.tr 25 | */ 26 | 27 | interface RestService { 28 | @GET 29 | fun get(@Url url:String, @QueryMap params:WeakHashMap):Call 30 | @FormUrlEncoded 31 | @POST 32 | fun post(@Url url:String, @FieldMap params:WeakHashMap):Call 33 | @POST 34 | fun postRaw(@Url url:String, @Body body:RequestBody):Call 35 | @FormUrlEncoded 36 | @PUT 37 | fun put(@Url url:String, @QueryMap params:WeakHashMap):Call 38 | @POST 39 | fun putRaw(@Url url:String, @Body body:RequestBody):Call 40 | @DELETE 41 | fun delete(@Url url:String, @QueryMap params:WeakHashMap):Call 42 | @Streaming 43 | @GET 44 | fun download(@Url url:String, @QueryMap params:WeakHashMap):Call 45 | @Multipart 46 | @POST 47 | fun upload(@Url url:String, @Part file:MultipartBody.Part):Call 48 | } -------------------------------------------------------------------------------- /rapi/src/main/java/com/birdeveloper/rapi/callback/IError.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi.callback 2 | /** 3 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 4 | Created date: 04.05.2020 5 | Author: Görkem KARA - gorkemkara.com.tr 6 | */ 7 | interface IError { 8 | fun onError(code: Int, msg: String) 9 | } -------------------------------------------------------------------------------- /rapi/src/main/java/com/birdeveloper/rapi/callback/IFailure.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi.callback 2 | /** 3 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 4 | Created date: 04.05.2020 5 | Author: Görkem KARA - gorkemkara.com.tr 6 | */ 7 | interface IFailure { 8 | fun onFailure() 9 | } -------------------------------------------------------------------------------- /rapi/src/main/java/com/birdeveloper/rapi/callback/IRequest.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi.callback 2 | /** 3 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 4 | Created date: 04.05.2020 5 | Author: Görkem KARA - gorkemkara.com.tr 6 | */ 7 | interface IRequest { 8 | fun onRequestStart() 9 | 10 | fun onRequestEnd() 11 | } -------------------------------------------------------------------------------- /rapi/src/main/java/com/birdeveloper/rapi/callback/ISuccess.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi.callback 2 | /** 3 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 4 | Created date: 04.05.2020 5 | Author: Görkem KARA - gorkemkara.com.tr 6 | */ 7 | interface ISuccess { 8 | fun onSuccess(response: String) 9 | } -------------------------------------------------------------------------------- /rapi/src/main/java/com/birdeveloper/rapi/callback/RequestCallBacks.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi.callback 2 | /** 3 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 4 | Created date: 04.05.2020 5 | Author: Görkem KARA - gorkemkara.com.tr 6 | */ 7 | import android.os.Handler 8 | import retrofit2.Call 9 | import retrofit2.Callback 10 | import retrofit2.Response 11 | 12 | 13 | class RequestCallBacks( 14 | private val REQUEST: IRequest?, private val SUCCESS: ISuccess?, private val FAILURE: IFailure? 15 | , private val ERROR: IError? 16 | ) : 17 | Callback { 18 | override fun onResponse( 19 | call: Call, 20 | response: Response 21 | ) { 22 | if (response.isSuccessful) { 23 | if (call.isExecuted) { 24 | SUCCESS?.onSuccess(if (response.body() != null) response.body()!! else "The response from the server is empty! Please check your api. @Rapi wishes you success :)") 25 | } 26 | } else { 27 | ERROR?.onError(response.code(), response.message()) 28 | } 29 | } 30 | 31 | override fun onFailure( 32 | call: Call, 33 | t: Throwable 34 | ) { 35 | FAILURE?.onFailure() 36 | REQUEST?.onRequestEnd() 37 | 38 | } 39 | 40 | 41 | companion object { 42 | private val HANDLER = Handler() 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /rapi/src/main/java/com/birdeveloper/rapi/download/DownloadHandler.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi.download 2 | /** 3 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 4 | Created date: 04.05.2020 5 | Author: Görkem KARA - gorkemkara.com.tr 6 | */ 7 | import android.os.AsyncTask 8 | import com.birdeveloper.rapi.RestCreator 9 | import com.birdeveloper.rapi.callback.IError 10 | import com.birdeveloper.rapi.callback.IFailure 11 | import com.birdeveloper.rapi.callback.IRequest 12 | import com.birdeveloper.rapi.callback.ISuccess 13 | import okhttp3.ResponseBody 14 | import retrofit2.Call 15 | import retrofit2.Callback 16 | import retrofit2.Response 17 | import java.util.* 18 | 19 | class DownloadHandler( 20 | private val URL: String?, 21 | request: IRequest?, 22 | downDir: String?, 23 | extension: String?, 24 | name: String?, 25 | success: ISuccess?, 26 | failure: IFailure?, 27 | error: IError? 28 | ) { 29 | private var REQUEST: IRequest? = null 30 | private val DOWNLOAD_DIR: String 31 | private val EXTENSION: String 32 | private val NAME: String 33 | private val SUCCESS: ISuccess 34 | private val FAILURE: IFailure? 35 | private val ERROR: IError? 36 | fun handleDownload() { 37 | if (REQUEST != null) { 38 | REQUEST!!.onRequestStart() 39 | } 40 | RestCreator.restService.download(URL!!, PARAMS) 41 | .enqueue(object : Callback { 42 | override fun onResponse( 43 | call: Call, 44 | response: Response 45 | ) { 46 | if (response.isSuccessful) { 47 | val responseBody = response.body() 48 | val task = SaveFileTask(REQUEST, SUCCESS) 49 | task.executeOnExecutor( 50 | AsyncTask.THREAD_POOL_EXECUTOR, 51 | DOWNLOAD_DIR, EXTENSION, responseBody, NAME 52 | ) 53 | if (task.isCancelled()) { 54 | if (REQUEST != null) { 55 | REQUEST!!.onRequestEnd() 56 | } 57 | } 58 | } else { 59 | if (ERROR != null) { 60 | ERROR.onError(response.code(), response.message()) 61 | } 62 | } 63 | RestCreator.params.clear() 64 | } 65 | 66 | override fun onFailure( 67 | call: Call, 68 | t: Throwable 69 | ) { 70 | if (FAILURE != null) { 71 | FAILURE.onFailure() 72 | RestCreator.params.clear() 73 | } 74 | } 75 | }) 76 | } 77 | 78 | companion object { 79 | private val PARAMS: WeakHashMap = 80 | RestCreator.params 81 | } 82 | 83 | init { 84 | REQUEST = request 85 | DOWNLOAD_DIR = downDir!! 86 | EXTENSION = extension!! 87 | NAME = name!! 88 | SUCCESS = success!! 89 | FAILURE = failure 90 | ERROR = error 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /rapi/src/main/java/com/birdeveloper/rapi/download/SaveFileTask.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi.download 2 | /** 3 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 4 | Created date: 04.05.2020 5 | Author: Görkem KARA - gorkemkara.com.tr 6 | */ 7 | import android.content.Intent 8 | import android.net.Uri 9 | import android.os.AsyncTask 10 | import com.birdeveloper.rapi.Rapi 11 | import com.birdeveloper.rapi.callback.IRequest 12 | import com.birdeveloper.rapi.callback.ISuccess 13 | import com.birdeveloper.rapi.utils.FileUtil 14 | import java.io.File 15 | import okhttp3.ResponseBody 16 | internal class SaveFileTask(REQUEST: IRequest?, SUCCESS: ISuccess):AsyncTask() { 17 | private var REQUEST:IRequest? = null 18 | private val SUCCESS:ISuccess 19 | init{ 20 | this.REQUEST = REQUEST 21 | this.SUCCESS = SUCCESS 22 | } 23 | override fun doInBackground(vararg params:Any):File { 24 | var downloadDir = params[0] as String 25 | var extension = params[1] as String 26 | val body = params[2] as ResponseBody 27 | val name = params[3] as String 28 | val `is` = body.byteStream() 29 | if (downloadDir == null || downloadDir == "") 30 | { 31 | downloadDir = "down_loads" 32 | } 33 | if (extension == null || extension == "") 34 | { 35 | extension = "" 36 | } 37 | if (name == null) 38 | { 39 | return FileUtil.writeToDisk(`is`, downloadDir, extension.toUpperCase(), extension) 40 | } 41 | else 42 | { 43 | return FileUtil.writeToDisk(`is`, downloadDir, name) 44 | } 45 | } 46 | override fun onPostExecute(file:File) { 47 | super.onPostExecute(file) 48 | if (SUCCESS != null) 49 | { 50 | SUCCESS.onSuccess(file.getPath()) 51 | } 52 | if (REQUEST != null) 53 | { 54 | REQUEST!!.onRequestEnd() 55 | } 56 | autoInstallApk(file) 57 | } 58 | private fun autoInstallApk(file:File) { 59 | if (FileUtil.getExtension(file.getPath()).equals("apk")) 60 | { 61 | val install = Intent() 62 | install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 63 | install.setAction(Intent.ACTION_VIEW) 64 | install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive") 65 | Rapi.appContext.startActivity(install) 66 | } 67 | } 68 | } -------------------------------------------------------------------------------- /rapi/src/main/java/com/birdeveloper/rapi/utils/FileUtil.java: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi.utils; 2 | 3 | import android.content.ContentResolver; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.content.res.AssetManager; 7 | import android.database.Cursor; 8 | import android.graphics.Bitmap; 9 | import android.graphics.Typeface; 10 | import android.media.MediaScannerConnection; 11 | import android.net.Uri; 12 | import android.os.Build; 13 | import android.os.Environment; 14 | import android.provider.MediaStore; 15 | import android.webkit.MimeTypeMap; 16 | import android.widget.TextView; 17 | 18 | 19 | import com.birdeveloper.rapi.Rapi; 20 | 21 | import java.io.BufferedInputStream; 22 | import java.io.BufferedOutputStream; 23 | import java.io.BufferedReader; 24 | import java.io.File; 25 | import java.io.FileNotFoundException; 26 | import java.io.FileOutputStream; 27 | import java.io.IOException; 28 | import java.io.InputStream; 29 | import java.io.InputStreamReader; 30 | import java.text.SimpleDateFormat; 31 | import java.util.Date; 32 | import java.util.Locale; 33 | /** 34 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 35 | Created date: 04.05.2020 36 | Author: Görkem KARA - gorkemkara.com.tr 37 | */ 38 | public final class FileUtil { 39 | private static final String TIME_FORMAT = "_yyyyMMdd_HHmmss"; 40 | private static final String SDCARD_DIR = 41 | Environment.getExternalStorageDirectory().getPath(); 42 | public static final String UPLOAD_PHOTO_DIR = 43 | Environment.getExternalStorageDirectory().getPath() + "/a_upload_photos/"; 44 | public static final String WEB_CACHE_DIR = 45 | Environment.getExternalStorageDirectory().getPath() + "/app_web_cache/"; 46 | public static final String CAMERA_PHOTO_DIR = 47 | Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/Camera/"; 48 | private static String getTimeFormatName(String timeFormatHeader) { 49 | final Date date = new Date(System.currentTimeMillis()); 50 | final SimpleDateFormat dateFormat = new SimpleDateFormat("'" + timeFormatHeader + "'" + TIME_FORMAT, Locale.getDefault()); 51 | return dateFormat.format(date); 52 | } 53 | public static String getFileNameByTime(String timeFormatHeader, String extension) { 54 | return getTimeFormatName(timeFormatHeader) + "." + extension; 55 | } 56 | 57 | @SuppressWarnings("ResultOfMethodCallIgnored") 58 | private static File createDir(String sdcardDirName) { 59 | //拼接成SD卡中完整的dir 60 | final String dir = SDCARD_DIR + "/" + sdcardDirName + "/"; 61 | final File fileDir = new File(dir); 62 | if (!fileDir.exists()) { 63 | fileDir.mkdirs(); 64 | } 65 | return fileDir; 66 | } 67 | 68 | @SuppressWarnings("ResultOfMethodCallIgnored") 69 | public static File createFile(String sdcardDirName, String fileName) { 70 | return new File(createDir(sdcardDirName), fileName); 71 | } 72 | 73 | private static File createFileByTime(String sdcardDirName, String timeFormatHeader, String extension) { 74 | final String fileName = getFileNameByTime(timeFormatHeader, extension); 75 | return createFile(sdcardDirName, fileName); 76 | } 77 | 78 | public static String getMimeType(String filePath) { 79 | final String extension = getExtension(filePath); 80 | return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); 81 | } 82 | 83 | public static String getExtension(String filePath) { 84 | String suffix = ""; 85 | final File file = new File(filePath); 86 | final String name = file.getName(); 87 | final int idx = name.lastIndexOf('.'); 88 | if (idx > 0) { 89 | suffix = name.substring(idx + 1); 90 | } 91 | return suffix; 92 | } 93 | public static File saveBitmap(Bitmap mBitmap, String dir, int compress) { 94 | final String sdStatus = Environment.getExternalStorageState(); 95 | if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { 96 | return null; 97 | } 98 | FileOutputStream fos = null; 99 | BufferedOutputStream bos = null; 100 | File fileName = createFileByTime(dir, "DOWN_LOAD", "jpg"); 101 | try { 102 | fos = new FileOutputStream(fileName); 103 | bos = new BufferedOutputStream(fos); 104 | mBitmap.compress(Bitmap.CompressFormat.JPEG, compress, bos);// 把数据写入文件 105 | } catch (FileNotFoundException e) { 106 | e.printStackTrace(); 107 | } finally { 108 | try { 109 | 110 | if (bos != null) { 111 | bos.flush(); 112 | } 113 | if (bos != null) { 114 | bos.close(); 115 | } 116 | if (fos != null) { 117 | fos.flush(); 118 | } 119 | if (fos != null) { 120 | fos.close(); 121 | } 122 | } catch (IOException e) { 123 | e.printStackTrace(); 124 | } 125 | } 126 | refreshDCIM(); 127 | return fileName; 128 | } 129 | 130 | public static File writeToDisk(InputStream is, String dir, String name) { 131 | final File file = FileUtil.createFile(dir, name); 132 | BufferedInputStream bis = null; 133 | FileOutputStream fos = null; 134 | BufferedOutputStream bos = null; 135 | try { 136 | bis = new BufferedInputStream(is); 137 | fos = new FileOutputStream(file); 138 | bos = new BufferedOutputStream(fos); 139 | 140 | byte data[] = new byte[1024 * 4]; 141 | 142 | int count; 143 | while ((count = bis.read(data)) != -1) { 144 | bos.write(data, 0, count); 145 | } 146 | bos.flush(); 147 | fos.flush(); 148 | } catch (IOException e) { 149 | e.printStackTrace(); 150 | } finally { 151 | try { 152 | if (bos != null) { 153 | bos.close(); 154 | } 155 | if (fos != null) { 156 | fos.close(); 157 | } 158 | if (bis != null) { 159 | bis.close(); 160 | } 161 | is.close(); 162 | } catch (IOException e) { 163 | e.printStackTrace(); 164 | } 165 | } 166 | 167 | return file; 168 | } 169 | 170 | public static File writeToDisk(InputStream is, String dir, String prefix, String extension) { 171 | final File file = FileUtil.createFileByTime(dir, prefix, extension); 172 | BufferedInputStream bis = null; 173 | FileOutputStream fos = null; 174 | BufferedOutputStream bos = null; 175 | try { 176 | bis = new BufferedInputStream(is); 177 | fos = new FileOutputStream(file); 178 | bos = new BufferedOutputStream(fos); 179 | byte data[] = new byte[1024 * 4]; 180 | int count; 181 | while ((count = bis.read(data)) != -1) { 182 | bos.write(data, 0, count); 183 | } 184 | bos.flush(); 185 | fos.flush(); 186 | } catch (IOException e) { 187 | e.printStackTrace(); 188 | } finally { 189 | try { 190 | if (bos != null) { 191 | bos.close(); 192 | } 193 | if (fos != null) { 194 | fos.close(); 195 | } 196 | if (bis != null) { 197 | bis.close(); 198 | } 199 | is.close(); 200 | } catch (IOException e) { 201 | e.printStackTrace(); 202 | } 203 | } 204 | return file; 205 | } 206 | private static void refreshDCIM() { 207 | if (Build.VERSION.SDK_INT >= 19) { 208 | //兼容android4.4版本,只扫描存放照片的目录 209 | MediaScannerConnection.scanFile(Rapi.appContext, 210 | new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath()}, 211 | null, null); 212 | } else { 213 | //扫描整个SD卡来更新系统图库,当文件很多时用户体验不佳,且不适合4.4以上版本 214 | Rapi.appContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + 215 | Environment.getExternalStorageDirectory()))); 216 | } 217 | } 218 | public static String getRawFile(int id) { 219 | final InputStream is = Rapi.appContext.getResources().openRawResource(id); 220 | final BufferedInputStream bis = new BufferedInputStream(is); 221 | final InputStreamReader isr = new InputStreamReader(bis); 222 | final BufferedReader br = new BufferedReader(isr); 223 | final StringBuilder stringBuilder = new StringBuilder(); 224 | String str; 225 | try { 226 | while ((str = br.readLine()) != null) { 227 | stringBuilder.append(str); 228 | } 229 | } catch (IOException e) { 230 | e.printStackTrace(); 231 | } finally { 232 | try { 233 | br.close(); 234 | isr.close(); 235 | bis.close(); 236 | is.close(); 237 | } catch (IOException e) { 238 | e.printStackTrace(); 239 | } 240 | } 241 | return stringBuilder.toString(); 242 | } 243 | 244 | 245 | public static void setIconFont(String path, TextView textView) { 246 | final Typeface typeface = Typeface.createFromAsset(Rapi.appContext.getAssets(), path); 247 | textView.setTypeface(typeface); 248 | } 249 | public static String getAssetsFile(String name) { 250 | InputStream is = null; 251 | BufferedInputStream bis = null; 252 | InputStreamReader isr = null; 253 | BufferedReader br = null; 254 | StringBuilder stringBuilder = null; 255 | final AssetManager assetManager = Rapi.appContext.getAssets(); 256 | try { 257 | is = assetManager.open(name); 258 | bis = new BufferedInputStream(is); 259 | isr = new InputStreamReader(bis); 260 | br = new BufferedReader(isr); 261 | stringBuilder = new StringBuilder(); 262 | String str; 263 | while ((str = br.readLine()) != null) { 264 | stringBuilder.append(str); 265 | } 266 | } catch (IOException e) { 267 | e.printStackTrace(); 268 | } finally { 269 | try { 270 | if (br != null) { 271 | br.close(); 272 | } 273 | if (isr != null) { 274 | isr.close(); 275 | } 276 | if (bis != null) { 277 | bis.close(); 278 | } 279 | if (is != null) { 280 | is.close(); 281 | } 282 | assetManager.close(); 283 | } catch (IOException e) { 284 | e.printStackTrace(); 285 | } 286 | } 287 | if (stringBuilder != null) { 288 | return stringBuilder.toString(); 289 | } else { 290 | return null; 291 | } 292 | } 293 | 294 | public static String getRealFilePath(final Context context, final Uri uri) { 295 | if (null == uri) return null; 296 | final String scheme = uri.getScheme(); 297 | String data = null; 298 | if (scheme == null) 299 | data = uri.getPath(); 300 | else if (ContentResolver.SCHEME_FILE.equals(scheme)) { 301 | data = uri.getPath(); 302 | } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { 303 | final Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null); 304 | if (null != cursor) { 305 | if (cursor.moveToFirst()) { 306 | final int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 307 | if (index > -1) { 308 | data = cursor.getString(index); 309 | } 310 | } 311 | cursor.close(); 312 | } 313 | } 314 | return data; 315 | } 316 | } 317 | -------------------------------------------------------------------------------- /rapi/src/main/java/com/birdeveloper/rapi/utils/Utils.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi.utils 2 | 3 | import com.google.gson.Gson 4 | import com.google.gson.GsonBuilder 5 | import com.google.gson.JsonArray 6 | import com.google.gson.JsonObject 7 | import com.google.gson.internal.LinkedTreeMap 8 | import com.google.gson.reflect.TypeToken 9 | import org.json.JSONObject 10 | import java.io.Serializable 11 | import java.lang.reflect.ParameterizedType 12 | import java.lang.reflect.Type 13 | import java.util.* 14 | /** 15 | Created by @birdeveloper , see profile link: https://github.com/birdeveloper 16 | Created date: 04.05.2020 17 | Author: Görkem KARA - gorkemkara.com.tr 18 | */ 19 | 20 | /* 21 | fun String.toObject(model: Serializable): Serializable{ 22 | val jsonString: String = this 23 | val data = model 24 | val gson = Gson().fromJson(jsonString, data::class.java) 25 | return data 26 | } 27 | public infix fun String.toObject(that: Class): T{ 28 | var mutableData = MutableLiveData() 29 | mutableData.value = Gson().fromJson( 30 | this, 31 | object : TypeToken() {}.type 32 | ) 33 | return mutableData.value!! 34 | }*/ 35 | 36 | fun String.getList( 37 | clazz: Class? 38 | ): T? { 39 | val typeOfT: Type = 40 | TypeToken.getParameterized(List::class.java, clazz).type 41 | return Gson().fromJson(this, typeOfT) 42 | } 43 | -------------------------------------------------------------------------------- /rapi/src/test/java/com/birdeveloper/rapi/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.birdeveloper.rapi 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /rapi_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/birdeveloper/Rapi/312c8d4b154f28cd288897a20252253c6693d84c/rapi_logo.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name='RapiSample' 2 | include ':app' 3 | include ':rapi' 4 | --------------------------------------------------------------------------------