Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unirest-objectmapper-jackson to version 4.1.1, new functions in CompanyClient to search by a parameter or get all the companies #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions hubspot/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile


plugins {
kotlin("jvm") version "1.6.10"

Expand All @@ -22,14 +23,11 @@ repositories {
dependencies {
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.14.2")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.14.2")

implementation("com.konghq:unirest-java:3.13.6")
implementation("com.konghq:unirest-objectmapper-jackson:3.13.6")

implementation("com.konghq:unirest-java:3.14.5")
implementation("com.konghq:unirest-objectmapper-jackson:4.1.1")
implementation(
"org.jetbrains.kotlin:kotlin-stdlib:1.6.10"
)

testImplementation("junit:junit:4.13.2")
testImplementation("org.junit.vintage:junit-vintage-engine:5.8.2")
testImplementation("org.mockito.kotlin:mockito-kotlin:3.2.0")
Expand Down Expand Up @@ -65,7 +63,7 @@ publishing {
licenses {
license {
name.set("The Apache License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
url.set("https://www.apache.org/licenses/LICENSE-2.0.txt")
}
}

Expand Down Expand Up @@ -127,3 +125,4 @@ tasks.withType<KotlinCompile> {
jvmTarget = "11"
}
}

Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package org.boomevents.hubspot.domain.company

import com.fasterxml.jackson.databind.JsonNode
import org.boomevents.hubspot.model.http.RequestMethod
import org.boomevents.hubspot.model.http.Requester
import org.boomevents.hubspot.Client
import org.boomevents.hubspot.ClientRequestCatalog
import org.boomevents.hubspot.domain.company.exceptions.CompanyNotFoundException
import org.boomevents.hubspot.model.http.exceptions.HttpRequestException
import org.boomevents.hubspot.model.mapper.Mapper
import org.boomevents.hubspot.model.mapper.Mapper.objectMapper
import java.math.BigInteger

class CompanyClient(private val hubSpotClient: Client) {

companion object {
const val COMPANIES_PER_PAGE = 100
}

fun <P> createCompany(request: CompanyRequest<P>): Company {
val response = Requester.requestJson(hubSpotClient, RequestMethod.POST, ClientRequestCatalog.V3.COMPANIES, emptyMap(), request)

Expand All @@ -21,6 +27,43 @@ class CompanyClient(private val hubSpotClient: Client) {
}
}

@Throws(
HttpRequestException::class
)
/**
* @return List of companies
*/
fun getCompanies(): List<Company> {
var after: String? = null
val allCompanies = mutableListOf<Company>()

do {
val url = buildUrl(after)
val response = Requester.requestJson(hubSpotClient, RequestMethod.GET, url)

if (response.isSuccess) {
val page = Mapper.mapToList<Company>(response.body)
allCompanies.addAll(page)

val nextAfter = extractNextAfter(response.body.toString())
after = nextAfter
} else {
throw RuntimeException("Failed to fetch companies: ${response.statusText}")
}
} while (after != null)

return allCompanies
}

private fun buildUrl(after: String?): String {
val baseUrl = ClientRequestCatalog.V3.COMPANIES
return "$baseUrl?limit=$COMPANIES_PER_PAGE" + (after?.let { "&after=$it" } ?: "")
}

private fun extractNextAfter(json: String): String? {
val rootNode: JsonNode = objectMapper.readTree(json)
return rootNode.path("paging").path("next").path("after").asText(null)
}

@Throws(
CompanyNotFoundException::class,
Expand All @@ -30,20 +73,55 @@ class CompanyClient(private val hubSpotClient: Client) {
val requestUrl = ClientRequestCatalog.V3.COMPANIES_DETAIL.replace(
"{companyId}", companyId.toString()
)
return makeRequestFindCompany(requestUrl)
}

@Throws(
CompanyNotFoundException::class,
HttpRequestException::class
)
fun findCompanyByDomain(domain: String): Company {
val requestUrl = ClientRequestCatalog.V3.COMPANIES_DETAIL.replace(
"{domain}", domain
) + "?idProperty=domain"
return makeRequestFindCompany(requestUrl)
}

@Throws(
CompanyNotFoundException::class,
HttpRequestException::class
)
fun findCompanyByName(name: String): Company {
val requestUrl = ClientRequestCatalog.V3.COMPANIES_DETAIL.replace(
"{name}", name
) + "?idProperty=name"
return makeRequestFindCompany(requestUrl)
}

@Throws(
CompanyNotFoundException::class,
HttpRequestException::class
)
fun findCompanyByProperty(propertyName: String, propertyValue: String): Company {
val requestUrl = ClientRequestCatalog.V3.COMPANIES_DETAIL.replace(
"{companyId}", propertyValue
) + "?idProperty=$propertyName"
return makeRequestFindCompany(requestUrl)
}

private fun makeRequestFindCompany(requestUrl: String): Company {
val response = Requester.requestJson(hubSpotClient, RequestMethod.GET, requestUrl)

if (response.isSuccess) {
return Mapper.mapToObject(response.body)
} else {
when (response.status) {
404 -> throw CompanyNotFoundException(companyId)
404 -> throw CompanyNotFoundException()
else -> throw HttpRequestException(response.status, response.statusText)
}
}
}


fun <P> changeCompany(companyId: BigInteger, request: CompanyRequest<P>): Company {
val requestUrl = ClientRequestCatalog.V3.COMPANIES_DETAIL.replace(
"{companyId}", companyId.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.boomevents.hubspot.domain.company.exceptions
import java.math.BigInteger

class CompanyNotFoundException(
companyId: BigInteger,
override val message: String = "Company '$companyId' was not found."
) : CompanyException(message)
companyId: BigInteger? = null
) : CompanyException(
message = "Company ${companyId?.let { "'$it'" } ?: "was not found."}"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.boomevents.hubspot.model.http

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.KotlinFeature
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper

class CustomObjectMapper : kong.unirest.ObjectMapper {
internal val jacksonMapper: com.fasterxml.jackson.databind.ObjectMapper = jacksonObjectMapper()

init {
// Configure your Jackson ObjectMapper here
jacksonMapper.registerModule(
KotlinModule.Builder()
.withReflectionCacheSize(512)
.configure(KotlinFeature.NullToEmptyCollection, false)
.configure(KotlinFeature.NullToEmptyMap, false)
.configure(KotlinFeature.NullIsSameAsDefault, false)
.configure(KotlinFeature.SingletonSupport, false)
.configure(KotlinFeature.StrictNullChecks, false)
.build()
)
.registerModule(JavaTimeModule())
}

override fun writeValue(value: Any): String {
return jacksonMapper.writeValueAsString(value)
}

override fun <T> readValue(value: String, valueType: Class<T>): T {
return jacksonMapper.readValue(value, valueType)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object Requester {
request
.header("Authorization", "Bearer ${client.apiKey}")
.withObjectMapper(
JacksonObjectMapper(Mapper.objectMapper)
CustomObjectMapper()
)

return request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.boomevents.hubspot.model.http.exceptions
import org.boomevents.hubspot.exceptions.HubSpotException

class HttpRequestException(
val responseCode: Int,
val responseMessage: String,
private val responseCode: Int,
private val responseMessage: String,
override val message: String = "Unknown HTTP error with code '$responseCode' and message '$responseMessage' received."
) : HubSpotException(message)
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,19 @@ package org.boomevents.hubspot.model.mapper

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.KotlinModule
import kong.unirest.JsonNode
import org.boomevents.hubspot.model.http.CustomObjectMapper

object Mapper {

val objectMapper: ObjectMapper = ObjectMapper()
.registerModule(
JavaTimeModule() // Support Java 8 modern datetime
)
.registerModule(
KotlinModule()
)
val objectMapper: ObjectMapper = CustomObjectMapper().jacksonMapper

inline fun <reified R> mapToObject(jsonNode: JsonNode): R {
return objectMapper.readValue(jsonNode.toString(), object : TypeReference<R>() {})
}

inline fun <reified R> mapToList(jsonNode: JsonNode): List<R> {
val resultsNode = jsonNode.`object`.get("results")
val resultsNode = jsonNode.`object`["results"]
return objectMapper.readValue(resultsNode.toString(), object : TypeReference<List<R>>() {})
}
}