|
26 | 26 | import java.util.HashMap; |
27 | 27 | import java.net.http.HttpClient; |
28 | 28 | import java.net.http.HttpRequest; |
| 29 | +import java.net.http.HttpResponse; |
| 30 | + |
29 | 31 |
|
30 | 32 | import java.net.http.HttpResponse.BodyHandlers; |
31 | 33 |
|
@@ -530,7 +532,7 @@ public static Collection pmcFactory(URL collectionURL) throws IOException, Inter |
530 | 532 | if(apiToken == null) { |
531 | 533 | throw new IllegalArgumentException("No Postman API Key configured"); |
532 | 534 | } |
533 | | - |
| 535 | + |
534 | 536 | // create a request |
535 | 537 | var request = HttpRequest.newBuilder( |
536 | 538 | URI.create(collectionURL.toString())) |
@@ -694,6 +696,65 @@ public void writeToFile(File outputFile) throws IOException { |
694 | 696 | throw(e); |
695 | 697 | } |
696 | 698 | } |
| 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 | + |
697 | 758 |
|
698 | 759 |
|
699 | 760 | /** |
|
0 commit comments