Creating and deleting a library

  • Date September 13, 2019
  • Written by Vasilij Nevlev
  • Category Curl

This guide shows how to create and delete library using SAS9API and CURL.

Objectives:

  • How to create (pre-assigned) library with a Post-request and CURL.
  • How to verify the result with Get-request and CURL.
  • How to delete (pre-assigned) library with Delete-request and CURL.

 

Introduction

There are two types of libraries: permanent (pre-assigned) and temporary. SAS permanent (pre-assigned) libraries are saved until you delete them. The permanent (pre-assigned) library is available for processing in the following SAS sessions. The SAS temporary library exists only for the current SAS session.

Step 1 – Use this parameters.

To create a permanent (pre-assigned) library, you can use this table:

Parameter Description
0 ServerName Your server name
1 LibraryName Created library name, as will be used in LIBNAME statement
2 RepositoryName Repository name
3 Engine Libname engine
4 DisplayName Library display name
5 Path Library data path
6 Location Folder to place metadata object

 

Step 2 – Make a POST-request.

Make a POST-request with your parameters:

/sas/servers/ServerName/libraries/SAS9API?repositoryName=Name&engine=V9&displayName=Demo%20Data&path=%2Fpubl%2Fdata&location=%2FUser%20Folders%2Fsasdev%2FMy%20Folder&isPreassigned=true

Our CURL request looks like this:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/xml' 'http://Server/serverSuffix//sas/servers/ServerName/libraries/SASHELP?repositoryName=Name&engine=V9&displayName=demolib&path=%2Fdata&location=%2FUser%20Folders%2Fsasdev%2FMy%20Folder&amp'

Status 200 – success without errors. All the parameters are the same as in our request:

{
	"status": 200,
	"error": null,
	"payload": {
    	"id": "A5X8AHW1.B40001QP",
    	"libname": "SAS9API",
    	"engine": "V9",
    	"path": "/data",
    	"level": 0,
    	"readonly": false,
    	"sequential": false,
    	"temp": false
	}
}

The SAS9API permanent (pre-assigned) library was created successfully.

Step 3 – Verification.

For verification, you can send a request to the server to get all the parameters of the new library or display a list of all libraries.
In the first case, we use GET-request “Get library information for workspace server by library name”.

/sas/libraries/SAS9API

Our CURL request looks like this:

curl -X GET --header 'Accept: application/xml' 'http://server/serverSuffix//sas/libraries/SAS9API?serverUrl=serverUrl&serverPort=Port'

The response is the information about the created library SAS9API:

{
	"status": 200,
	"error": null,
	"payload": {
    	"id": null,
    	"libname": "SAS9API",
    	"engine": "V9",
    	"path": "/pub/data",
    	"level": 0,
    	"readonly": false,
    	"sequential": false,
    	"temp": false
	}
}

The second way: get the list of libraries for workspace server:

/sas/libraries/?serverUrl={{ServerURI}}&serverPort={{ServerPort}}

We to run the following CURL command:

curl -X GET --header 'Accept: application/xml' 'http://server/serverSuffix//sas/libraries?serverUrl=serverUrl&serverPort=port'

Response is a complete list of our libraries including SAS9API:

  
},
{
        	"id": null,
        	"libname": "SAS9API",
        	"engine": "V9",
        	"path": "/pub/data",
        	"level": 0,
        	"readonly": false,
        	"sequential": false,
        	"temp": false
    	},
    	{
        	"id": null,
        	"libname": "SASDATA",
        	"engine": "BASE",
        	"path": "/srv/sas/config/Lev1/ServerName/Data",
        	"level": 0,
    	    "readonly": false,
        	"sequential": false,
        	"temp": false
    	},
{ …

 

Step 4 – Deleting library.

If you want to remove your library, you should know the libraries name (SAS9API) and send a DELETE request:

/sas/servers/{{ServerName}}/libraries/SAS9API/

We to run the following CURL command:

curl -X DELETE --header 'Accept: application/xml' 'http://server/serverSuffix//sas/servers/serverName/libraries/SAS9API?repositoryName=name'

Response is:

{
	"status": 200,
	"error": null,
	"payload": true
}

Status 200 – success without errors.

Step 5 – Check if the library was deleted.

For this we send a GET-request to show the parameters of the library by name (Step 3).
In the response, the status 500 is written – an internal server error and the reason for the error is described: “The library was not found».

{
	"status": 500,
	"error": "java.lang.Exception: Library 'SAS9API' was not found",
	"payload": null
}

The library with the name SAS9API does not exist.

Conclusion

This example showed how to create and delete a permanent (pre-assigned) library and how to check it using SAS9API and CURL.

For this and other examples, see www.sas9api.io/examples.