Skip to content

Commit

Permalink
Light implementation of deals (#6)
Browse files Browse the repository at this point in the history
* Upgrade Gradle from 7.2 to 7.6

* Light implementation of deals API

* Changed package version to 1.3.0
  • Loading branch information
Jiří Svěcený authored Apr 14, 2023
1 parent 5220786 commit fca34d1
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ General implementation of [HubSpot](https://developers.hubspot.com/docs/api/crm/

🎈 Currently in progress, send issues or pull requests 🙌🏼

🚀 Install from offical Maven repository with `org.boomevents:hubspot-sdk:$VERSION`<br>
🚀 Install from official Maven repository with `org.boomevents:hubspot-sdk:$VERSION`<br>

## Supported features
| Feature | List | Read | Create | Change | Delete |
|------------------|--------|--------|----------|----------|----------|
| Company ||||||
| Custom objects ||||||
| Deal ||||||
| Contact ||||||
| Deal ||||||

## 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.2.2"
version = "1.3.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 @@ -6,6 +6,9 @@ object ClientRequestCatalog {
const val COMPANIES = "/crm/v3/objects/companies"
const val COMPANIES_DETAIL = "/crm/v3/objects/companies/{companyId}"

const val DEALS = "/crm/v3/objects/deals"
const val DEALS_DETAIL = "/crm/v3/objects/deals/{dealId}"

const val CUSTOM_OBJECT = "/crm/v3/objects/{customObjectEntity}"
const val CUSTOM_OBJECT_DETAIL = "/crm/v3/objects/{customObjectEntity}/{customObjectId}"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.boomevents.hubspot.domain.deals

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

class Deal(
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,81 @@
package org.boomevents.hubspot.domain.deals

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.deals.exceptions.DealsNotFoundException
import org.boomevents.hubspot.model.http.exceptions.HttpRequestException
import org.boomevents.hubspot.model.mapper.Mapper
import java.math.BigInteger

class DealClient(private val hubSpotClient: Client) {

fun <P> createDeal(request: DealRequest<P>): Deal {
val response = Requester.requestJson(hubSpotClient, RequestMethod.POST, ClientRequestCatalog.V3.DEALS, emptyMap(), request)

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


@Throws(
DealsNotFoundException::class,
HttpRequestException::class
)
fun findDeal(dealId: BigInteger): Deal {
val requestUrl = ClientRequestCatalog.V3.DEALS_DETAIL.replace(
"{dealId}", dealId.toString()
)

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

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


fun <P> changeDeal(dealId: BigInteger, request: DealRequest<P>): Deal {
val requestUrl = ClientRequestCatalog.V3.DEALS_DETAIL.replace(
"{dealId}", dealId.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 DealsNotFoundException(dealId)
else -> throw HttpRequestException(response.status, response.statusText)
}
}
}

@Throws(
HttpRequestException::class
)
fun removeDeal(dealId: BigInteger) {
val requestUrl = ClientRequestCatalog.V3.DEALS_DETAIL.replace(
"{dealId}", dealId.toString()
)

// Unknown deal 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.deals

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

import org.boomevents.hubspot.exceptions.HubSpotException

abstract class DealException(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.deals.exceptions

import java.math.BigInteger

class DealsNotFoundException(
dealId: BigInteger,
override val message: String = "Deals '$dealId' was not found."
) : DealException(message)

0 comments on commit fca34d1

Please sign in to comment.