/Python-REST-Client-Examples

Python REST client and examples for DNA-Evolutions TourOptimizer

Primary LanguagePython

Python-REST-Client-Examples by DNA-Evolutions

DNA-Evolutions

Containerizing an application helps to use it more conveniently across different platforms and, most importantly, as a microservice. Further, scaling an application becomes more straightforward as various standardized orchestration tools can be utilized. A Microservice can be launched either (locally) or, for example, as a highly-scalable web-micro-service in a Kubernetes cluster.


Compatibility

This client can be used with JOpt-TourOptimizer Spring Server Compatible Versions:

  • 1.2.6-SNAPSHOT (this version was used to create the models of this repository)
  • 1.2.7-SNAPSHOT (Specs )

Further Documentation, Contact and Links

If you need any help, don't hesitate to get in contact with us via our company website www.dna-evolutions.com or write an email to info@dna-evolutions.com.


Short Introduction

This repository is part of our JOpt-REST-Suite. It provides examples of how to set up a REST client in Python to access the following DNA Evolution's web services:

  • JOpt-TourOptimizer based on JOpt-Core (available as a local Container and via Azure)

The service can be called via an API-Key using our Microsoft Azure-Kubernetes Infrastructure. If you are interested in hosting our JOpt-REST-GeoCoder and JOpt-REST-GeoRouter products in your environment, please get in contact with us.

All our RESTful Services utilize Spring WebFlux and Swagger. Internally the Java version of TourOptimizer is used. Indeed all specifications for the different services are derived from the core library, leading to guaranteed compatibility between all three services.

DNA-Evolutions Integration

JOpt-TourOptimizer

Optimize a problem consisting of Nodes, Resources, and optionally externally provided connections. In contrast to our other services, we allow you to host your JOpt-TourOptimizer locally. Please refer to "How to start JOpt TourOptimizer in docker" for more help.


Outline of this repository

Examples

  1. TourOptimizer Python Examples

Each of the sections has its README.


The architecture of the generated REST-Client-API

The Python-REST-Client class files used by the examples of this repository were generated utilizing the openapi-python Generator by OpenAPI Tools.

For creating the models, we used the containerized version of Open-API-Generator by calling:

docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:latest generate  -i '/local/swagger/touroptimizer/spec/touroptimizer_spec.json' -g python  -o /local/generated/jopt-touroptimizer-py-client  --package-name=touroptimizer_py_client --additional-properties="useOneOfDiscriminatorLookup=true"

where ${PWD} needs to be adjusted to find the Open-API-docs under /local/swagger/touroptimizer/spec/touroptimizer_spec.json when mounting the volume ${PWD} into /local. Calling the command will generate the Python client that is part of this repository. You can find the touroptimizer_spec.json here.

Attention: The generated models seem to have some bugs. The first commit of this repository is fixing those issues.

You can also generate a client in the programming language of your choice utilizing our API-docs. REST facilitates software integration in your desired language (including famous ones like C#, Java, JS, Scala, Python, and many more ). Don't hesitate to reach out to us if you need help setting up your client.

For setting up a local test enviorment with database support, please refer to the separate Hands-on Tutorial: Setting Up a Local Fire and Forget TourOptimizer-Database Test Environment tutorial.


Getting started

You can start using our examples:

Clone this repository

Clone this repository and import it in your favourite IDE.

Prerequisites

  • Python installed
  • Please check requirements.txt
  • Working Docker environment for local TourOptimizer instance

Install necessary files

You can call (from the main folder):

python setup.py install

Run the examples

Run a file from the examples subfolders.

Use our sandbox in your browser (Docker required)

If you want to get started without the hassle of installing Python and an IDE, we provide a sandbox. The sandbox is based on code-server and can be used inside your browser, and the interface itself is based on Visual Code. The sandbox is available via DockerHub (here). You have to host the sandbox in your Docker environment (Please provide at least 2-4Gb of Ram and 2 Cores). You can pull the sandbox from our DockerHub account (The Dockerfile for creating the sandbox is included in this repository). The latest version of our examples is cloned by default on launching the Docker container, and you can start testing JOpt-PYthon-REST right away.

Starting the sandbox and persist your changes

You must mount a volume to which the examples of this project are downloaded on the container's startup. After re-launching the container, the latest version of our examples is only cloned if the folder is not already existing, keeping your files safe from being overridden.

Launching a sandbox and mount your current directory ('$PWD') or any other directory you want:

docker run -it -d --name jopt-py-rest-examples -p 127.0.0.1:8033:8080 -v "$PWD/:/home/coder/project" dnaevolutions/jopt_py_example_server:latest

Using the sandbox

After starting the container, you can open http://localhost:8033/ with your browser and login with the password:

jopt

Common problems:

  • If you see the an error like this:
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=8081): Max retries exceeded with url: /healthStatus (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fc7c86ac1d0>: Failed to establish a new connection: [Errno 111] Connection refused'))

You are trying to connect to a local JOpt server but have not adjusted the endpoint. Remember, the sandbox is a docker container and you need to connect to it via the endpoint http://host.docker.internal:8081 instead of http://localhost:8081. You can run tour_optimizer_example_from_docker.py from the package examples.optimize where Endpoints.LOCAL_SWAGGER_TOUROPTIMIZER_FROM_DOCKER_URL is used instead of Endpoints.LOCAL_SWAGGER_TOUROPTIMIZER_URL.


Why use JOpt products from DNA Evolutions?

JOpt is a flexible routing optimization engine written in Java, allowing to solve tour-optimization problems that are highly restricted. For example, regarding time windows, skills, and even mandatory constraints can be applied.

Click to open our video:

Introduction Video for DNA's JOpt


Agreement

For reading our license agreement and for further information about license plans, please visit www.dna-evolutions.com.


Authors

A product by dna-evolutions ©