Skip to content

Commit 8ce02eb

Browse files
committed
First cut of upsert to Postman
1 parent a8ac4c7 commit 8ce02eb

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

src/main/java/com/postman/collection/Collection.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,10 +707,11 @@ public void updateInPostman(PostmanID workspaceID) {
707707
* @param outputFile The file into which to write the JSON
708708
* @throws IOException If there is an error attempting to create or write to the specified path
709709
*/
710-
public PostmanID writeToPostman(PostmanID workspaceID) throws IOException, InterruptedException {
710+
public PostmanID upsertToPostman(PostmanID workspaceID) throws IOException, InterruptedException {
711711
String colData = this.toJson();
712712
colData = colData.substring(colData.indexOf("\"item\":"));
713-
String apiURL = "https://api.getpostman.com/collections/?workspace=" + workspaceID;
713+
String apiURL = "https://api.getpostman.com/collections";
714+
714715
var client = HttpClient.newHttpClient();
715716
String colHeaderJSON = "{\"collection\": { \"info\": {\"name\": \"" + this.getName() + "\", \"schema\": \"https://schema.getpostman.com/json/collection/v2.1.0/collection.json\"},";
716717

@@ -721,14 +722,30 @@ public PostmanID writeToPostman(PostmanID workspaceID) throws IOException, Inter
721722
throw new IllegalArgumentException("No Postman API Key configured");
722723
}
723724

724-
// create a request
725-
var request = HttpRequest.newBuilder(
725+
HttpRequest request = null;
726+
if(workspaceID != null && workspaceID.getID().length() > 0) {
727+
//In this case we are creating
728+
apiURL = apiURL + "?workspace=" + workspaceID.getID();
729+
request = HttpRequest.newBuilder(
726730
URI.create(apiURL))
727731
.header("accept", "application/json")
728732
.header("x-api-key",apiToken)
729733

730734
.POST(HttpRequest.BodyPublishers.ofString(bodyJSON))
731735
.build();
736+
}
737+
else if (workspaceID == null && this.getPostmanID() != null) {
738+
apiURL = apiURL + "/" + this.getPostmanID();
739+
request = HttpRequest.newBuilder(
740+
URI.create(apiURL))
741+
.header("accept", "application/json")
742+
.header("x-api-key",apiToken)
743+
744+
.PUT(HttpRequest.BodyPublishers.ofString(bodyJSON))
745+
.build();
746+
}
747+
748+
732749

733750
GsonBuilder gsonBuilder = new GsonBuilder();
734751
Gson customGson = gsonBuilder.create();

src/main/resources/com/postman/collection/auth.postman_collection.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"info": {
3-
"_postman_id": "024e0a8d-d970-4694-aa41-e23742e5c447",
3+
"_postman_id": "23889826-fde8df9b-b240-49c4-83f5-bbe78db4ab60",
44
"name": "Auth",
55
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
66
"_exporter_id": "23889826"

src/test/java/com/postman/collection/AppTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,17 +1524,26 @@ public void testParentChain() {
15241524
@Test
15251525
public void testWriteToPostman() {
15261526
Collection pmcTest = null;
1527+
Collection pmcTest2 = null;
15271528

15281529
try {
15291530
pmcTest = Collection.pmcFactory(new File(filePath + resourcePath + "/auth.postman_collection.json"));
1531+
pmcTest2 = Collection.pmcFactory(new PostmanID("23889826-598a2a23-0a3b-454f-9309-ebd0004145d9"));
1532+
pmcTest2.setName("Auth renamed again");
15301533
}
1531-
catch(IOException e)
1534+
catch(Exception e)
15321535
{
15331536
assertTrue("IOException: " + e.getMessage(), false);
15341537
}
15351538

15361539
try {
1537-
pmcTest.writeToPostman(new PostmanID("d827e22e-3694-408d-b18b-d3eea108c001"));
1540+
//Create a new collection
1541+
pmcTest.upsertToPostman(new PostmanID("d827e22e-3694-408d-b18b-d3eea108c001"));
1542+
//update an existing collection
1543+
1544+
pmcTest2.upsertToPostman(null);
1545+
// pmcTest2.setName("Auth Renamed Again");
1546+
/// pmcTest2.updateInPostman(null);
15381547
}
15391548
catch(Exception e)
15401549
{

0 commit comments

Comments
 (0)