Custom resource-path

Hi,

Sorry if this is the wrong category.

I created a “Custom Scripting Service” running at “…api/v2/clients/”, but i need to create a resource-path, like “…api/v2/clients/list_something” or “…api/v2/clients/do_something/diferent”?

Can i have custom scripts for each one of those “custom endpoint”?

Thanks!

1 Like

Are you using the standard customer scripting service and are you using DF2.0? I don’t understand what you mean with: > running at "...api/v2/clients/, or do you mean your custom server script is called clients and within that script you have different scripts / functions defined?

1 Like

Hi @mrjohny,

Yes, i using a Custom Scripting Service, DF2.0.4, and yes, the service is called “clients” :slight_smile:

I want to have different resources/operations in this service, for example:

/clients/order/confirm/{orderId} “confirm the order”
/clients/order/cancel/{orderId} “cancel the order”
/clients/order/send/{orderId} “send the order to an email”
/clients/order/{orderId} “get an specific order”
/clients/registration/
/clients/my_custom_operation/

Thank you!

OK, i’m not running the latest version myself, so that is probably what caused a bit of confusion :slightly_smiling: , so i’m not sure if my answer is really going to help.

I think the simple answer is: yes, it should be possible. I’m not sure how new you are to DF, but did you read this document:

https://github.com/dreamfactorysoftware/dsp-core/wiki/API-Access-via-Scripts

I have around 20 scripts for 1 application and I call them all separately with different values/parameters

First week with DF :smile:

I’ve already read the doc, but its not exactly what i want.

I dont want to use params to select what operation/action i will run, if i can use the url (i just dont know how exactly) :sweat_smile:, but its possible, DF use this in his native services (system).

Did you keep these scripts in files or each script is a custom service and you use platform.api.get to invoke?
You could post some examples of your “Request URL”?

Thanks!

Sure. I’m using it in combination with Ajax on the client side. To call the scripts I’m using something like this:

declare some variable, for example (but you can use different names):
var AP = put your application key here, you can find it after you created yours under Apps;
var UT =put your session token here, it’s provided when your user logged in; there are way where your user doesn’t need to login;
var var1 = some variable;
var var2 = some other variable;

$.ajax({
	type: 'POST', 
	beforeSend: function(request) {
		request.setRequestHeader('X-DreamFactory-Api-Key', AP);
		request.setRequestHeader('X-DreamFactory-Session-Token', UT);
	}, 
	url: '//177.77.88.88/api/v2/*name of your script*?n1=' + var1 + '&n2=' + var2,
	//dataType: "JSON", //use this if you want JSON back 
	dataType: "TEXT", //use this if you want text / string back
	contentType: "JSON",
	
	success: function(response) {
		console.log(response); 
	},
	error: function(xhr, ajaxOptions, thrownError) { 
		console.log(thrownError); 
		console.log(ajaxOptions);
	}
});

If you want to call a script or db service within the the custom script you would use platform.api.post, for example something like this:

var adddata = platform.api.post("mysql/_table/Pictures", {"resource": [{"Picture_addedby" : username, "Picture_date" : dformat, "Picture_number" : picturename, "Picture_width" : width, "Picture_height" : height}]});

Also make sure that under roles you give access to a user to use the script API side (meaning the first example) or on the server side (SCRIPT) and what types you want to use. So for example if you want a user to call your script from a webpage, you would allow POST + API.

Hope it helps.

Maybe to add: so what you can do is the following URL:

url: '//177.77.88.88/api/v2/order?n1=' + scriptname + '&n2=' + user,

And pick this up on the server side (e.g. V8js)

//input variables
var params = event.request.parameters;
var procedure = params.n1;
var user = params.n2;

if (procedure === "confirm") {
        //do something
} else {
        //do something else
}

return "something";

But I would personally create multiple scripts and just call these. It makes it easier I would say, unless there are many functions you want to reuse and you don’t want to call scripts on the server side.

Thanks man, for the enlightening examples. Maybe that’s the only way to do what I want, even disliking hahahaha

Thanks for you time, u helped alot.

It really depends from where you want to call it and how you want to organize it I guess :slightly_smiling:

I agree, organization is the key :slight_smile: