Class Collection
Encapsulates a Postman collection
Postman SDK analog: Collection
Ingest a collection file
Collection myCollection = pmcFactory(new File("example-cat-facts-with-tests.postman_collection.json");
Ingest a collection from Postman
PostmanID myID = new PostmanID("invalid input: '<'your-collection-idinvalid input: '>'");
Collection myCollection = Collection.pmcFactory(myID);
Get a request item
Item myReq = myCollection.getItem("Get Random Fact");
Get pre-request script for the request
Event preReq = myReq.getPreRequestScript();
Get source code for pre-request script
String myCode = preReq.getSourceCode();
-
Constructor Summary
ConstructorsConstructorDescriptionCollection(String name) Construct a new, empty collection with the specified name -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCollection(Collection newColl) Add another collection and it's array ofitem,variable, andeventarrays to this collection in a new Folder.voidaddCollection(Collection newColl, boolean copyScripts, boolean copyVariables) Add another collection and it's array ofitem, optionallyvariable, and optionallyeventarrays to this collection as children of a new Folder child item of this collection.voidaddCollection(Collection newColl, ItemGroup parent) Add another collection and it's array ofitem,variable, andeventarrays to this collection as children ofparentvoidaddCollection(Collection newColl, ItemGroup parent, boolean copyScripts, boolean copyVariables) Add another collection and it's array ofitem, optionallyvariable, and optionallyeventarrays to this collection as children of the specified parent.Convenience method to add an item with no child items to this collection.Convenience method to add an item with no child items to this collection.addRequest(RequestBody reqElement, String name) Convenience method to add a newrequestitem as a top level child item of this collectionaddRequest(RequestBody reqElement, String name, int position) Add a new request to this collection at the specified position in the array ofrequestelementsaddRequest(RequestBody reqElement, String name, Response response) Create and add a newrequestas a top level child item of this collection.addRequest(RequestBody newRequest, String name, ArrayList<Response> responses) Convenience method to add a request with a pre-constructed ArrayList<Response> of response items.addRequest(String url) Create and add a newrequestas a top level child item of this collection.voidaddVariable(Property varNew) Add or replace variable to the collection of variables comprising this collectionsvariablearray property.voidaddVariables(PropertyList<Property> newVars) getAuth()Return the values in theauthobject property of this collection, or null if it has not been set.Return the value of thedescriptionpropertygetInfo()Return a Map<String> containing the key-value pairs comprising theinfoobject property of this collection.getKey()getName()Return the_postman_idproperty of theinfoobject property of this collectiongetRequest(String name) Return a request from this Collectionsitemproperty, or null if it doesn't exist.getVariable(String key) Return the Property key-value pair from this collection'svariablearray element, or null if it is not present.Get the ArrayList<Property> containing the key-value pairs comprising thevariablearray element of this collectionvoidMove an item to a different parent item.voidMoves an item in the array of items contained by this collection from one parent to another.static CollectionStatic factory method to create the new collection.static CollectionpmcFactory(PostmanID id) static CollectionpmcFactory(File jsonFile) Static factory method to create a new Collection from a Postman collection JSON file.static CollectionpmcFactory(String json) static CollectionpmcFactory(URL collectionURL) Create a collection via the Postman APIvoidremoveVariable(Property varNew) Remove variable from the array of key-value pairs comprising this collectionsvariablearray element.voidremoveVariable(String key) Remove variable with the specified key from the array of key-value pairs comprising this collectionsvariablearray element.resolveVariables(String src) voidsetAuth(RequestAuth auth) Set theauthobject property of this collection with aRequestAuthobject containing the values.voidsetDescription(String desc) Set the value of thedescriptionpropertyvoidsetExporterID(String id) Set the_exporter_idproperty of theinfoobject property of this collectionvoidset all elements of theinfoproperty object of this collection.voidSet the value of this collectionsinfoobject property with a populated Hash<String> containing the key-value pairs.voidvoidsetPostmanID(String id) Set the_postman_idproperty of theinfoobject property of this collectionvoidsetSchemaURI(URI schemaURI) Set theschemaproperty of theinfoobject property of this collectionvoidsetVariables(PropertyList<Property> vars) Set the array of key-value pairs in this collectionsvariablearray elementtoJson()Generate JSON as text from the values in this collection.upsertToPostman(PostmanID workspaceID) Create or update this collection in Postman.voidwriteToFile(File outputFile) Write this collections generated JSON to a file at the specified path.Methods inherited from class com.postman.collection.ItemGroup
addItem, addItem, addItems, getFolder, getItem, getItem, getItems, getItems, hasItem, removeItem, removeItem, setItemsMethods inherited from class com.postman.collection.Item
getEvent, getEvents, getParent, getPreRequestScript, getTestScript, setEvents, setParent, setPreRequestScript, setTestScriptMethods inherited from class com.postman.collection.CollectionElement
getCollection, getDefaultCollectionSchema, getDefaultValidationSchema, getUUID, getValidationMessages, isEquivalentTo, setParent, setUUID, toJson, validate, validate
-
Constructor Details
-
Collection
Construct a new, empty collection with the specified name
-
-
Method Details
-
moveItem
public void moveItem(String itemToMoveKey, String parentKey) throws RecursiveItemAddException, InvalidCollectionActionException Moves an item in the array of items contained by this collection from one parent to another.- Parameters:
itemToMoveKey-parentKey-- Throws:
InvalidCollectionActionException- If either the parent or item to be moved aren't present in theitemarrayRecursiveItemAddException
-
addFolder
public Folder addFolder(String name) throws RecursiveItemAddException, IllegalPropertyAccessException Convenience method to add an item with no child items to this collection.- Parameters:
name- The name for the new item- Throws:
RecursiveItemAddExceptionIllegalPropertyAccessException
-
addFolder
public Folder addFolder(String name, int index) throws RecursiveItemAddException, IllegalPropertyAccessException Convenience method to add an item with no child items to this collection.- Parameters:
name- The name for the new item- Throws:
RecursiveItemAddExceptionIllegalPropertyAccessException
-
addRequest
public Request addRequest(RequestBody reqElement, String name, Response response) throws RecursiveItemAddException, IllegalPropertyAccessException Create and add a newrequestas a top level child item of this collection.- Parameters:
newRequest- The new request to addname- The name for the new itemresponse- A response to include in the request item, or null to ignore- Returns:
- Item The new Request item
- Throws:
RecursiveItemAddException- If this collection already include this instance in it's array of items.IllegalPropertyAccessException
-
addRequest
public Request addRequest(String url) throws RecursiveItemAddException, IllegalPropertyAccessException, InvalidCollectionActionException, DuplicateVariableKeyException Create and add a newrequestas a top level child item of this collection. The request will be named 'New Request'- Parameters:
url- The URL for the request. Cannot be null or non-zero- Returns:
- Item The new Request item
- Throws:
RecursiveItemAddException- If this collection already include this instance in it's array of items.DuplicateVariableKeyException- If parsing the URL results in the creation of duplicate parameter variables.IllegalPropertyAccessExceptionInvalidCollectionActionExample- If a null or zero-length string URL is passed.InvalidCollectionActionException
-
addRequest
public Request addRequest(RequestBody reqElement, String name) throws RecursiveItemAddException, IllegalPropertyAccessException Convenience method to add a newrequestitem as a top level child item of this collection- Parameters:
reqElement-name-- Returns:
- Item
- Throws:
RecursiveItemAddException- If this collection already include this instance in it's array of items.IllegalPropertyAccessException
-
getRequest
Return a request from this Collectionsitemproperty, or null if it doesn't exist.- Overrides:
getRequestin classItemGroup
-
addRequest
public Request addRequest(RequestBody newRequest, String name, ArrayList<Response> responses) throws RecursiveItemAddException, IllegalPropertyAccessException Convenience method to add a request with a pre-constructed ArrayList<Response> of response items.- Parameters:
newRequest- The new Request to addname- The name of the requestresponses- Pre-constructed ArrayList of response objects.- Returns:
- Item The new request item.
- Throws:
RecursiveItemAddException- If this collection already include this instance in it's array of items.IllegalPropertyAccessException
-
addRequest
public Request addRequest(RequestBody reqElement, String name, int position) throws RecursiveItemAddException, IllegalPropertyAccessException Add a new request to this collection at the specified position in the array ofrequestelements- Parameters:
reqElement-name-position-- Throws:
RecursiveItemAddException- If this collection already include this instance in it's array of items.IllegalPropertyAccessException
-
moveItem
public void moveItem(Item itemToMove, ItemGroup newParent) throws RecursiveItemAddException, InvalidCollectionActionException Move an item to a different parent item.- Parameters:
itemToMove- The item to movenewParent- The item's new parent- Throws:
RecursiveItemAddException- If the parent item is the same as the new child item, or if the parent item already contains this item.InvalidCollectionActionException
-
addCollection
public void addCollection(Collection newColl, ItemGroup parent) throws RecursiveItemAddException, InvalidCollectionActionException, IllegalPropertyAccessException Add another collection and it's array of
item,variable, andeventarrays to this collection as children ofparent- Parameters:
newColl- The collection to combine with this oneparent-- Throws:
RecursiveItemAddException- If the new collection is the same as this collectionInvalidCollectionActionException- If the specified parent is not a folder (e.g., contains a request element)IllegalPropertyAccessException
-
addCollection
public void addCollection(Collection newColl, boolean copyScripts, boolean copyVariables) throws RecursiveItemAddException, InvalidCollectionActionException, IllegalPropertyAccessException Add another collection and it's array of
item, optionallyvariable, and optionallyeventarrays to this collection as children of a new Folder child item of this collection.- Parameters:
newColl- The collection to combine with this onecopyScripts- Whether to copy the source collections events to the new parent foldercopyVariables- Whether to copy the source collections variables to the target collections array ofvariableelements. Note there is no checking for namespace collisions.- Throws:
RecursiveItemAddException- If the new collection is the same as this collectionInvalidCollectionActionException- If the specified parent is not a folder (e.g., contains a request element)IllegalPropertyAccessException
-
addCollection
public void addCollection(Collection newColl, ItemGroup parent, boolean copyScripts, boolean copyVariables) throws RecursiveItemAddException, InvalidCollectionActionException, IllegalPropertyAccessException Add another collection and it's array of
item, optionallyvariable, and optionallyeventarrays to this collection as children of the specified parent.- Parameters:
newColl- The collection to combine with this oneparent- The new parent objectcopyScripts- Whether to copy the source collections events to the new parent foldercopyVariables- Whether to copy the source collections variables to the target collections array ofvariableelements. Note there is no checking for namespace collisions.- Throws:
RecursiveItemAddException- If the new collection is the same as this collectionInvalidCollectionActionException- If the specified parent is not a folder (e.g., contains a request element)IllegalPropertyAccessException
-
addCollection
public void addCollection(Collection newColl) throws RecursiveItemAddException, InvalidCollectionActionException, IllegalPropertyAccessException Add another collection and it's array of
item,variable, andeventarrays to this collection in a new Folder. The new folder will have the same name as the collection added- Parameters:
newColl- The collection to add- Throws:
RecursiveItemAddException- If the new collection is the same as this collectionInvalidCollectionActionExceptionIllegalPropertyAccessException
-
setName
-
setDescription
Description copied from class:ItemSet the value of thedescriptionproperty- Overrides:
setDescriptionin classItem- Parameters:
desc-
-
getDescription
Description copied from class:ItemReturn the value of thedescriptionproperty- Overrides:
getDescriptionin classItem- Returns:
- String
-
pmcFactory
Static factory method to create the new collection. Default constructor is undefined to avoid conflict with Gson.- Returns:
- Collection
-
pmcFactory
Static factory method to create a new Collection from a Postman collection JSON file.- Parameters:
jsonFile- The Postman Collection JSON file to ingest- Returns:
- Collection The new collection
- Throws:
FileNotFoundException- If the specified JSON file does not existIOException- If an IO exception occurs attempting to read the file, eg., inadequate permissions, etc.
-
pmcFactory
- Parameters:
json-- Returns:
- Collection
-
pmcFactory
public static Collection pmcFactory(URL collectionURL) throws IOException, InterruptedException, IllegalArgumentException, CollectionNotFoundException, ValidationException, InvalidCollectionActionException Create a collection via the Postman API- Parameters:
collectionURL- URL for the collection to ingest- Returns:
- Collection The new collection
- Throws:
IOExceptionInterruptedExceptionIllegalArgumentExceptionCollectionNotFoundException- If there is no collection at the specified URLValidationException- If the JSON returned by the specified URL does not conform to the Postman schemaInvalidCollectionActionException- If any other error occurs during the generation of the collection asdfasdf
-
pmcFactory
public static Collection pmcFactory(PostmanID id) throws IOException, InterruptedException, CollectionNotFoundException, ValidationException, InvalidCollectionActionException - Parameters:
id-- Returns:
- Collection
- Throws:
IOExceptionInterruptedExceptionCollectionNotFoundExceptionValidationExceptionInvalidCollectionActionException
-
getName
-
setInfo
Set the value of this collectionsinfoobject property with a populated Hash<String> containing the key-value pairs. Note that the keys are not validated.- Parameters:
newInfo-
-
getPostmanID
Return the_postman_idproperty of theinfoobject property of this collection- Returns:
- String The Postman ID
-
setPostmanID
Set the_postman_idproperty of theinfoobject property of this collection- Parameters:
id- The new ID
-
setExporterID
Set the_exporter_idproperty of theinfoobject property of this collection- Parameters:
id-
-
setSchemaURI
Set theschemaproperty of theinfoobject property of this collection- Parameters:
schemaURI-
-
setInfo
public void setInfo(String name, String description, String postmanID, String exporterID, String schemaURI) throws URISyntaxException set all elements of theinfoproperty object of this collection. Values are not validated- Parameters:
name- Thenameproperty of the collectiondescription- Thedescriptionproperty of the collectionpostmanID- The_postman_idproperty of this collectionexporterID- The_exporter_idproperty of this collecitonschemaURI- The URI for theschemaof this collection- Throws:
URISyntaxException- If the provided string forschemaURIis not a properly constructed URI
-
writeToFile
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 a corresponding Postman generated file. However, this does not affect the validity or functionality of the generated JSON.- Parameters:
outputFile- The file into which to write the JSON- Throws:
IOException- If there is an error attempting to create or write to the specified path
-
upsertToPostman
public PostmanID upsertToPostman() throws IOException, InterruptedException, CollectionNotFoundException, InvalidCollectionActionException -
upsertToPostman
public PostmanID upsertToPostman(PostmanID workspaceID) throws IOException, InterruptedException, CollectionNotFoundException, InvalidCollectionActionException Create or update this collection in Postman. - If this collection has a Postman ID, it will be updated in Postman - If this collection does not have a Postman ID, it will be created as a new collection, and the ID returned by Postman will be assigned to it. - You can optionally include a workspace ID for a workspace to link a newly created collection to.- Parameters:
WorkspaceID- Optional workspace ID to which to link a newly created collection.- Throws:
IOException- If there is an error attempting to create or write to the specified pathInterruptedException- If the HTTP request is interruptedCollectionNotFoundException- if the collection ID is not found in PostmanInvalidCollectionActionException- If an HTTP status code other than 200 or 404 is returned.
-
setAuth
Set theauthobject property of this collection with aRequestAuthobject containing the values.- Parameters:
auth- The new auth values, or null to remove an existing auth.
-
getAuth
Return the values in theauthobject property of this collection, or null if it has not been set.- Returns:
- RequestAuth The auth values, or null.
-
resolveVariables
- Parameters:
src-- Returns:
- String
- Throws:
VariableResolutionException
-
toJson
Generate JSON as text from the values in this collection.- Overrides:
toJsonin classCollectionElement- Returns:
- String
-
getInfo
Return a Map<String> containing the key-value pairs comprising theinfoobject property of this collection.- Returns:
- Map<String> The key-value pairs.
-
getKey
-
setVariables
Set the array of key-value pairs in this collectionsvariablearray element- Parameters:
vars- The ArrayList<Property> containing the variables
-
addVariable
Add or replace variable to the collection of variables comprising this collectionsvariablearray property. If a variable with the samekeyalready exists in the collection it is replaced.- Parameters:
varNew-
-
removeVariable
Remove variable with the specified key from the array of key-value pairs comprising this collectionsvariablearray element.- Parameters:
key- Key of the variable to remove
-
removeVariable
Remove variable from the array of key-value pairs comprising this collectionsvariablearray element.- Parameters:
varNew- The variable to remove. Matching is by the value ofkey
-
getVariable
Return the Property key-value pair from this collection'svariablearray element, or null if it is not present.- Parameters:
key-- Returns:
- Property
-
getVariables
Get the ArrayList<Property> containing the key-value pairs comprising thevariablearray element of this collection- Returns:
- ArrayList<
Property>
-
addVariables
- Parameters:
newVars-
-