Skip to content

Commit

Permalink
light implementation of contacts (#7)
Browse files Browse the repository at this point in the history
* light implementation of contacts

* fix package name & copy/paste typos

* package version & docs
  • Loading branch information
dulajo committed Apr 17, 2023
1 parent a39ce25 commit 31f2307
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ General implementation of [HubSpot](https://developers.hubspot.com/docs/api/crm/
| Company ||||||
| Custom objects ||||||
| Deal ||||||
| Contact || | | | |
| Contact || | | | |

## Supported types

Expand Down
2 changes: 1 addition & 1 deletion hubspot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
}

group = "org.boomevents"
version = "1.3.1"
version = "1.4.0"

java.sourceCompatibility = JavaVersion.VERSION_11
java.targetCompatibility = JavaVersion.VERSION_11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ object ClientRequestCatalog {

const val CUSTOM_OBJECT = "/crm/v3/objects/{customObjectEntity}"
const val CUSTOM_OBJECT_DETAIL = "/crm/v3/objects/{customObjectEntity}/{customObjectId}"

const val CONTACTS = "/crm/v3/objects/contacts"
const val CONTACTS_DETAIL = "/crm/v3/objects/contacts/{contactId}"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.boomevents.hubspot.domain.contact

import org.boomevents.hubspot.domain.DataEntity
import java.math.BigInteger
import java.time.LocalDateTime

class Contact(
override val id: BigInteger,
override val properties: Map<String, Any>,
override val createdAt: LocalDateTime,
override val updatedAt: LocalDateTime,
override val archived: Boolean
) : DataEntity
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.boomevents.hubspot.domain.contact

import org.boomevents.hubspot.domain.contact.exception.ContactNotFoundException
import org.boomevents.hubspot.Client
import org.boomevents.hubspot.ClientRequestCatalog
import org.boomevents.hubspot.model.http.RequestMethod
import org.boomevents.hubspot.model.http.Requester
import org.boomevents.hubspot.model.http.exceptions.HttpRequestException
import org.boomevents.hubspot.model.mapper.Mapper
import java.math.BigInteger
import java.util.Collections.emptyMap

class ContactClient(private val hubSpotClient: Client) {

fun <P> createContact(request: ContactRequest<P>): Contact {
val response = Requester.requestJson(
hubSpotClient,
RequestMethod.POST,
ClientRequestCatalog.V3.CONTACTS,
emptyMap(),
request
)

if (response.isSuccess) {
return Mapper.mapToObject(response.body)
} else {
throw RuntimeException(response.statusText)
}
}

@Throws(
ContactNotFoundException::class,
HttpRequestException::class
)
fun findContact(contactId: BigInteger): Contact {
val requestUrl = ClientRequestCatalog.V3.CONTACTS_DETAIL.replace(
"{contactId}", contactId.toString()
)

val response = Requester.requestJson(hubSpotClient, RequestMethod.GET, requestUrl)

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

fun <P> changeContact(contactId: BigInteger, request: ContactRequest<P>): Contact {
val requestUrl = ClientRequestCatalog.V3.CONTACTS_DETAIL.replace(
"{contactId}", contactId.toString()
)

val response = Requester.requestJson(hubSpotClient, RequestMethod.PATCH, requestUrl, emptyMap(), request)

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

@Throws(
HttpRequestException::class
)
fun removeContact(contactId: BigInteger) {
val requestUrl = ClientRequestCatalog.V3.CONTACTS_DETAIL.replace(
"{contactId}", contactId.toString()
)

// Unknown contact 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)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.boomevents.hubspot.domain.contact

class ContactRequest<P>(
val properties: P
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.boomevents.hubspot.domain.contact.exception

import org.boomevents.hubspot.exceptions.HubSpotException

abstract class ContactException(override val message: String) : HubSpotException(message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.boomevents.hubspot.domain.contact.exception

import java.math.BigInteger

class ContactNotFoundException(
contactId: BigInteger,
override val message: String = "Contact '$contactId' was not found."
) : ContactException(message)

0 comments on commit 31f2307

Please sign in to comment.