Skip to content

Commit a8ac4c7

Browse files
committed
Save to Postman workspace
1 parent 5305099 commit a8ac4c7

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

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

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.util.HashMap;
2727
import java.net.http.HttpClient;
2828
import java.net.http.HttpRequest;
29+
import java.net.http.HttpResponse;
30+
2931

3032
import java.net.http.HttpResponse.BodyHandlers;
3133

@@ -530,7 +532,7 @@ public static Collection pmcFactory(URL collectionURL) throws IOException, Inter
530532
if(apiToken == null) {
531533
throw new IllegalArgumentException("No Postman API Key configured");
532534
}
533-
535+
534536
// create a request
535537
var request = HttpRequest.newBuilder(
536538
URI.create(collectionURL.toString()))
@@ -694,6 +696,65 @@ public void writeToFile(File outputFile) throws IOException {
694696
throw(e);
695697
}
696698
}
699+
public void updateInPostman(PostmanID workspaceID) {
700+
701+
}
702+
/**
703+
*
704+
* Write this collections generated JSON to a file at the specified path. Note that the order of elements in the resulting file is not guaranteed and may not match
705+
* a corresponding Postman generated file. However, this does not affect the validity or functionality of the generated JSON.
706+
*
707+
* @param outputFile The file into which to write the JSON
708+
* @throws IOException If there is an error attempting to create or write to the specified path
709+
*/
710+
public PostmanID writeToPostman(PostmanID workspaceID) throws IOException, InterruptedException {
711+
String colData = this.toJson();
712+
colData = colData.substring(colData.indexOf("\"item\":"));
713+
String apiURL = "https://api.getpostman.com/collections/?workspace=" + workspaceID;
714+
var client = HttpClient.newHttpClient();
715+
String colHeaderJSON = "{\"collection\": { \"info\": {\"name\": \"" + this.getName() + "\", \"schema\": \"https://schema.getpostman.com/json/collection/v2.1.0/collection.json\"},";
716+
717+
String bodyJSON = colHeaderJSON + colData + "}";
718+
719+
String apiToken = System.getenv("POSTMAN_API_KEY");
720+
if(apiToken == null) {
721+
throw new IllegalArgumentException("No Postman API Key configured");
722+
}
723+
724+
// create a request
725+
var request = HttpRequest.newBuilder(
726+
URI.create(apiURL))
727+
.header("accept", "application/json")
728+
.header("x-api-key",apiToken)
729+
730+
.POST(HttpRequest.BodyPublishers.ofString(bodyJSON))
731+
.build();
732+
733+
GsonBuilder gsonBuilder = new GsonBuilder();
734+
Gson customGson = gsonBuilder.create();
735+
736+
737+
// use the client to send the request
738+
var response = client.send(request, BodyHandlers.ofString());
739+
Type hashType = new TypeToken<HashMap<String,HashMap<String, String>>>() {}.getType();
740+
HashMap<String, HashMap> respJSON = customGson.fromJson(response.body(), hashType);
741+
/*
742+
if(response.statusCode() == 404) {
743+
throw new CollectionNotFoundException("Collection not found or invalid endopint");
744+
}
745+
else if(response.statusCode() != 200)
746+
{
747+
throw new InvalidCollectionActionException("An error occurred retrieving the collection" + (response.body() == null ? "[no response info]" : response.body()));
748+
}
749+
*/
750+
751+
this.setPostmanID(respJSON.get("collection").get("id").toString());
752+
753+
return new PostmanID(this.getPostmanID());
754+
755+
}
756+
757+
697758

698759

699760
/**

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,29 @@ public void testParentChain() {
15191519

15201520

15211521

1522+
}
1523+
1524+
@Test
1525+
public void testWriteToPostman() {
1526+
Collection pmcTest = null;
1527+
1528+
try {
1529+
pmcTest = Collection.pmcFactory(new File(filePath + resourcePath + "/auth.postman_collection.json"));
1530+
}
1531+
catch(IOException e)
1532+
{
1533+
assertTrue("IOException: " + e.getMessage(), false);
1534+
}
1535+
1536+
try {
1537+
pmcTest.writeToPostman(new PostmanID("d827e22e-3694-408d-b18b-d3eea108c001"));
1538+
}
1539+
catch(Exception e)
1540+
{
1541+
assertTrue("Exception: " + e.getMessage(), false);
1542+
}
1543+
1544+
15221545
}
15231546

15241547
}

0 commit comments

Comments
 (0)