diff --git a/ballerina/Ballerina.toml b/ballerina/Ballerina.toml index ff2f1dd..5457cbc 100644 --- a/ballerina/Ballerina.toml +++ b/ballerina/Ballerina.toml @@ -6,7 +6,7 @@ version = "1.0.0" license = ["Apache-2.0"] authors = ["Ballerina"] keywords = ["hubspot","crm", "commerce", "orders"] -icon = "icon.png" +icon = "icon.png" repository = "https://github.com/ballerina-platform/module-ballerinax-hubspot.crm.commerce.orders" [build-options] diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index c0fdbd6..eb61e8f 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.3" +distribution-version = "2201.10.0" [[package]] org = "ballerina" @@ -256,6 +256,9 @@ dependencies = [ {org = "ballerina", name = "io"}, {org = "ballerina", name = "jballerina.java"} ] +modules = [ + {org = "ballerina", packageName = "os", moduleName = "os"} +] [[package]] org = "ballerina" @@ -318,6 +321,7 @@ version = "1.0.0" dependencies = [ {org = "ballerina", name = "http"}, {org = "ballerina", name = "oauth2"}, + {org = "ballerina", name = "os"}, {org = "ballerina", name = "test"}, {org = "ballerina", name = "url"}, {org = "ballerinai", name = "observe"} @@ -325,3 +329,4 @@ dependencies = [ modules = [ {org = "ballerinax", packageName = "hubspot.crm.commerce.orders", moduleName = "hubspot.crm.commerce.orders"} ] + diff --git a/ballerina/tests/mock_test.bal b/ballerina/tests/mock_test.bal index ddb4a21..e680760 100644 --- a/ballerina/tests/mock_test.bal +++ b/ballerina/tests/mock_test.bal @@ -16,11 +16,14 @@ import ballerina/test; -final Client orderClient = check new Client(config, serviceUrl = "http://localhost:9090/crm/v3/objects/orders"); +final Client ordersClient = check new(config = {auth:{ + token: "test-token" +}}, serviceUrl="http://localhost:9090/crm/v3/objects/orders" +); @test:Config {} -isolated function mockTestForCreatingABatchOfOrders() returns error? { - BatchResponseSimplePublicObject response = check orderClient->/batch/create.post( +function mockTestForCreatingABatchOfOrders() returns error? { + BatchResponseSimplePublicObject response = check ordersClient->/batch/create.post( { inputs: [ { @@ -50,7 +53,7 @@ isolated function mockTestForCreatingABatchOfOrders() returns error? { @test:Config {} isolated function mockTestForCreatingBatchOfOrdersByUniqueProperty() returns error? { BatchResponseSimplePublicUpsertObject|BatchResponseSimplePublicUpsertObjectWithErrors response = - check orderClient->/batch/upsert.post( + check ordersClient->/batch/upsert.post( payload = { inputs: [ { diff --git a/ballerina/tests/test.bal b/ballerina/tests/test.bal index 2ce82b5..7df0dd9 100644 --- a/ballerina/tests/test.bal +++ b/ballerina/tests/test.bal @@ -16,11 +16,15 @@ import ballerina/http; import ballerina/oauth2; +import ballerina/os; import ballerina/test; configurable string clientId = ?; configurable string clientSecret = ?; configurable string refreshToken = ?; +configurable boolean enableLiveServerTest = false; + +final boolean isLiveServer = os:getEnv("IS_LIVE_SERVER") == "true"; OAuth2RefreshTokenGrantConfig auth = { clientId, @@ -29,10 +33,15 @@ OAuth2RefreshTokenGrantConfig auth = { credentialBearer: oauth2:POST_BODY_BEARER }; -ConnectionConfig config = {auth: auth}; -final Client baseClient = check new Client(config); +final Client orderClient = check new ({ + auth: enableLiveServerTest ? auth + : {token: "test-token"} +}); -@test:Config {} +@test:Config { + groups: ["live_tests"], + enable: enableLiveServerTest +} function testPostOrdersSearch() returns error? { PublicObjectSearchRequest payload = { query: "1", @@ -55,11 +64,14 @@ function testPostOrdersSearch() returns error? { ] }; CollectionResponseWithTotalSimplePublicObjectForwardPaging response = - check baseClient->/search.post(payload = payload); + check orderClient->/search.post(payload = payload); test:assertTrue(response.total >= 0); } -@test:Config {} +@test:Config { + groups: ["live_tests"], + enable: enableLiveServerTest +} function testPostOrdersBatchRead() returns error? { BatchReadInputSimplePublicObjectId payload = { @@ -74,7 +86,7 @@ function testPostOrdersBatchRead() returns error? { properties: ["hs_lastmodifieddate", "hs_createdate", "hs_object_id", "updatedAt"] }; BatchResponseSimplePublicObject|BatchResponseSimplePublicObjectWithErrors response = - check baseClient->/batch/read.post(payload = payload); + check orderClient->/batch/read.post(payload = payload); if response.status != "PENDING" && response.status != "PROCESSING" && response.status != "CANCELED" && response.status != "COMPLETE" { test:assertFail("invalid status type"); @@ -83,15 +95,21 @@ function testPostOrdersBatchRead() returns error? { test:assertFalse(response.startedAt is "", "startedAt should not be empty"); } -@test:Config {} +@test:Config { + groups: ["live_tests"], + enable: enableLiveServerTest +} function testDeleteObjectsOrdersByOrderId() returns error? { string orderId = "10"; - http:Response response = check baseClient->/[orderId].delete(); + http:Response response = check orderClient->/[orderId].delete(); test:assertTrue(response.statusCode == 204); } -@test:Config {} +@test:Config { + groups: ["live_tests"], + enable: enableLiveServerTest +} function testPatchObjectsOrdersByOrderId() returns error? { string orderId = "395972319872"; SimplePublicObjectInput payload = @@ -102,23 +120,29 @@ function testPatchObjectsOrdersByOrderId() returns error? { "hs_shipping_tracking_number": "123098521091" } }; - SimplePublicObject response = check baseClient->/[orderId].patch(payload = payload); + SimplePublicObject response = check orderClient->/[orderId].patch(payload = payload); test:assertFalse(response?.id is "", "id should not be empty"); test:assertFalse(response?.createdAt is "", "creation time should not be empty"); test:assertFalse(response?.updatedAt is "", "updated time should not be empty"); } -@test:Config {} +@test:Config { + groups: ["live_tests"], + enable: enableLiveServerTest +} function testGetObjectsOrdersByOrderId() returns error? { string orderId = "395972319872"; - SimplePublicObjectWithAssociations response = check baseClient->/[orderId]; + SimplePublicObjectWithAssociations response = check orderClient->/[orderId]; test:assertFalse(response?.createdAt is "", "creation time should not be empty"); test:assertFalse(response?.updatedAt is "", "updated time should not be empty"); } -@test:Config {} +@test:Config { + groups: ["live_tests"], + enable: enableLiveServerTest +} function testPostordersBatchUpsert() returns error? { BatchInputSimplePublicObjectBatchInputUpsert payload = { inputs: [ @@ -135,13 +159,16 @@ function testPostordersBatchUpsert() returns error? { ] }; BatchResponseSimplePublicUpsertObject|BatchResponseSimplePublicUpsertObjectWithErrors response = - check baseClient->/batch/upsert.post(payload = payload); + check orderClient->/batch/upsert.post(payload = payload); test:assertTrue(response.status == "COMPLETE"); test:assertFalse(response?.completedAt is "", "creation time should not be empty"); test:assertFalse(response?.startedAt is "", "start time should not be empty"); } -@test:Config {} +@test:Config { + groups: ["live_tests"], + enable: enableLiveServerTest +} function testPostOrdersBatchCreate() returns error? { BatchInputSimplePublicObjectInputForCreate payload = { @@ -166,12 +193,15 @@ function testPostOrdersBatchCreate() returns error? { } ] }; - BatchResponseSimplePublicObject response = check baseClient->/batch/create.post(payload = payload); + BatchResponseSimplePublicObject response = check orderClient->/batch/create.post(payload = payload); test:assertFalse(response.completedAt is "", "completedAt should not be empty"); test:assertFalse(response.startedAt is "", "startedAt should not be empty"); } -@test:Config {} +@test:Config { + groups: ["live_tests"], + enable: enableLiveServerTest +} function testPostObjectsOrdersBatchUpdate() returns error? { BatchInputSimplePublicObjectBatchInput payload = { @@ -185,12 +215,15 @@ function testPostObjectsOrdersBatchUpdate() returns error? { ] }; BatchResponseSimplePublicObject|BatchResponseSimplePublicObjectWithErrors response = - check baseClient->/batch/update.post(payload = payload); + check orderClient->/batch/update.post(payload = payload); test:assertFalse(response.completedAt is "", "completedAt should not be empty"); test:assertFalse(response.startedAt is "", "startedAt should not be empty"); } -@test:Config {} +@test:Config { + groups: ["live_tests"], + enable: enableLiveServerTest +} function testPostObjectsOrders() returns error? { SimplePublicObjectInputForCreate payload = @@ -219,16 +252,19 @@ function testPostObjectsOrders() returns error? { "hs_shipping_address_street": "123 Fake Street" } }; - SimplePublicObject response = check baseClient->/.post(payload = payload); + SimplePublicObject response = check orderClient->/.post(payload = payload); test:assertFalse(response.createdAt is "", "createdAt should not be empty"); test:assertFalse(response.updatedAt is "", "updateAt should not be empty"); } -@test:Config {} +@test:Config { + groups: ["live_tests"], + enable: enableLiveServerTest +} function testGetObjectsOrders() returns error? { - CollectionResponseSimplePublicObjectWithAssociationsForwardPaging response = check baseClient->/; + CollectionResponseSimplePublicObjectWithAssociationsForwardPaging response = check orderClient->/; foreach SimplePublicObjectWithAssociations result in response.results { test:assertFalse(result.createdAt is "", "createdAt should not be empty"); @@ -236,7 +272,10 @@ function testGetObjectsOrders() returns error? { } } -@test:Config {} +@test:Config { + groups: ["live_tests"], + enable: enableLiveServerTest +} function testPostOrdersBatchArchive() returns error? { BatchInputSimplePublicObjectId payload = { inputs: [ @@ -245,6 +284,6 @@ function testPostOrdersBatchArchive() returns error? { } ] }; - http:Response response = check baseClient->/batch/archive.post(payload = payload); + http:Response response = check orderClient->/batch/archive.post(payload = payload); test:assertTrue(response.statusCode == 204); } diff --git a/examples/batch-operations/main.bal b/examples/batch-operations/main.bal index 1f25719..64f3184 100644 --- a/examples/batch-operations/main.bal +++ b/examples/batch-operations/main.bal @@ -16,7 +16,7 @@ import ballerina/io; import ballerina/oauth2; -import ballerinax/hubspot.crm.commerce.orders as orders; +import ballerinax/hubspot.crm.commerce.orders as hsorders; // Configuration for HubSpot API client credentials configurable string clientId = ?; @@ -25,7 +25,7 @@ configurable string refreshToken = ?; public function main() returns error? { // Initialize the HubSpot client with the given configuration - orders:ConnectionConfig config = { + hsorders:ConnectionConfig config = { auth: { clientId, clientSecret, @@ -33,30 +33,28 @@ public function main() returns error? { credentialBearer: oauth2:POST_BODY_BEARER } }; - final orders:Client hubspotClient = check new orders:Client( - config, serviceUrl = "https://api.hubapi.com/crm/v3/objects"); + final hsorders:Client hubspotClient = check new hsorders:Client(config); io:println("HubSpot Client initialized successfully."); // Handle batch operations - check handleBatchOperations(hubspotClient); + handleBatchOperations(hubspotClient); } // Function to handle all batch operations -function handleBatchOperations(orders:Client hubspotClient) returns error? { +function handleBatchOperations(hsorders:Client hubspotClient) { io:println("Starting Batch Operations..."); // Perform batch create orders - check batchCreateOrders(hubspotClient); + batchCreateOrders(hubspotClient); // Perform batch update orders - check batchUpdateOrders(hubspotClient); - + batchUpdateOrders(hubspotClient); io:println("Batch Operations Completed."); } // Function to create a batch of orders -function batchCreateOrders(orders:Client hubspotClient) returns error? { - orders:BatchInputSimplePublicObjectInputForCreate batchCreateRequest = { +function batchCreateOrders(hsorders:Client hubspotClient) { + hsorders:BatchInputSimplePublicObjectInputForCreate batchCreateRequest = { inputs: [ { associations: [ @@ -79,19 +77,18 @@ function batchCreateOrders(orders:Client hubspotClient) returns error? { ] }; - orders:BatchResponseSimplePublicObject|error response = + hsorders:BatchResponseSimplePublicObject|error response = hubspotClient->/orders/batch/create.post(batchCreateRequest); - if response is orders:BatchResponseSimplePublicObject { + if response is hsorders:BatchResponseSimplePublicObject { io:println("Batch of orders created successfully."); } else { io:println("Failed to create batch of orders."); - return error("Batch creation failed."); } } // Function to update a batch of orders -function batchUpdateOrders(orders:Client hubspotClient) returns error? { - orders:BatchInputSimplePublicObjectBatchInput batchUpdateRequest = { +function batchUpdateOrders(hsorders:Client hubspotClient) { + hsorders:BatchInputSimplePublicObjectBatchInput batchUpdateRequest = { inputs: [ { id: "394961395351", @@ -102,12 +99,11 @@ function batchUpdateOrders(orders:Client hubspotClient) returns error? { ] }; - orders:BatchResponseSimplePublicObject|error response = + hsorders:BatchResponseSimplePublicObject|error response = hubspotClient->/orders/batch/update.post(batchUpdateRequest); - if response is orders:BatchResponseSimplePublicObject { + if response is hsorders:BatchResponseSimplePublicObject { io:println("Batch of orders updated successfully."); } else { io:println("Failed to update batch of orders."); - return error("Batch update failed."); } } diff --git a/examples/order-management/main.bal b/examples/order-management/main.bal index cd12984..1043a56 100644 --- a/examples/order-management/main.bal +++ b/examples/order-management/main.bal @@ -16,9 +16,8 @@ import ballerina/http; import ballerina/io; -import ballerina/log; import ballerina/oauth2; -import ballerinax/hubspot.crm.commerce.orders as hsOrders; +import ballerinax/hubspot.crm.commerce.orders as hsorders; // Configuration for HubSpot API client credentials configurable string clientId = ?; @@ -27,7 +26,7 @@ configurable string refreshToken = ?; public function main() returns error? { // Initialize the HubSpot client with the given configuration - hsOrders:ConnectionConfig config = { + hsorders:ConnectionConfig config = { auth: { clientId, clientSecret, @@ -35,12 +34,12 @@ public function main() returns error? { credentialBearer: oauth2:POST_BODY_BEARER } }; - final hsOrders:Client hubspotClient = check new hsOrders:Client(config); - log:printInfo("HubSpot Client initialized successfully."); + final hsorders:Client hubspotClient = check new hsorders:Client(config); + io:println("HubSpot Client initialized successfully."); handleOrderManagement(hubspotClient); } -function handleOrderManagement(hsOrders:Client hubspotClient) { +function handleOrderManagement(hsorders:Client hubspotClient) { io:println("Starting Order Management..."); string|error newOrderId = ""; newOrderId = createOrder(hubspotClient); @@ -56,8 +55,8 @@ function handleOrderManagement(hsOrders:Client hubspotClient) { } // Create a new order -function createOrder(hsOrders:Client hubspotClient) returns string|error { - hsOrders:SimplePublicObjectInputForCreate newOrder = +function createOrder(hsorders:Client hubspotClient) returns string|error { + hsorders:SimplePublicObjectInputForCreate newOrder = { associations: [ { @@ -82,8 +81,8 @@ function createOrder(hsOrders:Client hubspotClient) returns string|error { "hs_shipping_address_street": "123 Fake Street" } }; - hsOrders:SimplePublicObject|error response = hubspotClient->/orders.post(newOrder); - if response is hsOrders:SimplePublicObject { + hsorders:SimplePublicObject|error response = hubspotClient->/orders.post(newOrder); + if response is hsorders:SimplePublicObject { io:println("Order created successfully with ID: ", response.id); return response.id; } else { @@ -92,35 +91,35 @@ function createOrder(hsOrders:Client hubspotClient) returns string|error { } } -function readOrder(hsOrders:Client hubspotClient, string orderId) { +function readOrder(hsorders:Client hubspotClient, string orderId) { var response = hubspotClient->/orders/[orderId]; - if response is hsOrders:SimplePublicObjectWithAssociations { + if response is hsorders:SimplePublicObjectWithAssociations { io:println("Order details retrieved: ", response); } else { io:println("Failed to retrieve order with ID: ", orderId); } } -function updateOrder(hsOrders:Client hubspotClient, string orderId) { - hsOrders:SimplePublicObjectInput updateDetails = { +function updateOrder(hsorders:Client hubspotClient, string orderId) { + hsorders:SimplePublicObjectInput updateDetails = { properties: { "hs_fulfillment_status": "Shipped" } }; - var response = hubspotClient->/orders/[orderId].patch(updateDetails); - if response is hsOrders:SimplePublicObject { - log:printInfo("Order updated successfully with ID: "+ response.id); + hsorders:SimplePublicObject|error response = hubspotClient->/orders/[orderId].patch(updateDetails); + if response is hsorders:SimplePublicObject { + io:println("Order updated successfully with ID: "+ response.id); } else { - log:printError("Failed to update order with ID: "+ orderId); + io:println("Failed to update order with ID: "+ orderId); } } -function deleteOrder(hsOrders:Client hubspotClient, string orderId){ +function deleteOrder(hsorders:Client hubspotClient, string orderId){ http:Response|error response = hubspotClient->/orders/[orderId].delete(); if response is http:Response { io:println("Order deleted successfully with status: ", response.statusCode); } else { - log:printError("Failed to delete order with ID: " + orderId); + io:println("Failed to delete order with ID: " + orderId); } } diff --git a/examples/search-operation/main.bal b/examples/search-operation/main.bal index 7c45a20..419b977 100644 --- a/examples/search-operation/main.bal +++ b/examples/search-operation/main.bal @@ -16,7 +16,7 @@ import ballerina/io; import ballerina/oauth2; -import ballerinax/hubspot.crm.commerce.orders as orders; +import ballerinax/hubspot.crm.commerce.orders as hsorders; // Configuration for HubSpot API client credentials configurable string clientId = ?; @@ -25,7 +25,7 @@ configurable string refreshToken = ?; public function main() returns error? { // Initialize the HubSpot client with the given configuration - orders:ConnectionConfig config = { + hsorders:ConnectionConfig config = { auth: { clientId, clientSecret, @@ -33,27 +33,26 @@ public function main() returns error? { credentialBearer: oauth2:POST_BODY_BEARER } }; - final orders:Client hubspotClient = check new orders:Client( - config, serviceUrl = "https://api.hubapi.com/crm/v3/objects"); + final hsorders:Client hubspotClient = check new hsorders:Client(config); io:println("HubSpot Client initialized successfully."); // Handle search operations - check handleSearchOperations(hubspotClient); + handleSearchOperations(hubspotClient); } // Function to handle all search operations -function handleSearchOperations(orders:Client hubspotClient) returns error? { +function handleSearchOperations(hsorders:Client hubspotClient) { io:println("Starting Search Operations..."); // Perform order search - check searchOrders(hubspotClient); + searchOrders(hubspotClient); io:println("Search Operations Completed."); } // Function to search for orders based on specific criteria -function searchOrders(orders:Client hubspotClient) returns error? { - orders:PublicObjectSearchRequest searchRequest = { +function searchOrders(hsorders:Client hubspotClient) { + hsorders:PublicObjectSearchRequest searchRequest = { query: "apple", properties: ["hs_order_name", "hs_currency_code"], filterGroups: [ @@ -69,12 +68,11 @@ function searchOrders(orders:Client hubspotClient) returns error? { ] }; - orders:CollectionResponseWithTotalSimplePublicObjectForwardPaging|error response = + hsorders:CollectionResponseWithTotalSimplePublicObjectForwardPaging|error response = hubspotClient->/orders/search.post(searchRequest); - if response is orders:CollectionResponseWithTotalSimplePublicObjectForwardPaging { + if response is hsorders:CollectionResponseWithTotalSimplePublicObjectForwardPaging { io:println("Search results: ", response.results.length(), " orders found."); } else { io:println("No orders found matching search criteria."); - return error("Search operation failed."); } }