Badgeyay is no longer a simple badge generator. It has more cool features than before.
Badgeyay now supports a feature that shows your badges. It is called ‘my-badges’ component. To get this component work, we need to design a backend API to deliver the badges produced by a particular user.
Why do we need such an API?
The main aim of Badgeyay has changed from being a standard and simple badge generator to a complete suite that solves your badge generation and management problem. So to tackle the problem of managing the produced badges per user, we need to define a separate route and schema that delivers the generated badges.
Adding the functionality to backend
Let us see how we implemented this functionality into the backend of the project.
Step 1 : Adding a route
This step involves adding a separate route that provides with the generated output of the badges linked with the user account.
@router.route(‘/get_badges’, methods=[‘GET’]) |
This route allows us to get badges produced by the user as a JSON API data object. This object is fed to the frontend to render the badges as cards.
Step 2 : Adding a relevant Schema
After creating a route we need to add a relevant schema that will help us to deliver the badges generated by the user to the Ember JS frontend so that it can be consumed as JSON API objects and shown to the user.
class UserBadges(Schema): id = fields.Str(required=True, dump_only=True) |
This is the ‘UserBadge’ schema that produces the output results of the GET request on the route.
Finally, once this is done we can fire up a GET request on our deployment to receive results. The command that you need to run is given below.
$ ~ curl -X GET http://localhost:5000/api/get_badges?uid={user_id} |
Further Improvements
We are working on adding multiple routes and adding modifications to database models and schemas so that the functionality of Badgeyay can be extended to a large extent. This will help us in making this badge generator even better.
Resources
- The Pull Request for the same : https://github.com/fossasia/badgeyay/pull/949
- The Issue for the same : https://github.com/fossasia/badgeyay/issues/948
- Read about adding routes Blueprint : http://flask.pocoo.org/docs/1.0/blueprints/
- Read about Schemas : https://github.com/marshmallow-code/marshmallow-jsonapi