Skip to content

Commit

Permalink
feat: added functionality to choose between apache and okHttp for sen…
Browse files Browse the repository at this point in the history
…ding HTTP requests via settings
  • Loading branch information
tangcent committed Apr 28, 2024
1 parent 0f706c1 commit 4460d00
Show file tree
Hide file tree
Showing 17 changed files with 508 additions and 93 deletions.
38 changes: 8 additions & 30 deletions common-api/src/main/kotlin/com/itangcent/http/ApacheHttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ open class ApacheHttpClient : HttpClient {

private val httpClient: org.apache.http.client.HttpClient

constructor() {
val basicCookieStore = BasicCookieStore()
this.apacheCookieStore = ApacheCookieStore(basicCookieStore)
this.httpClientContext!!.cookieStore = basicCookieStore
this.httpClient = HttpClients.custom()
constructor() : this(
HttpClients.custom()
.setConnectionManager(PoolingHttpClientConnectionManager().also {
it.maxTotal = 50
it.defaultMaxPerRoute = 20
Expand All @@ -73,10 +70,8 @@ open class ApacheHttpClient : HttpClient {
.setSocketTimeout(30 * 1000)
.setCookieSpec(CookieSpecs.STANDARD).build()
)
.setSSLHostnameVerifier(NOOP_HOST_NAME_VERIFIER)
.setSSLSocketFactory(SSLSF)
.build()
}
)

constructor(httpClient: org.apache.http.client.HttpClient) {
val basicCookieStore = BasicCookieStore()
Expand Down Expand Up @@ -187,13 +182,7 @@ open class ApacheHttpClient : HttpClient {
}

@ScriptTypeName("request")
class ApacheHttpRequest : AbstractHttpRequest {

private val apacheHttpClient: ApacheHttpClient

constructor(apacheHttpClient: ApacheHttpClient) : super() {
this.apacheHttpClient = apacheHttpClient
}
class ApacheHttpRequest(private val apacheHttpClient: ApacheHttpClient) : AbstractHttpRequest() {

/**
* Executes HTTP request using the [apacheHttpClient].
Expand All @@ -214,13 +203,7 @@ fun HttpRequest.contentType(contentType: ContentType): HttpRequest {
* The implement of [CookieStore] by [org.apache.http.client.CookieStore].
*/
@ScriptTypeName("cookieStore")
class ApacheCookieStore : CookieStore {

private var cookieStore: org.apache.http.client.CookieStore

constructor(cookieStore: org.apache.http.client.CookieStore) {
this.cookieStore = cookieStore
}
class ApacheCookieStore(private var cookieStore: org.apache.http.client.CookieStore) : CookieStore {

/**
* Adds an [Cookie], replacing any existing equivalent cookies.
Expand Down Expand Up @@ -281,7 +264,7 @@ class ApacheHttpResponse(
*
* @return the status of the response, or {@code null} if not yet set
*/
override fun code(): Int? {
override fun code(): Int {
val statusLine = response.statusLine
return statusLine.statusCode
}
Expand Down Expand Up @@ -353,17 +336,12 @@ class ApacheHttpResponse(
* The implement of [Cookie] by [org.apache.http.cookie.Cookie].
*/
@ScriptTypeName("cookie")
class ApacheCookie : Cookie {
private val cookie: org.apache.http.cookie.Cookie
class ApacheCookie(private val cookie: org.apache.http.cookie.Cookie) : Cookie {

fun getWrapper(): org.apache.http.cookie.Cookie {
return cookie
}

constructor(cookie: org.apache.http.cookie.Cookie) {
this.cookie = cookie
}

override fun getName(): String? {
return cookie.name
}
Expand Down Expand Up @@ -404,7 +382,7 @@ class ApacheCookie : Cookie {
return cookie.isSecure
}

override fun getVersion(): Int? {
override fun getVersion(): Int {
return cookie.version
}

Expand Down
4 changes: 4 additions & 0 deletions idea-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ dependencies {
// https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc
implementation("org.xerial:sqlite-jdbc:3.34.0")

// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
implementation("com.squareup.okhttp3:okhttp:4.12.0")


// https://search.maven.org/artifact/org.mockito.kotlin/mockito-kotlin/3.2.0/jar
testImplementation("org.mockito.kotlin:mockito-kotlin:3.2.0")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class YapiDashBoardAction : ApiExportAction("YapiDashBoard") {
builder.bind(YapiDashBoard::class) { it.singleton() }

builder.bind(YapiApiDashBoardExporter::class) { it.singleton() }
builder.bind(YapiApiHelper::class) { it.with(YapiCachedApiHelper::class).singleton() }
builder.bind(YapiApiHelper::class) { it.with(CachedYapiApiHelper::class).singleton() }
builder.bind(HttpClientProvider::class) { it.with(ConfigurableHttpClientProvider::class).singleton() }

//allow cache api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class YapiExportAction : ApiExportAction("Export Yapi") {
builder.bind(HttpClientProvider::class) { it.with(ConfigurableHttpClientProvider::class).singleton() }
builder.bind(LinkResolver::class) { it.with(YapiLinkResolver::class).singleton() }

builder.bind(YapiApiHelper::class) { it.with(YapiCachedApiHelper::class).singleton() }
builder.bind(YapiApiHelper::class) { it.with(CachedYapiApiHelper::class).singleton() }

builder.bind(ClassExporter::class) { it.with(CompositeClassExporter::class).singleton() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ open class SuvApiExporter {

builder.bind(LocalFileRepository::class) { it.with(DefaultLocalFileRepository::class).singleton() }

builder.bind(YapiApiHelper::class) { it.with(YapiCachedApiHelper::class).singleton() }
builder.bind(YapiApiHelper::class) { it.with(CachedYapiApiHelper::class).singleton() }

builder.bind(HttpClientProvider::class) { it.with(ConfigurableHttpClientProvider::class).singleton() }
builder.bind(LinkResolver::class) { it.with(YapiLinkResolver::class).singleton() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.util.concurrent.ConcurrentHashMap
* cache:
* projectToken -> projectId
*/
open class YapiCachedApiHelper : DefaultYapiApiHelper() {
open class CachedYapiApiHelper : DefaultYapiApiHelper() {

@Inject
private val localFileRepository: LocalFileRepository? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<grid id="b08f8" binding="rootPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="794" height="1439"/>
<xy x="20" y="20" width="922" height="1597"/>
</constraints>
<properties/>
<border type="none"/>
Expand Down Expand Up @@ -1407,7 +1407,7 @@
</grid>
</children>
</grid>
<grid id="1841c" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="1841c" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
Expand All @@ -1418,7 +1418,7 @@
<grid id="eb6fe" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="9" fill="0" indent="0" use-parent-layout="false">
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="9" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="30"/>
</grid>
</constraints>
Expand Down Expand Up @@ -1452,7 +1452,7 @@
<grid id="3f49d" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
Expand All @@ -1470,9 +1470,9 @@
</properties>
</component>
<grid id="7438f" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="15"/>
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
Expand All @@ -1496,6 +1496,66 @@
</grid>
</children>
</grid>
<grid id="593e9" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="9" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="1d0be" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="8" fill="2" indent="0" use-parent-layout="false">
<minimum-size width="310" height="-1"/>
<preferred-size width="310" height="-1"/>
<maximum-size width="310" height="-1"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="cfaac" class="javax.swing.JCheckBox" binding="unsafeSslCheckBox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="unsafeSsl"/>
</properties>
</component>
</children>
</grid>
<grid id="ad482" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="8" fill="2" indent="0" use-parent-layout="false">
<minimum-size width="310" height="-1"/>
<preferred-size width="310" height="-1"/>
<maximum-size width="310" height="-1"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="57a03" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="http client:"/>
</properties>
</component>
<component id="c4bb7" class="javax.swing.JComboBox" binding="httpClientComboBox">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
</children>
</grid>
</children>
</grid>
</children>
</grid>
</children>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import com.itangcent.idea.utils.isDoubleClick
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.extend.rx.ThrottleHelper
import com.itangcent.intellij.logger.Logger
import com.itangcent.suv.http.ConfigurableHttpClientProvider
import com.itangcent.common.utils.ResourceUtils
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.awt.event.MouseListener
Expand Down Expand Up @@ -165,6 +163,10 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() {

private var recommendedCheckBox: JCheckBox? = null

private var unsafeSslCheckBox: JCheckBox? = null

private var httpClientComboBox: JComboBox<String>? = null

private var httpTimeOutTextField: JTextField? = null

private var trustHostsTextArea: JTextArea? = null
Expand Down Expand Up @@ -253,6 +255,9 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() {
markdownFormatTypeComboBox!!.model =
DefaultComboBoxModel(MarkdownFormatType.values().mapToTypedArray { it.name })

httpClientComboBox!!.model =
DefaultComboBoxModel(HttpClientType.values().mapToTypedArray { it.name.lowercase().capitalize() })

//endregion general-----------------------------------------------------
}

Expand Down Expand Up @@ -300,6 +305,8 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() {
this.trustHostsTextArea!!.text = settings.trustHosts.joinToString(separator = "\n")
this.maxDeepTextField!!.text = settings.inferMaxDeep.toString()

this.unsafeSslCheckBox!!.isSelected = settings.unsafeSsl
this.httpClientComboBox!!.selectedItem = settings.httpClient
this.httpTimeOutTextField!!.text = settings.httpTimeOut.toString()

refresh()
Expand Down Expand Up @@ -606,8 +613,9 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() {
settings.yapiExportMode = yapiExportModeComboBox!!.selectedItem as? String ?: YapiExportMode.ALWAYS_UPDATE.name
settings.yapiReqBodyJson5 = yapiReqBodyJson5CheckBox!!.isSelected
settings.yapiResBodyJson5 = yapiResBodyJson5CheckBox!!.isSelected
settings.httpTimeOut =
httpTimeOutTextField!!.text.toIntOrNull() ?: ConfigurableHttpClientProvider.defaultHttpTimeOut
settings.unsafeSsl = unsafeSslCheckBox!!.isSelected
settings.httpClient = httpClientComboBox!!.selectedItem as? String ?: HttpClientType.APACHE.name
settings.httpTimeOut = httpTimeOutTextField!!.text.toIntOrNull() ?: 10
settings.useRecommendConfig = recommendedCheckBox!!.isSelected
settings.logLevel =
(logLevelComboBox!!.selected() ?: CommonSettingsHelper.CoarseLogLevel.LOW).getLevel()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.itangcent.idea.plugin.settings

/**
* @author joe.wu
* @date 2024/04/28
*/
enum class HttpClientType {
APACHE,
OKHTTP,
}
Loading

0 comments on commit 4460d00

Please sign in to comment.