Skip to content

Instantly share code, notes, and snippets.

@leonmaia
Last active April 9, 2019 00:09
Show Gist options
  • Save leonmaia/ca9d1a2d2589502fd869 to your computer and use it in GitHub Desktop.
Save leonmaia/ca9d1a2d2589502fd869 to your computer and use it in GitHub Desktop.
readme

newmotion api

An charging system in Finagle with Scala

Introduction

Charging System API built using Finagle with Scala.

Libraries:

  • Joda-Time: SimpleDateFormat is not thread-safe.
  • Scalatest: Test library for scala.
  • Redis: Finagle-Redis was choosed for this task. Jedis is another pretty good library but Finagle-Redis is non-blocking, which led me to choose it.

Overview

POST /transactions

Create new transaction. Required fields are:

  • customerId
  • startTime
  • volume
  • activeStarting, and should be after current time

Example

curl -H 'content-type: application/json' http://localhost:8088/transactions -d '{ "customerId": "john", "startTime": "2015-10-28T09:34:17Z", "endTime": "2015-10-28T16:45:13Z", "volume": 32.03 }'

POST /tariffs

Create new tariff. Required fields are:

  • startFee
  • hourlyFee
  • feePerkWh
  • activeStarting, and should be after current time

Example

curl -H 'content-type: application/json' http://localhost:8088/tariffs -d '{ "startFee": 0.20, "hourlyFee": 1.00, "feePerKWh": 0.25, "activeStarting": "2015-08-18T06:00:00Z" }'

GET /overview

Overview of charge sessions.

Example

curl http://localhost:8088/overview

Response:

pete,2014-10-27T13:32:14Z,2014-10-27T14:32:14Z,13.21,4.50 john,2014-10-28T09:34:17Z,2014-10-28T16:45:13Z,32.03,14.70

With content-type:"text/csv"

GET /invoices/<year>/<month number>/<customer name>

Customer invoice.

Example

curl http://localhost:8088/invoices/2015/10/john

Response:

Dear john,

In October 2015, you have charged:
from 2015-10-28 09:34 to 2015-10-28 16:45: 32.03 kWh @ € 15.39

Total amount: € 15.39

Kind regards, Your dearest mobility provider, The Venerable Inertia

With content-type:"text/txt"

How to Run

Vagrant

  1. Install vagrant, and ansible.
$ git clone [email protected]:leonmaia/newmotion.git
$ cd ./path/to/newmotion/repo
$ vagrant up --provision

Upstart is being used to run the api as a ubuntu service.

Local

$ git clone [email protected]:leonmaia/newmotion.git
$ cd ./path/to/newmotion/repo
$ java -Dfile.encoding=UTF-8 -jar target/scala-2.11/newmotion-api-assembly-1.0.jar

Local - Dev Mode

$ git clone [email protected]:leonmaia/newmotion.git
$ cd ./path/to/newmotion/repo
$ ./sbt run

Running tests

Tests are run using sbt.

$ ./sbt test

Or you can view a coverage report:

$ ./sbt cov

And open the index.html file at ./path/to/newmotion/repo/target/scala-2.11/scoverage-report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment