Skip to content

Commit

Permalink
Issue #1 Create first version of pyinuse using internal API
Browse files Browse the repository at this point in the history
  • Loading branch information
Othman Moumni Abdou committed May 31, 2022
1 parent d320ebc commit 56863ce
Show file tree
Hide file tree
Showing 14 changed files with 552 additions and 2 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ target/
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
Expand Down
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.8.12
2 changes: 2 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Guillaume Thomas
- Othman Moumni Abdou
25 changes: 25 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Deploy package

Create a distribution:

```bash
rm -rf dist/
python setup.py sdist
python setup.py bdist_wheel
```

Upload to [Test PyPI](https://packaging.python.org/en/latest/guides/using-testpypi/) and verify things look right:

```bash
twine upload -r testpypi dist/*
```

Twine will prompt for your username and password.

Upload to [PyPI](https://pypi.org/):

```bash
twine upload dist/*
```

Create a git tag and a github release associated to this distribution.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## History

### 0.1.0

- First version based on the internal API
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019,

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 10 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
include AUTHORS.md
include CONTRIBUTING.md
include HISTORY.md
include LICENSE
include README.md
include requirements.txt

recursive-include pyinuse *
recursive-exclude * __pycache__
recursive-exclude * *.py[co]
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# pyinuse

`pyinuse` is the official Python client for [InUse](https://inuse.eu/) API.

It provides the following main API methods:

- list
- create
- update
- partial_update
- retrieve
- destroy

For main end points such as:

- manufacturers
- machine_models
- machines
- producers
- sites
- lines
- machines
- properties
- multispans
- csvs
- posts
- triggers
- synoptics
- files
- agents
- groups
- favorites
- notifications
- alerts
- sandboxes

Please refer to InUse API documentation (Accessible from the Studio) for more details.

## API versioning

The actual API version is the **internal** API (not versioned) and is subject to changes. A versioned public API will be created in a later stage and used in this python client.

## Example of usage

```python
# Library used to type a password in a secured way.
import getpass
# Client library for the InUse API.
from pyinuse import InUse
# Library for logging
import logging
logging.basicConfig(format="%(asctime)s, %(name)s, %(levelname)s, %(message)s", level=logging.INFO)

manufacturer_code="mycompany" # replace with your manufacturer code

# login
inuse = InUse(base_url=f"https://studio.{manufacturer_code}.productinuse.com")
inuse.login(input("Username? "), getpass.getpass("Password? "))

# get my manufacturer
# each endpoint is accessible by its name as an attribute of InUse instance
manufacturer = inuse.manufacturers.list()[0]

# create a new machine_model
machine_model = inuse.machine_models.update_or_create(
params={"manufacturer": manufacturer["pk"], "code": "my-machine-model"},
data={
"manufacturer": manufacturer["pk"],
"code": "my-machine-model",
"name": "my machine model",
},
)

# logout once you're done
inuse.logout()
```
237 changes: 237 additions & 0 deletions examples/1. Basics - Create organizations & machines.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Create organizations & machines"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The goal of this notebook is to demonstrate how to use InUse api in order to automate creation, modifications & deletions of organizations & machines.\n",
"\n",
"Before going further, here's a quick reminder. Organizations are modeled using a 5 level hierarchy as follow:\n",
"- `Manufacturer`\n",
" - `Producer `\n",
" - `Site`\n",
" - `Line`\n",
" - `Machine`\n",
" \n",
"Please note that a `Machine` can have other machines as children, which make the hierarchy potentially limitless in terms of height. With this mechanism, it's possibe to represent machine, components, subcomponents etc."
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true
},
"source": [
"## Imports"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hidden": true
},
"outputs": [],
"source": [
"# Library used to type a password in a secured way.\n",
"import getpass\n",
"# Client library for the InUse API.\n",
"from pyinuse import InUse\n",
"# Library for logging\n",
"import logging\n",
"logging.basicConfig(format=\"%(asctime)s, %(name)s, %(levelname)s, %(message)s\", level=logging.INFO)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create a client"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First of all, we will create a client and login with it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"inuse = InUse()\n",
"inuse.login(input(\"Username? \"), getpass.getpass(\"Password? \"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Great, you're now authentified!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create a hierarchy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's find the manufacturer. Since the `Manufacturer` is the root object of the organization, listing them will return a list of one item only."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"manufacturer = inuse.manufacturers.list()[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, we will create a machine model named `Notebook machine model`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"machine_model = inuse.machine_models.update_or_create(\n",
" params={\"manufacturer\": manufacturer[\"pk\"], \"code\": \"notebook-machine-model\"},\n",
" data={\n",
" \"manufacturer\": manufacturer[\"pk\"],\n",
" \"code\": \"notebook-machine-model\",\n",
" \"name\": \"Notebook machine model\",\n",
" },\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, we will create a hierarchy composed of only one item per level: one producer, one site, one line & one machine. Obviously, you can create as many as you for each level."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"hierarchy = [\n",
" {\n",
" \"name\": \"Notebook producer\",\n",
" \"code\": \"notebook-producer\",\n",
" \"type\": \"producer\",\n",
" \"children\": [\n",
" {\n",
" \"code\": \"notebook-site-1\",\n",
" \"name\": \"Notebook site 1\",\n",
" \"type\": \"site\",\n",
" \"children\": [\n",
" {\n",
" \"type\": \"line\",\n",
" \"code\": \"notebook-line-1\",\n",
" \"name\": \"Notebook line 1\",\n",
" \"children\": [\n",
" {\n",
" \"type\": \"machine\",\n",
" \"code\": \"notebook-machine-1\",\n",
" \"name\": \"Notebook machine 1\",\n",
" \"machine_model\": machine_model[\"pk\"],\n",
" \"visible_on_share\": True,\n",
" }\n",
" ],\n",
" }\n",
" ],\n",
" }\n",
" ],\n",
" },\n",
"]\n",
"\n",
"\n",
"def run(parent, parent_type, obj):\n",
" if isinstance(obj, list):\n",
" for item in obj:\n",
" run(parent, parent_type, item)\n",
" else:\n",
" typ = obj.pop(\"type\")\n",
" api = getattr(inuse, f\"{typ}s\")\n",
" ret = api.update_or_create(\n",
" params={parent_type: parent[\"pk\"], \"code\": obj[\"code\"]},\n",
" data={**obj, parent_type: parent[\"pk\"]},\n",
" )\n",
" obj.update(ret)\n",
" run(obj, typ, obj.get(\"children\", []))\n",
"\n",
"\n",
"run(manufacturer, \"manufacturer\", hierarchy)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Logout"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is important to logout once it's done."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"inuse.logout()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
1 change: 1 addition & 0 deletions pyinuse/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .pyinuse import InUse # NO QA
Loading

0 comments on commit 56863ce

Please sign in to comment.