rdex

package module
v0.0.0-...-0ccbfcb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

README

RDEX Go server and client implementation

Go (Golang) implementation of the RDEX carpooling data format.

RDEX standard

This library implements :

Notes about the API authentication implementation

RDEX v1.2.1 protocol specifies the authentication method as a public/private key mechanism with a signature in the request URL.

This implementation extends the protocol authentication mechanism by allowing API authentication through :

  • API key in the HTTP header
  • Bearer authentication
Public/private key with signature (as described in the RDEX protocol specification)

TODO : not yet implemented

API key authentication

The API key authentication uses the "X-API-KEY" request header with the operator's private key.

In this case, remove the api_key and signature from the URL and add a X-API-KEY header to the request.

Bearer authentication

TODO : not yet implemented

Supported features

We focus first on JSON implementation and the "journeys.json" endpoint

  • RDEX Server/API implementation
    • Base protocol
      • Journeys (JSON)
      • Journeys (XML)
      • Connexions (JSON & XML)
    • Authentication
      • Public/Private key with signature (as described in the RDEX protocol)
      • API KEY in the header
      • Bearer token
  • RDEX client
    • Journeys
    • Connexions

How to use

Server

You can easily generate a server implementation of the RDEX API, by implementing the function to retrieve the carpooling data.

Example implementation :

You will find an example implementation in the "examples/server" folder

To run the example, go to this folder and run :

$ go run server.go

Then, run cURL command :

$ curl -H "X-API-KEY: 123456" -X GET "http://localhost:8080/journeys.json?p[driver][state]=1&frequency=punctual&p[passenger][state]=0&p[from][latitude]=45.128038&p[from][longitude]=5.587706&p[to][latitude]=45.172348&p[to][longitude]=5.62839&p[outward][mindate]=2015-12-14&p[outward][maxdate]=2015-12-14" | jq
Client

TODO

Contributions

We are open to contributions : feel free to submit pull requests !

Licence

Copyright 2021 Scity.coop

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConvertibleBoolean

type ConvertibleBoolean bool

func (*ConvertibleBoolean) MarshalJSON

func (bit *ConvertibleBoolean) MarshalJSON(data []byte) ([]byte, error)

func (*ConvertibleBoolean) UnmarshalJSON

func (bit *ConvertibleBoolean) UnmarshalJSON(data []byte) error

type RDEXCost

type RDEXCost struct {
	Fixed    float64 `json:"fixed,omitempty"`
	Variable float64 `json:"variable,omitempty"`
}

RDEXCost is the cost of the journey, either fixed or variable

type RDEXDays

type RDEXDays struct {
	Monday    ConvertibleBoolean `json:"monday"`
	Tuesday   ConvertibleBoolean `json:"tuesday"`
	Wednesday ConvertibleBoolean `json:"wednesday"`
	Thursday  ConvertibleBoolean `json:"thursday"`
	Friday    ConvertibleBoolean `json:"friday"`
	Saturday  ConvertibleBoolean `json:"saturday"`
	Sunday    ConvertibleBoolean `json:"sunday"`
}

RDEXDays defines which days of the week the journey happens

type RDEXError

type RDEXError struct {
	Name         string  `json:"name"`
	MessageDebug *string `json:"message_debug,omitempty"`
	MessageUser  *string `json:"message_user,omitempty"`
	Field        *string `json:"field,omitempty"`
}

type RDEXGeography

type RDEXGeography struct {
	Address      string              `json:"address"`
	City         string              `json:"city"`
	Postalcode   string              `json:"postalcode,omitempty"`
	Country      string              `json:"country,omitempty"`
	Latitude     float64             `json:"latitude"`
	Longitude    float64             `json:"longitude"`
	StepDistance int64               `json:"step_distance,omitempty"`
	StepDuration time.Duration       `json:"step_duration,omitempty"`
	Type         string              `json:"type,omitempty"` // pick-up or drop-point
	Mandatory    *ConvertibleBoolean `json:"mandatory,omitempty"`
}

RDEXGeography is a geographical point used in From, To or Waypoints

type RDEXJourney

type RDEXJourney struct {
	UUID              string              `json:"uuid"`
	Operator          string              `json:"operator"`
	Origin            string              `json:"origin"`
	URL               *string             `json:"url,omitempty"`
	Driver            *RDEXPerson         `json:"driver,omitempty"`
	Passenger         *RDEXPerson         `json:"passenger,omitempty"`
	From              RDEXGeography       `json:"from"`
	To                RDEXGeography       `json:"to"`
	Distance          int64               `json:"distance"`
	Duration          time.Duration       `json:"duration"`
	Route             *string             `json:"route,omitempty"`
	NumberOfWaypoints *int                `json:"number_of_waypoints,omitempty"`
	Waypoints         *[]RDEXGeography    `json:"waypoints,omitempty"`
	Cost              RDEXCost            `json:"cost"`
	Details           *string             `json:"details,omitempty"`
	Vehicle           *RDEXVehicle        `json:"vehicle,omitempty"`
	Frequency         string              `json:"frequency"` // punctual or regular
	Type              string              `json:"type"`      // one-way or round-trip
	Realtime          *ConvertibleBoolean `json:"real_time,omitempty"`
	Stopped           *ConvertibleBoolean `json:"stopped,omitempty"`
	Days              RDEXDays            `json:"days"`
	Outward           RDEXSchedule        `json:"outward"`
	Return            *RDEXSchedule       `json:"return,omitempty"`
}

RDEXJourney is the carpooling journey base type of the RDEX protocol

type RDEXOperator

type RDEXOperator struct {
	OperatorName string
	PrivateKey   string
	PublicKey    string
}

func (*RDEXOperator) Signature

func (o *RDEXOperator) Signature(url string) string

type RDEXParamFrom

type RDEXParamFrom struct {
	Latitude  float64 `json:"p[from][latitude]"`
	Longitude float64 `json:"p[from][longitude]"`
}

RDEXParamFrom is a latitude/longitude parameter from the RDEX query origin

type RDEXParamTo

type RDEXParamTo struct {
	Latitude  float64 `json:"p[to][latitude]"`
	Longitude float64 `json:"p[to][longitude]"`
}

RDEXParamTo is a latitude/longitude parameter from the RDEX query destination

type RDEXParameters

type RDEXParameters struct {
	Driver    bool `schema:"p[driver]"`
	Passenger bool `schema:"p[passenger]"`
	From      RDEXParamFrom
	To        RDEXParamTo
	Frequency string `schema:"p[frequency]"` // regular or punctal
	Outward   RDEXSchedule
}

RDEXParameters defines the RDEX parameters used to search a journey

type RDEXPerson

type RDEXPerson struct {
	UUID  *string            `json:"uuid,omitempty"`
	Alias *string            `json:"alias,omitempty"`
	Image *string            `json:"image,omitempty"`
	Seats *int               `json:"seats,omitempty"`
	State ConvertibleBoolean `json:"state,omitempty"`
}

RDEXPerson defines a driver or a passenger

type RDEXRequest

type RDEXRequest struct {
	Timestamp int64          `schema:"timestamp"`
	Apikey    string         `schema:"apikey"`
	P         RDEXParameters // TODO handle both journeys and connections
	Signature string         `schema:"signature"`
}

RDEXRequest describes a base request as sent and received using RDEX protocol

func ParseRDEXRequest

func ParseRDEXRequest(r *http.Request) (req RDEXRequest)

type RDEXSchedule

type RDEXSchedule struct {
	MinDate   string          `json:"mindate" schema:"mindate"`
	MaxDate   string          `json:"maxdate" schema:"maxdate"`
	Monday    *RDEXTimeLimits `json:"monday,omitempty"`
	Tuesday   *RDEXTimeLimits `json:"tuesday,omitempty"`
	Wednesday *RDEXTimeLimits `json:"wednesday,omitempty"`
	Thursday  *RDEXTimeLimits `json:"thursday,omitempty"`
	Friday    *RDEXTimeLimits `json:"friday,omitempty"`
	Saturday  *RDEXTimeLimits `json:"saturday,omitempty"`
	Sunday    *RDEXTimeLimits `json:"sunday,omitempty"`
}

RDEXSchedule is used to define outward and return datetimes

type RDEXServer

type RDEXServer struct {
	BaseUrl             string
	AuthorizedOperators []RDEXOperator
	Handler             RDEXServerHandler
}

RDEXServer is a handler to create a RDEX API server

func NewServer

func NewServer(baseUrl string, handler RDEXServerHandler) (*RDEXServer, error)

NewServer creates a RDEX server handler

func (*RDEXServer) AddAuthorizedOperator

func (s *RDEXServer) AddAuthorizedOperator(o RDEXOperator)

AddAuthorizedOperator adds credentials (public key / private key) for an operator accessing the API

func (*RDEXServer) AuthenticateOperator

func (s *RDEXServer) AuthenticateOperator(r *http.Request) (*RDEXOperator, error)

func (RDEXServer) ServeHTTP

func (s RDEXServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

type RDEXServerHandler

type RDEXServerHandler interface {
	Journeys(RDEXParameters) ([]RDEXJourney, error)
}

type RDEXTimeLimits

type RDEXTimeLimits struct {
	MinTime string `json:"mintime"`
	MaxTime string `json:"maxtime"`
}

RDEXTimeLimits defines the minimum time and maximum time for the journey in a day

type RDEXVehicle

type RDEXVehicle struct {
	VehicleImage string `json:"vehicle_image,omitempty"`
	Model        string `json:"model,omitempty"`
	Color        string `json:"color,omitempty"`
}

RDEXVehicle is the vehicle description

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL