Dropwizard: Resource

This entry is part 4 of 5 in the series Dropwizard

In this tutorial I will give a basic example of a resource endpoint. If you haven’t configured Guice yet please do so before continuing.

So basically now that you have Guice configured and working you can now create an api endpoint. For this we will just use a GET but you can also do POST, PUT, DELETE.

package ca.gaudreault.mydropwizardapp.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.codahale.metrics.annotation.Timed;
import com.google.inject.Inject;

import ca.gaudraeult.mydropwizardapp.services.MyService;
import ca.gaudreault.mydropwizardapp.models.MyModel;

@Timed
@Path("/my-resource")
public class MyResource {
	MyService myService;

	@Inject
	public MyResource(final MyService myService) {
		this.myService = myService;
	}

	@GET
	@Timed
	@Produces(MediaType.APPLICATION_JSON)
	public MyModel runTest() {
		return this.myService.runTest();
	}
}

Once you run your application you can view the endpoint by going to http://localhost:8080/my-resource.

The output will be as follows.

{"value":123123}

If you noticed we added the “@Timed” annotation. You can now go to http://localhost:8081/metrics?pretty=true to view the metrics on our “runTest” method. The output will look like the below.

{
	"ca.gaudreault.mydropwizardapp.resources.MyResource.runTest": {
		"count": 0,
		"max": 0.0,
		"mean": 0.0,
		"min": 0.0,
		"p50": 0.0,
		"p75": 0.0,
		"p95": 0.0,
		"p98": 0.0,
		"p99": 0.0,
		"p999": 0.0,
		"stddev": 0.0,
		"m15_rate": 0.0,
		"m1_rate": 0.0,
		"m5_rate": 0.0,
		"mean_rate": 0.0,
		"duration_units": "seconds",
		"rate_units": "calls/second"
}

Python: Flask Resource

This tutorial helps setup flask restful api’s.

Install Python Packages:

Open cmd and navigate into your testApp folder and run the following commands.

pip install flask-RESTful && pip freeze > requirements.txt
__init__.py:

On the init of your application you will need to setup flask_restful. There are config options you could set for config.py. Look into it!

from flask_restful import Api
api = Api(app)

#Add api endpoints
#Get
api.add_resource(home.views.MyResource, '/home/getMyData/')

#Post
api.add_resource(home.views.MyResource, '/home/getMyData/', methods=['POST'])
Setup home views.py:

You need to import Resource in it’s most simplistic form. However if you want to deal with request parameters add in reqparse and inputs. Inputs give you access to boolean that way a boolean can be parsed into a python boolean easily.

from flask_restful import Resource, reqparse, inputs

You can now use get, post, etc. I will give you three examples below.

Get:

class MyResource(Resource):
	def get(self):
		return {}

Get /w Parameter:

class MyResource(Resource):
	def get(self, var):
		return {}

Get /w Parameter & Request Parameter:

class MyResource(Resource):
	def get(self, var):
        	parser = reqparse.RequestParser()
        	parser.add_argument('new_request_var', type=str, default='')

        	#If you want to have a boolean request parameter do the following.
        	parser.add_argument('new_request_var_bool', type=inputs.boolean, default=False)

        	args = parser.parse_args(strict=True)
        	new_request_var = args['new_request_var']
        	new_request_var_bool = args['new_request_var_bool']

		return {}

Post:

class MyResource(Resource):
	def post(self):
		return {}