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
ConstructorDescriptionCollection
(String name) Construct a new, empty collection with the specified name -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCollection
(Collection newColl) Add another collection and it's array ofitem
,variable
, andevent
arrays to this collection in a new Folder.void
addCollection
(Collection newColl, boolean copyScripts, boolean copyVariables) Add another collection and it's array ofitem
, optionallyvariable
, and optionallyevent
arrays to this collection as children of a new Folder child item of this collection.void
addCollection
(Collection newColl, ItemGroup parent) Add another collection and it's array ofitem
,variable
, andevent
arrays to this collection as children ofparent
void
addCollection
(Collection newColl, ItemGroup parent, boolean copyScripts, boolean copyVariables) Add another collection and it's array ofitem
, optionallyvariable
, and optionallyevent
arrays 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 newrequest
item 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 ofrequest
elementsaddRequest
(RequestBody reqElement, String name, Response response) Create and add a newrequest
as 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 newrequest
as a top level child item of this collection.void
addVariable
(Property varNew) Add or replace variable to the collection of variables comprising this collectionsvariable
array property.void
addVariables
(PropertyList<Property> newVars) getAuth()
Return the values in theauth
object property of this collection, or null if it has not been set.Return the value of thedescription
propertygetInfo()
Return a Map<String> containing the key-value pairs comprising theinfo
object property of this collection.getKey()
getName()
Return the_postman_id
property of theinfo
object property of this collectiongetRequest
(String name) Return a request from this Collectionsitem
property, or null if it doesn't exist.getVariable
(String key) Return the Property key-value pair from this collection'svariable
array element, or null if it is not present.Get the ArrayList<Property
> containing the key-value pairs comprising thevariable
array element of this collectionvoid
Move an item to a different parent item.void
Moves an item in the array of items contained by this collection from one parent to another.static Collection
Static factory method to create the new collection.static Collection
pmcFactory
(PostmanID id) static Collection
pmcFactory
(File jsonFile) Static factory method to create a new Collection from a Postman collection JSON file.static Collection
pmcFactory
(String json) static Collection
pmcFactory
(URL collectionURL) Create a collection via the Postman APIvoid
removeVariable
(Property varNew) Remove variable from the array of key-value pairs comprising this collectionsvariable
array element.void
removeVariable
(String key) Remove variable with the specified key from the array of key-value pairs comprising this collectionsvariable
array element.resolveVariables
(String src) void
setAuth
(RequestAuth auth) Set theauth
object property of this collection with aRequestAuth
object containing the values.void
setDescription
(String desc) Set the value of thedescription
propertyvoid
setExporterID
(String id) Set the_exporter_id
property of theinfo
object property of this collectionvoid
set all elements of theinfo
property object of this collection.void
Set the value of this collectionsinfo
object property with a populated Hash<String> containing the key-value pairs.void
void
setPostmanID
(String id) Set the_postman_id
property of theinfo
object property of this collectionvoid
setSchemaURI
(URI schemaURI) Set theschema
property of theinfo
object property of this collectionvoid
setVariables
(PropertyList<Property> vars) Set the array of key-value pairs in this collectionsvariable
array elementtoJson()
Generate JSON as text from the values in this collection.upsertToPostman
(PostmanID workspaceID) Create or update this collection in Postman.void
writeToFile
(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, setItems
Methods inherited from class com.postman.collection.Item
getEvent, getEvents, getParent, getPreRequestScript, getTestScript, setEvents, setParent, setPreRequestScript, setTestScript
Methods 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 theitem
arrayRecursiveItemAddException
-
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:
RecursiveItemAddException
IllegalPropertyAccessException
-
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:
RecursiveItemAddException
IllegalPropertyAccessException
-
addRequest
public Request addRequest(RequestBody reqElement, String name, Response response) throws RecursiveItemAddException, IllegalPropertyAccessException Create and add a newrequest
as 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 newrequest
as 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.IllegalPropertyAccessException
InvalidCollectionActionExample
- 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 newrequest
item 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 Collectionsitem
property, or null if it doesn't exist.- Overrides:
getRequest
in 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 ofrequest
elements- 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
, andevent
arrays 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 optionallyevent
arrays 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 ofvariable
elements. 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 optionallyevent
arrays 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 ofvariable
elements. 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
, andevent
arrays 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 collectionInvalidCollectionActionException
IllegalPropertyAccessException
-
setName
-
setDescription
Description copied from class:Item
Set the value of thedescription
property- Overrides:
setDescription
in classItem
- Parameters:
desc
-
-
getDescription
Description copied from class:Item
Return the value of thedescription
property- Overrides:
getDescription
in 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:
IOException
InterruptedException
IllegalArgumentException
CollectionNotFoundException
- 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:
IOException
InterruptedException
CollectionNotFoundException
ValidationException
InvalidCollectionActionException
-
getName
-
setInfo
Set the value of this collectionsinfo
object property with a populated Hash<String> containing the key-value pairs. Note that the keys are not validated.- Parameters:
newInfo
-
-
getPostmanID
Return the_postman_id
property of theinfo
object property of this collection- Returns:
- String The Postman ID
-
setPostmanID
Set the_postman_id
property of theinfo
object property of this collection- Parameters:
id
- The new ID
-
setExporterID
Set the_exporter_id
property of theinfo
object property of this collection- Parameters:
id
-
-
setSchemaURI
Set theschema
property of theinfo
object 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 theinfo
property object of this collection. Values are not validated- Parameters:
name
- Thename
property of the collectiondescription
- Thedescription
property of the collectionpostmanID
- The_postman_id
property of this collectionexporterID
- The_exporter_id
property of this collecitonschemaURI
- The URI for theschema
of this collection- Throws:
URISyntaxException
- If the provided string forschemaURI
is 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 theauth
object property of this collection with aRequestAuth
object containing the values.- Parameters:
auth
- The new auth values, or null to remove an existing auth.
-
getAuth
Return the values in theauth
object 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:
toJson
in classCollectionElement
- Returns:
- String
-
getInfo
Return a Map<String> containing the key-value pairs comprising theinfo
object property of this collection.- Returns:
- Map<String> The key-value pairs.
-
getKey
-
setVariables
Set the array of key-value pairs in this collectionsvariable
array element- Parameters:
vars
- The ArrayList<Property
> containing the variables
-
addVariable
Add or replace variable to the collection of variables comprising this collectionsvariable
array property. If a variable with the samekey
already 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 collectionsvariable
array element.- Parameters:
key
- Key of the variable to remove
-
removeVariable
Remove variable from the array of key-value pairs comprising this collectionsvariable
array element.- Parameters:
varNew
- The variable to remove. Matching is by the value ofkey
-
getVariable
Return the Property key-value pair from this collection'svariable
array element, or null if it is not present.- Parameters:
key
-- Returns:
- Property
-
getVariables
Get the ArrayList<Property
> containing the key-value pairs comprising thevariable
array element of this collection- Returns:
- ArrayList<
Property
>
-
addVariables
- Parameters:
newVars
-
-