Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
API for Dummies
matthew_stacy
Member Posts: 487 PRO
in General
My goal is to setup a configurator similar to this example, but I would like to drive this from the views.py file of a django web application. The intent is to enable a user to configure a simple part, not unlike the "dancing cube" that @Ethan_K demonstrated on youtube, and then download a STEP file.
So far I have installed onshapepy and onshape_client, but have yet to achieve anything productive yet. I would greatly appreciate any advice, suggestions or examples.
1
Comments
I'll also post my code that updates these variables.
One thing to remember about the API is that your request returns a leaf of the data structure. It's best to change the values in the leaf then upload it in its entirety. Don't try and send back one variable of the leaf.
You have to put back what you recieved.
@matthew_stacy I got my code originally from a python program. I converted the steps into nodejs. It doesn't matter what language is used, the API steps are the same. You can pick out the steps fairly easy.
I've highlighted the basic config call with //**** comments.
1. async call to OS and then parse into a object config
server.dwe is the document address
server.po.osPath is the OS server type, they had a prototype server & production server at one time.
2. further parse config into html
3. send html to client and update a tag called "input"
This is as clear as mud, right?
In the send code I:
1. parse the html and extract the variables
2. take the leaf that I sent from server to client and update the json (I'm parsing the leaf on the client and updating it from the html elements)
3. then I send the updated leaf up to my server and it forwards it to OS
4. then I send all the updated images back to the client from my server & OS servers for updating
I'm doing more than just getting a config leaf. I'm parsing the data and creating HTML to display on the client. Then an onchange handler in the client sends the round robin response.
Here's the code in action:
https://rustyshed.com/?quill=5fed5590dd784a11e1364a0e
It's the rest API protocol used by everybody in the cloud. It doesn't matter what language you're using because it's the same steps. Is it worth learning? Yes, it's the future. You're doing good, you're trying. All you need is on the cloud and it's all free.
There's 3 computers involved:
1. the client (in my case, google chrome browser)
2. my server (rustyshed)
3. OS server
It's asynchronous because you just don't know how long things are going to take. It's all JSON which is kinda hard if you don't know javascript.
I built my server from a fresh ubuntu instance. I wouldn't do that today and would spin up a container of ubuntu, nginx & nodejs. In fact I'll probably refactor my site to a newer server install. I'll probably host with google (gcp). When it's up I'll just reroute the DNS server to my new site.
I like nodejs because it's built around server communication. I'm also running sockets which makes the client to my server communication simple. When I open my computer in the morning, socket re-connects with the server. It really is painless and tightly integrated with nodejs. Javascript everywhere is both good and bad. I'm not having to convert data from one format to another on 3 different computers. JSON is native to javascript. The bad is keeping track where the code is running. In the code I shared, I'm parsing the returned config object on my server vs. sending it to the client and letting your computer parse the data. If you go with nodejs, to update this issue, you can cut the code out of the server and paste it into the client and it'll work with minor clean up.
I like nodejs and would recommend it if your starting out.