-
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.
light implementation of contacts (#7)
* light implementation of contacts * fix package name & copy/paste typos * package version & docs
- Loading branch information
Showing
8 changed files
with
122 additions
and
2 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
13 changes: 13 additions & 0 deletions
13
hubspot/src/main/kotlin/org/boomevents/hubspot/domain/contact/Contact.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,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 |
86 changes: 86 additions & 0 deletions
86
hubspot/src/main/kotlin/org/boomevents/hubspot/domain/contact/ContactClient.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,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) | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
hubspot/src/main/kotlin/org/boomevents/hubspot/domain/contact/ContactRequest.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 org.boomevents.hubspot.domain.contact | ||
|
||
class ContactRequest<P>( | ||
val properties: P | ||
) |
5 changes: 5 additions & 0 deletions
5
hubspot/src/main/kotlin/org/boomevents/hubspot/domain/contact/exception/ContactException.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 org.boomevents.hubspot.domain.contact.exception | ||
|
||
import org.boomevents.hubspot.exceptions.HubSpotException | ||
|
||
abstract class ContactException(override val message: String) : HubSpotException(message) |
8 changes: 8 additions & 0 deletions
8
...c/main/kotlin/org/boomevents/hubspot/domain/contact/exception/ContactNotFoundException.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,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) |