Skip to content

Commit

Permalink
Automatically close body on .text
Browse files Browse the repository at this point in the history
  • Loading branch information
Blatzar committed Oct 11, 2023
1 parent 2a69d1d commit dc80907
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package com.lagradost.nicehttp.example

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.lagradost.nicehttp.Requests
import com.lagradost.nicehttp.ResponseParser
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlin.reflect.KClass

Expand All @@ -23,8 +24,7 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
lifecycleScope.launch {

CoroutineScope(Dispatchers.IO).launch {
/**
* Implement your own json parsing to then do request.parsed<T>()
* */
Expand Down Expand Up @@ -59,7 +59,8 @@ class MainActivity : AppCompatActivity() {

// Example for json Parser
val json =
requests.get("https://api.github.com/repos/blatzar/nicehttp").parsed<GithubJson>()
requests.get("https://api.github.com/repos/blatzar/nicehttp")
.parsed<GithubJson>()
println("JSON description: ${json.description}")

// Example for Async-ed Requests
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ publishing {
maven(MavenPublication) {
groupId 'com.lagradost'
artifactId 'nicehttp'
version '0.4.3'
version '0.4.4'

afterEvaluate {
from components.release
Expand Down
11 changes: 8 additions & 3 deletions library/src/main/java/com/lagradost/nicehttp/NiceResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package com.lagradost.nicehttp

import okhttp3.Request
import okhttp3.Response
import okhttp3.internal.closeQuietly
import org.jsoup.Jsoup
import org.jsoup.nodes.Document


val Response.cookies: Map<String, String>
get() = this.headers.getCookies("set-cookie")

Expand All @@ -16,10 +16,15 @@ class NiceResponse(
val okhttpResponse: Response,
val parser: ResponseParser?
) {
/** Lazy, initialized on use. Returns empty string on null. */
val text by lazy { okhttpResponse.body.string() }
/** Lazy, initialized on use. Returns empty string on null. Automatically closes the body! */
val text by lazy {
body.string().also {
body.closeQuietly()
}
}
val url by lazy { okhttpResponse.request.url.toString() }
val cookies by lazy { okhttpResponse.cookies }
/** Remember to close the body! */
val body by lazy { okhttpResponse.body }

/** Return code */
Expand Down

0 comments on commit dc80907

Please sign in to comment.