From b525f8d29ca465f718ee504a9d79c47521ed916d Mon Sep 17 00:00:00 2001 From: tangcent Date: Sun, 30 Jun 2024 15:43:38 +0800 Subject: [PATCH] chore: polish Postman API helper classes and cache management --- .../com/itangcent/cache/CacheSwitcher.kt | 6 + .../export/postman/DefaultPostmanApiHelper.kt | 218 ++++++++++-------- .../api/export/postman/PostmanApiHelper.kt | 41 +++- .../export/postman/PostmanCachedApiHelper.kt | 124 +++++++--- 4 files changed, 251 insertions(+), 138 deletions(-) diff --git a/idea-plugin/src/main/kotlin/com/itangcent/cache/CacheSwitcher.kt b/idea-plugin/src/main/kotlin/com/itangcent/cache/CacheSwitcher.kt index f1b22a6b..389f52e0 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/cache/CacheSwitcher.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/cache/CacheSwitcher.kt @@ -5,8 +5,14 @@ package com.itangcent.cache */ interface CacheSwitcher { + /** + * Disables the use of cache for subsequent operations. + */ fun notUserCache() + /** + * Enables the use of cache for subsequent operations. + */ fun userCache() } diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/DefaultPostmanApiHelper.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/DefaultPostmanApiHelper.kt index 302d011b..97af7da0 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/DefaultPostmanApiHelper.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/DefaultPostmanApiHelper.kt @@ -1,12 +1,14 @@ package com.itangcent.idea.plugin.api.export.postman -import com.google.gson.JsonObject import com.google.gson.internal.LazilyParsedNumber import com.google.inject.Inject import com.itangcent.common.logger.Log import com.itangcent.common.logger.traceError import com.itangcent.common.utils.* -import com.itangcent.http.* +import com.itangcent.http.HttpRequest +import com.itangcent.http.HttpResponse +import com.itangcent.http.RawContentType +import com.itangcent.http.contentType import com.itangcent.idea.plugin.settings.helper.PostmanSettingsHelper import com.itangcent.idea.utils.GsonExUtils import com.itangcent.intellij.context.ActionContext @@ -48,17 +50,14 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { @Inject protected lateinit var actionContext: ActionContext - private val apiThrottle: Throttle = ThrottleHelper().build("postman_api") - - protected open fun beforeRequest(request: HttpRequest) { - apiThrottle.acquireGreedy(LIMIT_PERIOD_PRE_REQUEST) - } - - private fun onErrorResponse(response: HttpResponse) { - - val returnValue = response.string() + /** + * Handles error responses from the Postman API. + * Logs the error and provides appropriate messages. + */ + private fun HttpResponse.onError() { + val returnValue = string() if (returnValue.isNullOrBlank()) { - logger.error("No Response For: ${response.request().url()}") + logger.error("No Response For: ${request().url()}") return } if (returnValue.contains("AuthenticationError") @@ -68,25 +67,21 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { return } - if (returnValue.contains("WLError") - || returnValue.contains("attribute is invalid") + if ( + (returnValue.contains("WLError") + || returnValue.contains("attribute is invalid")) + && postmanSettingsHelper.buildExample() ) { logger.error("Please try after turning off [build example] at Preferences(Settings) > Other Settings > EasyApi > Postman > build example") return } val returnObj = returnValue.asJsonElement() - val errorName = returnObj - .sub("error") - .sub("name") - ?.asString - val errorMessage = (returnObj as? JsonObject) - .sub("error") - .sub("message") - ?.asString - - if (response.code() == 429) { + val errorObj = returnObj.sub("error") + val errorName = errorObj.sub("name")?.asString + val errorMessage = errorObj.sub("message")?.asString + if (code() == 429) { if (errorName == null) { logger.error("$errorMessage \n $LIMIT_ERROR_MSG") return @@ -104,28 +99,25 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { logger.error("Error Response:$returnValue") } - protected open fun getHttpClient(): HttpClient { - return httpClientProvider!!.getHttpClient() - } + protected open val httpClient by lazy { httpClientProvider!!.getHttpClient() } /** + * Creates a collection in Postman. * @return collection id */ override fun createCollection(collection: HashMap, workspaceId: String?): Map? { LOG.info("create collection in workspace $workspaceId to postman") - val request = getHttpClient() + val request = httpClient .post(COLLECTION) .contentType(RawContentType.APPLICATION_JSON) - .header("x-api-key", postmanSettingsHelper.getPrivateToken()) .body(linkedMapOf("collection" to collection)) workspaceId?.let { request.query("workspace", it) } try { - beforeRequest(request) - call(request).use { response -> + request.callPostman().use { response -> val returnValue = response.string() - if (returnValue.notNullOrEmpty() && returnValue!!.contains("collection")) { + if (returnValue?.contains("collection") == true) { val returnObj = returnValue.asJsonElement() val collectionInfo = returnObj.sub("collection")?.asMap() if (collectionInfo.notNullOrEmpty()) { @@ -133,7 +125,7 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { } } - onErrorResponse(response) + response.onError() return null } @@ -143,6 +135,10 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { } } + /** + * Updates a collection in Postman. + * @return true if successful, false otherwise. + */ override fun updateCollection(collectionId: String, collectionInfo: Map): Boolean { LOG.info("update collection $collectionId to postman") if (doUpdateCollection(collectionId, collectionInfo)) { @@ -164,10 +160,13 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { return false } + /** + * Attempts to fix the collection data by ensuring proper types for certain fields. + */ @Suppress("UNCHECKED_CAST") - fun tryFixCollection(apiInfo: MutableMap) { + private fun tryFixCollection(apiInfo: MutableMap) { if (apiInfo.containsKey("item")) { - val items = apiInfo["item"] as List<*>? ?: return + val items = apiInfo["item"] as? List<*>? ?: return apiInfo["item"] = items .asSequence() .map { it as Map } @@ -199,19 +198,20 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { } } + /** + * Performs the actual update of the collection in Postman. + * @return true if successful, false otherwise. + */ private fun doUpdateCollection(collectionId: String, apiInfo: Map): Boolean { - val request = getHttpClient().put("$COLLECTION/$collectionId") + val request = httpClient.put("$COLLECTION/$collectionId") .contentType(RawContentType.APPLICATION_JSON) - .header("x-api-key", postmanSettingsHelper.getPrivateToken()) .body(GsonUtils.toJson(linkedMapOf("collection" to apiInfo)).apply { GsonExUtils.resolveGsonLazily(this) }) try { - beforeRequest(request) - - call(request).use { response -> + request.callPostman().use { response -> val returnValue = response.string() - if (returnValue.notNullOrEmpty() && returnValue!!.contains("collection")) { + if (returnValue?.contains("collection") == true) { val returnObj = returnValue.asJsonElement() val collectionName = returnObj .sub("collection") @@ -222,7 +222,7 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { } } - onErrorResponse(response) + response.onError() return false } } catch (e: Throwable) { @@ -232,16 +232,18 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { } } + /** + * Retrieves all collections from Postman. + * @return list of collections if successful, null otherwise. + */ override fun getAllCollection(): List>? { LOG.info("read all collection from postman") - val request = getHttpClient().get(COLLECTION) - .header("x-api-key", postmanSettingsHelper.getPrivateToken()) + val request = httpClient.get(COLLECTION) try { - beforeRequest(request) - call(request).use { response -> + request.callPostman().use { response -> val returnValue = response.string() - if (returnValue.notNullOrEmpty() && returnValue!!.contains("collections")) { + if (returnValue?.contains("collections") == true) { val returnObj = returnValue.asJsonElement() val collections = returnObj.sub("collections") ?.asJsonArray ?: return null @@ -250,7 +252,7 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { return collectionList } - onErrorResponse(response) + response.onError() return null } @@ -261,16 +263,18 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { } } + /** + * Retrieves all collections in a specific workspace from Postman. + * @return list of collections if successful, null otherwise. + */ override fun getCollectionByWorkspace(workspaceId: String): List>? { LOG.info("read collection in workspace [$workspaceId] from postman") - val request = getHttpClient().get("$WORKSPACE/$workspaceId") - .header("x-api-key", postmanSettingsHelper.getPrivateToken()) + val request = httpClient.get("$WORKSPACE/$workspaceId") try { - beforeRequest(request) - call(request).use { response -> + request.callPostman().use { response -> val returnValue = response.string() - if (returnValue.notNullOrEmpty() && returnValue!!.contains("workspace")) { + if (returnValue?.contains("workspace") == true) { val returnObj = returnValue.asJsonElement() val collections = returnObj .sub("workspace") @@ -281,7 +285,7 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { return collectionList } - onErrorResponse(response) + response.onError() return null } @@ -291,23 +295,25 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { } } + /** + * Retrieves information about a specific collection from Postman. + * @return collection information if successful, null otherwise. + */ override fun getCollectionInfo(collectionId: String): Map? { LOG.info("read collection of $collectionId from postman") - val request = getHttpClient().get("$COLLECTION/$collectionId") - .header("x-api-key", postmanSettingsHelper.getPrivateToken()) + val request = httpClient.get("$COLLECTION/$collectionId") try { - beforeRequest(request) - call(request).use { response -> + request.callPostman().use { response -> val returnValue = response.string() - if (returnValue.notNullOrEmpty() && returnValue!!.contains("collection")) { + if (returnValue?.contains("collection") == true) { val returnObj = returnValue.asJsonElement() return returnObj .sub("collection") ?.asMap() } - onErrorResponse(response) + response.onError() return null } @@ -318,38 +324,30 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { } } + /** + * Retrieves all workspaces from Postman. + * @return list of workspaces if successful, null otherwise. + */ override fun getAllWorkspaces(): List? { if (postmanSettingsHelper.getPrivateToken() == null) { return null } LOG.info("read allWorkspaces from postman") - val request = getHttpClient().get(WORKSPACE) - .header("x-api-key", postmanSettingsHelper.getPrivateToken()) + val request = httpClient.get(WORKSPACE) try { - beforeRequest(request) - call(request).use { response -> + request.callPostman().use { response -> val returnValue = response.string() - if (returnValue.notNullOrEmpty() && returnValue!!.contains("workspaces")) { + if (returnValue?.contains("workspaces") == true) { val returnObj = returnValue.asJsonElement() val workspaces = returnObj.sub("workspaces") ?.asJsonArray ?: return null - val workspaceList = mutableListOf() - workspaces - .forEach { - val res = it.asMap() - workspaceList.add( - PostmanWorkspace( - res["id"] as String, - res["name"] as String, - res["type"] as String - ) - ) - } - return workspaceList + return workspaces + .map { it.asMap() } + .map { it.parsePostmanWorkspace() } } - onErrorResponse(response) + response.onError() return null } @@ -360,29 +358,25 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { } } + /** + * Retrieves information about a specific workspace from Postman. + * @return workspace information if successful, null otherwise. + */ override fun getWorkspaceInfo(workspaceId: String): PostmanWorkspace? { LOG.info("read workspaceInfo of $workspaceId from postman") - val request = getHttpClient().get("$WORKSPACE/$workspaceId") - .header("x-api-key", postmanSettingsHelper.getPrivateToken()) + val request = httpClient.get("$WORKSPACE/$workspaceId") try { - beforeRequest(request) - call(request).use { response -> + request.callPostman().use { response -> val returnValue = response.string() - if (returnValue.notNullOrEmpty() && returnValue!!.contains("workspace")) { + if (returnValue?.contains("workspace") == true) { val returnObj = returnValue.asJsonElement() return returnObj.sub("workspace") ?.asMap() - ?.let { - PostmanWorkspace( - it["id"] as String, - it["name"] as String, - it["type"] as String - ) - } + ?.parsePostmanWorkspace() } - onErrorResponse(response) + response.onError() return null } @@ -392,23 +386,34 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { } } + /** + * parse a map into a PostmanWorkspace object. + */ + private fun Map.parsePostmanWorkspace() = PostmanWorkspace( + id = this["id"] as String, + name = this["name"] as String, + type = this["type"] as String + ) + + /** + * Deletes a collection in Postman. + * @return collection information if successful, null otherwise. + */ override fun deleteCollectionInfo(collectionId: String): Map? { LOG.info("delete collection $collectionId from postman") - val request = getHttpClient().delete("$COLLECTION/$collectionId") - .header("x-api-key", postmanSettingsHelper.getPrivateToken()) + val request = httpClient.delete("$COLLECTION/$collectionId") try { - beforeRequest(request) - call(request).use { response -> + request.callPostman().use { response -> val returnValue = response.string() - if (returnValue.notNullOrEmpty() && returnValue!!.contains("collection")) { + if (returnValue?.contains("collection") == true) { val returnObj = returnValue.asJsonElement() return returnObj .sub("collection") ?.asMap() } - onErrorResponse(response) + response.onError() return null } @@ -418,12 +423,21 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { } } + private val apiThrottle: Throttle = ThrottleHelper().build("postman_api") + private val semaphore = Semaphore(5) - protected fun call(request: HttpRequest): HttpResponse { + /** + * Executes a Postman API call with throttling and concurrency control. + */ + private fun HttpRequest.callPostman(): HttpResponse { + //throttling API requests to avoid hitting rate limits. + apiThrottle.acquireGreedy(LIMIT_PERIOD_PRE_REQUEST) + semaphore.acquire() try { - return request.call() + return header("x-api-key", postmanSettingsHelper.getPrivateToken()) + .call() } finally { semaphore.release() } @@ -436,8 +450,8 @@ open class DefaultPostmanApiHelper : PostmanApiHelper { const val COLLECTION = "$POSTMAN_HOST/collections" const val WORKSPACE = "$POSTMAN_HOST/workspaces" - //the postman rate limit is 60 per/s - //Just to be on the safe side,limit to 30 per/s + // The Postman rate limit is 60 per second. + // To be safe, limit it to 30 per second. private val LIMIT_PERIOD_PRE_REQUEST = TimeUnit.MINUTES.toMillis(1) / 30 private const val LIMIT_ERROR_MSG = "API access rate limits are applied at a per-key basis in unit time.\n" + diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/PostmanApiHelper.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/PostmanApiHelper.kt index 3e4191e7..720425c6 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/PostmanApiHelper.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/PostmanApiHelper.kt @@ -4,28 +4,65 @@ import com.google.inject.ImplementedBy import com.itangcent.idea.plugin.api.export.postman.Emojis.PERSONAL import com.itangcent.idea.plugin.api.export.postman.Emojis.TEAM +/** + * A helper class provides methods to manage collections and workspaces in Postman. + */ @ImplementedBy(DefaultPostmanApiHelper::class) interface PostmanApiHelper { /** - * @return collection id + * Creates a new collection in Postman. + * @param collection The collection details to create. + * @param workspaceId Optional workspace ID where the collection will be created. + * @return The created collection details or null if creation failed. */ fun createCollection(collection: HashMap, workspaceId: String?): Map? + /** + * Updates an existing collection in Postman. + * @param collectionId The ID of the collection to update. + * @param collectionInfo The new collection details. + * @return True if the update was successful, false otherwise. + */ fun updateCollection(collectionId: String, collectionInfo: Map): Boolean /** - * On successful deletion of the collection, return the id and uid. + * Deletes a collection in Postman. + * @param collectionId The ID of the collection to delete. + * @return The deleted collection details or null if deletion failed. */ fun deleteCollectionInfo(collectionId: String): Map? + /** + * Retrieves all collections from Postman. + * @return A list of all collections or null if retrieval failed. + */ fun getAllCollection(): List>? + /** + * Retrieves all collections within a specific workspace from Postman. + * @param workspaceId The ID of the workspace to retrieve collections from. + * @return A list of collections in the specified workspace or null if retrieval failed. + */ fun getCollectionByWorkspace(workspaceId: String): List>? + /** + * Retrieves information about a specific collection from Postman. + * @param collectionId The ID of the collection to retrieve. + * @return The collection details or null if retrieval failed. + */ fun getCollectionInfo(collectionId: String): Map? + /** + * Retrieves all workspaces from Postman. + * @return A list of all workspaces or null if retrieval failed. + */ fun getAllWorkspaces(): List? + /** + * Retrieves information about a specific workspace from Postman. + * @param workspaceId The ID of the workspace to retrieve. + * @return The workspace details or null if retrieval failed. + */ fun getWorkspaceInfo(workspaceId: String): PostmanWorkspace? } diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/PostmanCachedApiHelper.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/PostmanCachedApiHelper.kt index 1b01ecc6..3df1a023 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/PostmanCachedApiHelper.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/postman/PostmanCachedApiHelper.kt @@ -8,14 +8,15 @@ import com.itangcent.common.utils.notNullOrEmpty import com.itangcent.idea.binder.DbBeanBinderFactory import com.itangcent.idea.plugin.settings.helper.PostmanSettingsHelper import com.itangcent.intellij.file.LocalFileRepository -import com.itangcent.intellij.logger.Logger +/** + * A helper class for interacting with the Postman API with caching capabilities. + * This class extends the DefaultPostmanApiHelper to provide additional caching functionality + * for various Postman API operations such as creating, updating, deleting, and retrieving collections. + */ @Singleton class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { - @Inject - private lateinit var logger: Logger - private var readCache: Boolean = true @Inject @@ -24,19 +25,14 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { @Inject private lateinit var postmanSettingsHelper: PostmanSettingsHelper - private var dbBeanBinderFactory: DbBeanBinderFactory? = null - - private fun getDbBeanBinderFactory(): DbBeanBinderFactory { - if (dbBeanBinderFactory == null) { - synchronized(this) { - dbBeanBinderFactory = - DbBeanBinderFactory(localFileRepository!!.getOrCreateFile(".api.postman.v1.1.db").path) - { NULL_POSTMAN_INFO_CACHE } - } - } - return this.dbBeanBinderFactory!! + private val dbBeanBinderFactory: DbBeanBinderFactory by lazy { + DbBeanBinderFactory(localFileRepository!!.getOrCreateFile(".api.postman.v1.1.db").path) { NULL_POSTMAN_INFO_CACHE } } + /** + * Creates a new collection in Postman and updates the local cache. + * @return The created collection details or null if creation failed. + */ override fun createCollection(collection: HashMap, workspaceId: String?): Map? { val createdCollection: Map = super.createCollection(collection, workspaceId) ?: return null @@ -45,7 +41,7 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { collection["id"] = collectionId //region update collection of AllCollection----------------------------- - val allCollectionBeanBinder = getDbBeanBinderFactory() + val allCollectionBeanBinder = dbBeanBinderFactory .getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_getAllCollection") val cacheOfAllCollection = allCollectionBeanBinder.read() if (cacheOfAllCollection != NULL_POSTMAN_INFO_CACHE) { @@ -61,7 +57,7 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { val cacheOfCollection = PostmanInfoCache() cacheOfCollection.collectionDetail = collection - getDbBeanBinderFactory() + dbBeanBinderFactory .getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_collection:$collectionId") .save(cacheOfCollection) //endregion add cache of created collection-------------------------------- @@ -70,11 +66,15 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { } + /** + * Updates an existing collection in Postman and updates the local cache. + * @return True if the update was successful, false otherwise. + */ @Suppress("UNCHECKED_CAST") override fun updateCollection(collectionId: String, collectionInfo: Map): Boolean { if (super.updateCollection(collectionId, collectionInfo)) { //region try update collection of AllCollection----------------------------- - val allCollectionBeanBinder = getDbBeanBinderFactory() + val allCollectionBeanBinder = dbBeanBinderFactory .getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_getAllCollection") val cacheOfAllCollection = allCollectionBeanBinder.read() if (cacheOfAllCollection != NULL_POSTMAN_INFO_CACHE) { @@ -95,7 +95,7 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { //endregion try update collection of AllCollection----------------------------- val collectionDetailBeanBinder = - getDbBeanBinderFactory().getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_collection:$collectionId") + dbBeanBinderFactory.getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_collection:$collectionId") val cache = PostmanInfoCache() cache.collectionDetail = collectionInfo collectionDetailBeanBinder.save(cache) @@ -104,12 +104,16 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { return false } + /** + * Deletes a collection in Postman and updates the local cache. + * @return The deleted collection details or null if deletion failed. + */ override fun deleteCollectionInfo(collectionId: String): Map? { val deleteCollectionInfo = super.deleteCollectionInfo(collectionId) if (deleteCollectionInfo != null) { //region update collection of AllCollection----------------------------- - val allCollectionBeanBinder = getDbBeanBinderFactory() + val allCollectionBeanBinder = dbBeanBinderFactory .getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_getAllCollection") val cacheOfAllCollection = allCollectionBeanBinder.read() if (cacheOfAllCollection != NULL_POSTMAN_INFO_CACHE) { @@ -123,18 +127,27 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { } //endregion update collection of AllCollection----------------------------- - getDbBeanBinderFactory().deleteBinder("${postmanSettingsHelper.getPrivateToken()}_collection:$collectionId") + dbBeanBinderFactory.deleteBinder("${postmanSettingsHelper.getPrivateToken()}_collection:$collectionId") } return deleteCollectionInfo } + /** + * Retrieves all collections from Postman, optionally using the cache. + * @return A list of all collections or null if retrieval failed. + */ override fun getAllCollection(): List>? { return getAllCollection(true) } + /** + * Retrieves all collections from Postman, with an option to use the cache. + * @param useCache Whether to use the cached data or not. + * @return A list of all collections or null if retrieval failed. + */ fun getAllCollection(useCache: Boolean): List>? { if (useCache && this.readCache) { - val allCollectionBeanBinder = getDbBeanBinderFactory().getBeanBinder( + val allCollectionBeanBinder = dbBeanBinderFactory.getBeanBinder( "${postmanSettingsHelper.getPrivateToken()}_getAllCollection" ) val cache = allCollectionBeanBinder.read() @@ -149,7 +162,7 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { cache.allCollection = allCollection.map { it.toMutableMap() }.toMutableList() - val beanBinder = getDbBeanBinderFactory() + val beanBinder = dbBeanBinderFactory .getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_getAllCollection") //try clean cache @@ -169,7 +182,7 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { .toList() deletedCollections.forEach { - getDbBeanBinderFactory().deleteBinder("${postmanSettingsHelper.getPrivateToken()}_collection:$it") + dbBeanBinderFactory.deleteBinder("${postmanSettingsHelper.getPrivateToken()}_collection:$it") } } @@ -178,14 +191,24 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { return allCollection } + /** + * Retrieves information about a specific collection from Postman, optionally using the cache. + * @return The collection details or null if retrieval failed. + */ override fun getCollectionInfo(collectionId: String): Map? { return getCollectionInfo(collectionId, true) } + /** + * Retrieves information about a specific collection from Postman, with an option to use the cache. + * @param collectionId The ID of the collection to retrieve. + * @param useCache Whether to use the cached data or not. + * @return The collection details or null if retrieval failed. + */ fun getCollectionInfo(collectionId: String, useCache: Boolean = true): Map? { if (useCache && this.readCache) { val collectionDetailBeanBinder = - getDbBeanBinderFactory().getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_collection:$collectionId") + dbBeanBinderFactory.getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_collection:$collectionId") val cache = collectionDetailBeanBinder.read() if (cache != NULL_POSTMAN_INFO_CACHE) { LOG.debug("read cache of collection $collectionId") @@ -196,19 +219,29 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { val collectionDetail = super.getCollectionInfo(collectionId) val cache = PostmanInfoCache() cache.collectionDetail = collectionDetail - getDbBeanBinderFactory() + dbBeanBinderFactory .getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_collection:$collectionId") .save(cache) return collectionDetail } + /** + * Retrieves all collections in a specific workspace from Postman, optionally using the cache. + * @return A list of collections in the specified workspace or null if retrieval failed. + */ override fun getCollectionByWorkspace(workspaceId: String): List>? { return getCollectionByWorkspace(workspaceId, true) } + /** + * Retrieves all collections in a specific workspace from Postman, with an option to use the cache. + * @param workspaceId The ID of the workspace to retrieve collections from. + * @param useCache Whether to use the cached data or not. + * @return A list of collections in the specified workspace or null if retrieval failed. + */ fun getCollectionByWorkspace(workspaceId: String, useCache: Boolean): List>? { if (useCache && this.readCache) { - val workspaceInfoBeanBinder = getDbBeanBinderFactory() + val workspaceInfoBeanBinder = dbBeanBinderFactory .getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_collectionByWorkspace:$workspaceId") val cache = workspaceInfoBeanBinder.read() if (cache != NULL_POSTMAN_INFO_CACHE) { @@ -220,7 +253,7 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { val cache = PostmanInfoCache() cache.allCollection = collectionInWorkspace?.map { it.toMutableMap() }?.toMutableList() - val beanBinder = getDbBeanBinderFactory() + val beanBinder = dbBeanBinderFactory .getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_collectionByWorkspace:$workspaceId") if (!useCache) { //try clean collection info cache @@ -238,7 +271,7 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { .toList() deletedCollections.forEach { - getDbBeanBinderFactory().deleteBinder("${postmanSettingsHelper.getPrivateToken()}_collection:$it") + dbBeanBinderFactory.deleteBinder("${postmanSettingsHelper.getPrivateToken()}_collection:$it") } } } @@ -248,8 +281,13 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { return collectionInWorkspace } + + /** + * Retrieves all collections in a specific workspace from Postman, optionally using the cache. + * @return A list of collections in the specified workspace or null if retrieval failed. + */ override fun getWorkspaceInfo(workspaceId: String): PostmanWorkspace? { - val workspaceInfoBeanBinder = getDbBeanBinderFactory() + val workspaceInfoBeanBinder = dbBeanBinderFactory .getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_workspace:$workspaceId") var cache = workspaceInfoBeanBinder.read() if (cache != NULL_POSTMAN_INFO_CACHE) { @@ -259,19 +297,28 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { val workspace = super.getWorkspaceInfo(workspaceId) cache = PostmanInfoCache() cache.workspaceDetail = workspace - getDbBeanBinderFactory() + dbBeanBinderFactory .getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_workspace:$workspaceId") .save(cache) return workspace } + /** + * Retrieves all workspaces from Postman, optionally using the cache. + * @return A list of all workspaces or null if retrieval failed. + */ override fun getAllWorkspaces(): List? { return getAllWorkspaces(true) } + /** + * Retrieves all workspaces from Postman, with an option to use the cache. + * @param useCache Whether to use the cached data or not. + * @return A list of all workspaces or null if retrieval failed. + */ fun getAllWorkspaces(useCache: Boolean): List? { if (useCache && this.readCache) { - val allWorkspacesBeanBinder = getDbBeanBinderFactory().getBeanBinder( + val allWorkspacesBeanBinder = dbBeanBinderFactory.getBeanBinder( "${postmanSettingsHelper.getPrivateToken()}_getAllWorkspaces" ) val cache = allWorkspacesBeanBinder.read() @@ -285,7 +332,7 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { val cache = PostmanInfoCache() cache.allWorkspace = allWorkspaces - val beanBinder = getDbBeanBinderFactory() + val beanBinder = dbBeanBinderFactory .getBeanBinder("${postmanSettingsHelper.getPrivateToken()}_getAllWorkspaces") //try clean cache @@ -305,7 +352,7 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { .toList() deletedCollections.forEach { - getDbBeanBinderFactory().deleteBinder("${postmanSettingsHelper.getPrivateToken()}_workspace:$it") + dbBeanBinderFactory.deleteBinder("${postmanSettingsHelper.getPrivateToken()}_workspace:$it") } } @@ -314,14 +361,23 @@ class PostmanCachedApiHelper : DefaultPostmanApiHelper(), CacheSwitcher { return allWorkspaces } + /** + * Disables the use of cache for subsequent operations. + */ override fun notUserCache() { readCache = false } + /** + * Enables the use of cache for subsequent operations. + */ override fun userCache() { readCache = true } + /** + * Data class for caching Postman API responses. + */ data class PostmanInfoCache( var allCollection: MutableList>? = null, var collectionDetail: Map? = null,