-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for HubSpot custom objects (#3)
* Support for HubSpot custom objects * HubSpot SDK version change
- Loading branch information
Showing
10 changed files
with
198 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
hubspot/src/main/kotlin/com/goforboom/hubspot/domain/DataEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.goforboom.hubspot.domain | ||
|
||
import java.math.BigInteger | ||
import java.time.LocalDateTime | ||
|
||
interface DataEntity { | ||
val id: BigInteger | ||
|
||
// Jackson is not able to recognize generic in generic, | ||
// it means he is not able to cast two levels of generic (company and custom company props) | ||
val properties: Map<String, Any> | ||
|
||
val createdAt: LocalDateTime | ||
val updatedAt: LocalDateTime | ||
val archived: Boolean | ||
} |
17 changes: 7 additions & 10 deletions
17
hubspot/src/main/kotlin/com/goforboom/hubspot/domain/company/Company.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
package com.goforboom.hubspot.domain.company | ||
|
||
import com.goforboom.hubspot.domain.DataEntity | ||
import java.math.BigInteger | ||
import java.time.LocalDateTime | ||
|
||
class Company( | ||
val id: BigInteger, | ||
|
||
// Jackson is not able to recognize generic in generic, | ||
// it means he is not able to cast two levels of generic (company and custom company props) | ||
val properties: Map<String, Any>, | ||
|
||
val createdAt: LocalDateTime, | ||
val updatedAt: LocalDateTime, | ||
val archived: Boolean | ||
) | ||
override val id: BigInteger, | ||
override val properties: Map<String, Any>, | ||
override val createdAt: LocalDateTime, | ||
override val updatedAt: LocalDateTime, | ||
override val archived: Boolean | ||
) : DataEntity |
16 changes: 16 additions & 0 deletions
16
hubspot/src/main/kotlin/com/goforboom/hubspot/domain/customobject/CustomObject.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.goforboom.hubspot.domain.customobject | ||
|
||
import com.goforboom.hubspot.domain.DataEntity | ||
import java.math.BigInteger | ||
import java.time.LocalDateTime | ||
|
||
class CustomObject( | ||
override val id: BigInteger, | ||
override val properties: Map<String, Any>, | ||
override val createdAt: LocalDateTime, | ||
override val updatedAt: LocalDateTime, | ||
override val archived: Boolean | ||
) : DataEntity | ||
|
||
|
||
|
87 changes: 87 additions & 0 deletions
87
hubspot/src/main/kotlin/com/goforboom/hubspot/domain/customobject/CustomObjectClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.goforboom.hubspot.domain.customobject | ||
|
||
import com.goforboom.hubspot.model.http.RequestMethod | ||
import com.goforboom.hubspot.model.http.Requester | ||
import com.goforboom.hubspot.Client | ||
import com.goforboom.hubspot.ClientRequestCatalog | ||
import com.goforboom.hubspot.domain.customobject.exceptions.CustomObjectNotFoundException | ||
import com.goforboom.hubspot.model.http.exceptions.HttpRequestException | ||
import com.goforboom.hubspot.model.mapper.Mapper | ||
import java.math.BigInteger | ||
|
||
class CustomObjectClient( | ||
private val hubSpotClient: Client, | ||
private val customObjectEntity: String | ||
) { | ||
|
||
private fun buildUrlForObjectRecord(customObjectId: BigInteger): String { | ||
return ClientRequestCatalog.V3.CUSTOM_OBJECT_DETAIL | ||
.replace("{customObjectEntity}", customObjectEntity) | ||
.replace( | ||
"{customObjectId}", customObjectId.toString() | ||
) | ||
} | ||
|
||
|
||
fun <P> createCustomObjectRecord(request: CustomObjectRequest<P>): CustomObject { | ||
val requestUrl = ClientRequestCatalog.V3.CUSTOM_OBJECT.replace("{customObjectEntity}", customObjectEntity) | ||
|
||
val response = Requester.requestJson(hubSpotClient, RequestMethod.POST, requestUrl, emptyMap(), request) | ||
|
||
if (response.isSuccess) { | ||
return Mapper.mapToObject(response.body) | ||
} else { | ||
throw RuntimeException(response.body.toPrettyString()) | ||
} | ||
} | ||
|
||
|
||
@Throws( | ||
CustomObjectNotFoundException::class, | ||
HttpRequestException::class | ||
) | ||
fun findCustomObjectRecord(customObjectId: BigInteger): CustomObject { | ||
val requestUrl = this.buildUrlForObjectRecord(customObjectId) | ||
val response = Requester.requestJson(hubSpotClient, RequestMethod.GET, requestUrl) | ||
|
||
if (response.isSuccess) { | ||
return Mapper.mapToObject(response.body) | ||
} else { | ||
when (response.status) { | ||
404 -> throw CustomObjectNotFoundException(this.customObjectEntity, customObjectId) | ||
else -> throw HttpRequestException(response.status, response.statusText) | ||
} | ||
} | ||
} | ||
|
||
|
||
fun <P> changeCustomObjectRecord(customObjectId: BigInteger, request: CustomObjectRequest<P>): CustomObject { | ||
val requestUrl = this.buildUrlForObjectRecord(customObjectId) | ||
val response = Requester.requestJson(hubSpotClient, RequestMethod.PATCH, requestUrl, emptyMap(), request) | ||
|
||
if (response.isSuccess) { | ||
return Mapper.mapToObject(response.body) | ||
} else { | ||
when (response.status) { | ||
404 -> throw CustomObjectNotFoundException(this.customObjectEntity, customObjectId) | ||
else -> throw HttpRequestException(response.status, response.statusText) | ||
} | ||
} | ||
} | ||
|
||
@Throws( | ||
HttpRequestException::class | ||
) | ||
fun removeCustomObjectRecord(customObjectId: BigInteger) { | ||
val requestUrl = this.buildUrlForObjectRecord(customObjectId) | ||
|
||
// Unknown company returns HTTP code 204 | ||
val response = Requester.requestVoid(hubSpotClient, RequestMethod.DELETE, requestUrl) | ||
|
||
if (!response.isSuccess) { | ||
when (response.status) { | ||
else -> throw HttpRequestException(response.status, response.statusText) | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
hubspot/src/main/kotlin/com/goforboom/hubspot/domain/customobject/CustomObjectRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.goforboom.hubspot.domain.customobject | ||
|
||
data class CustomObjectRequest<P>( | ||
val properties: P | ||
) |
5 changes: 5 additions & 0 deletions
5
...main/kotlin/com/goforboom/hubspot/domain/customobject/exceptions/CustomObjectException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.goforboom.hubspot.domain.customobject.exceptions | ||
|
||
import com.goforboom.hubspot.exceptions.HubSpotException | ||
|
||
abstract class CustomObjectException(override val message: String) : HubSpotException(message) |
9 changes: 9 additions & 0 deletions
9
...lin/com/goforboom/hubspot/domain/customobject/exceptions/CustomObjectNotFoundException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.goforboom.hubspot.domain.customobject.exceptions | ||
|
||
import java.math.BigInteger | ||
|
||
class CustomObjectNotFoundException( | ||
customObjectEntity: String, | ||
customObjectId: BigInteger, | ||
override val message: String = "Custom object '$customObjectId' was not found in table '$customObjectEntity'." | ||
) : CustomObjectException(message) |