Skip to content

Commit abde2b3

Browse files
authored
Merge pull request #49 from BidnessForB/implement-save-to-postman
Implement save to postman
2 parents dd30840 + 47c5f60 commit abde2b3

File tree

3 files changed

+173
-2
lines changed

3 files changed

+173
-2
lines changed

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

Lines changed: 85 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,88 @@ 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 upsertToPostman(PostmanID workspaceID) throws IOException, InterruptedException, CollectionNotFoundException, InvalidCollectionActionException {
711+
String colData = this.toJson();
712+
colData = colData.substring(colData.indexOf("\"item\":"));
713+
String apiURL = "https://api.getpostman.com/collections";
714+
715+
var client = HttpClient.newHttpClient();
716+
String colHeaderJSON = "{\"collection\": { \"info\": {\"name\": \"" + this.getName() + "\", \"schema\": \"https://schema.getpostman.com/json/collection/v2.1.0/collection.json\"},";
717+
718+
String bodyJSON = colHeaderJSON + colData + "}";
719+
720+
String apiToken = System.getenv("POSTMAN_API_KEY");
721+
if(apiToken == null) {
722+
throw new IllegalArgumentException("No Postman API Key configured");
723+
}
724+
725+
HttpRequest request = null;
726+
if(this.getPostmanID() == null) {
727+
//In this case we are creating
728+
if(workspaceID != null && workspaceID.getID().length() > 0)
729+
apiURL = apiURL + "?workspace=" + workspaceID.getID();
730+
731+
request = HttpRequest.newBuilder(
732+
URI.create(apiURL))
733+
.header("accept", "application/json")
734+
.header("x-api-key",apiToken)
735+
736+
.POST(HttpRequest.BodyPublishers.ofString(bodyJSON))
737+
.build();
738+
}
739+
else if (this.getPostmanID() != null) {
740+
apiURL = apiURL + "/" + this.getPostmanID();
741+
request = HttpRequest.newBuilder(
742+
URI.create(apiURL))
743+
.header("accept", "application/json")
744+
.header("x-api-key",apiToken)
745+
746+
.PUT(HttpRequest.BodyPublishers.ofString(bodyJSON))
747+
.build();
748+
}
749+
750+
var response = client.send(request, BodyHandlers.ofString());
751+
752+
753+
if(response.statusCode() == 200) {
754+
GsonBuilder gsonBuilder = new GsonBuilder();
755+
Gson customGson = gsonBuilder.create();
756+
757+
758+
// use the client to send the request
759+
760+
Type hashType = new TypeToken<HashMap<String,HashMap<String, String>>>() {}.getType();
761+
HashMap<String, HashMap<String, String>> respJSON = customGson.fromJson(response.body(), hashType);
762+
this.setPostmanID(respJSON.get("collection").get("id").toString());
763+
764+
}
765+
else if(response.statusCode() == 404) {
766+
throw new CollectionNotFoundException("Collection not found or invalid endopint");
767+
}
768+
else
769+
{
770+
throw new InvalidCollectionActionException("An error occurred retrieving the collection" + (response.body() == null ? "[no response info]" : response.body()));
771+
}
772+
773+
return new PostmanID(this.getPostmanID());
774+
775+
776+
777+
778+
}
779+
780+
697781

698782

699783
/**

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-169dff8a-c684-4ccc-b8df-7ad436efda57",
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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.ArrayList;
1616
import java.util.Arrays;
1717
import java.io.File;
18+
19+
import org.apache.commons.collections4.functors.FalsePredicate;
1820
import org.junit.Test;
1921

2022
import com.networknt.schema.ValidationMessage;
@@ -25,6 +27,7 @@
2527
import java.nio.file.DirectoryStream;
2628
import java.nio.file.Path;
2729
import java.nio.file.Paths;
30+
import java.security.Timestamp;
2831
import java.nio.file.Files;
2932
import java.io.IOException;
3033
import java.net.MalformedURLException;
@@ -1519,6 +1522,90 @@ public void testParentChain() {
15191522

15201523

15211524

1525+
}
1526+
1527+
@Test
1528+
public void testWriteToPostman() {
1529+
Collection pmcTest = Collection.pmcFactory();
1530+
Collection pmcTest2 = null;
1531+
BodyElement body = null;
1532+
RequestBody req = null;
1533+
Response resp = null;
1534+
String colName = new java.sql.Timestamp(System.currentTimeMillis()).toString();
1535+
//Test creating a new collection
1536+
try {
1537+
1538+
body = new BodyElement(enumRequestBodyMode.URLENCODED);
1539+
body.setFormdata("x-field-1", "value 1", "This is value 1");
1540+
body.setFormdata("x-field-2", "value 2", "This is value 2");
1541+
req = new RequestBody(enumHTTPRequestMethod.POST, "https://postman-echo.com/post");
1542+
req.setBody(body);
1543+
resp = new Response("NORMAL Urlencoded", req, "OK", 200, "this is the expected response body");
1544+
pmcTest.addRequest(req, "URLEncoded body", resp);
1545+
pmcTest.setName("UPSERT test " + colName);
1546+
}
1547+
catch(Exception eee) {
1548+
eee.printStackTrace();
1549+
assertTrue("Exception creating test collection: " + eee.getMessage(), false);
1550+
}
1551+
1552+
try {
1553+
assertNull("No postmanID before upsert: ", pmcTest.getPostmanID());
1554+
pmcTest.upsertToPostman(null);
1555+
assertNotNull("Postman ID successfully assigned: " , pmcTest.getPostmanID());
1556+
}
1557+
catch(Exception e) {
1558+
e.printStackTrace();
1559+
assertTrue("Exception upserting new collection to Postman: " + e.getMessage(), false);
1560+
}
1561+
1562+
//Upsert a collection to a workspace
1563+
try {
1564+
1565+
body = new BodyElement(enumRequestBodyMode.URLENCODED);
1566+
body.setFormdata("x-field-1", "value 1", "This is value 1");
1567+
body.setFormdata("x-field-2", "value 2", "This is value 2");
1568+
req = new RequestBody(enumHTTPRequestMethod.POST, "https://postman-echo.com/post");
1569+
req.setBody(body);
1570+
resp = new Response("NORMAL Urlencoded", req, "OK", 200, "this is the expected response body");
1571+
pmcTest.addRequest(req, "URLEncoded body", resp);
1572+
pmcTest.setName("UPSERT test to Workspace" + colName);
1573+
pmcTest.upsertToPostman(new PostmanID("23889826-169dff8a-c684-4ccc-b8df-7ad436efda57"));
1574+
}
1575+
catch(Exception eee) {
1576+
eee.printStackTrace();
1577+
assertTrue("Exception creating test collection: " + eee.getMessage(), false);
1578+
}
1579+
1580+
//Fetch a known collection from Postman, change the name, then Upsert it
1581+
try {
1582+
1583+
pmcTest = Collection.pmcFactory(new PostmanID("23889826-169dff8a-c684-4ccc-b8df-7ad436efda57"));
1584+
String origName = pmcTest.getName();
1585+
pmcTest.setName(origName + "- UPSERTED");
1586+
pmcTest.upsertToPostman(null);
1587+
pmcTest = Collection.pmcFactory(new PostmanID("23889826-169dff8a-c684-4ccc-b8df-7ad436efda57"));
1588+
assertTrue(pmcTest.getName().equals(origName + "- UPSERTED"));
1589+
}
1590+
catch(Exception ee) {
1591+
ee.printStackTrace();
1592+
assertTrue("Exception writing known collection: " + ee.getMessage(), false);
1593+
}
1594+
1595+
1596+
try {
1597+
//read in a collection with an ID
1598+
pmcTest = Collection.pmcFactory(new File(filePath + resourcePath + "/auth.postman_collection.json"));
1599+
pmcTest.setName("Auth renamed again " + new java.sql.Timestamp(System.currentTimeMillis()));
1600+
pmcTest.upsertToPostman(null);
1601+
}
1602+
catch(Exception e)
1603+
{
1604+
assertTrue("IOException: " + e.getMessage(), false);
1605+
}
1606+
1607+
1608+
15221609
}
15231610

15241611
}

0 commit comments

Comments
 (0)