Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape, CAD, maker project and design.

First time visiting? Here are some places to start:

  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. 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.

Update feature using API and Python

anna_bengtsson419anna_bengtsson419 Member Posts: 3
edited October 11 in Data management

Hi!

I’m new to use Onshape REST API, or API overall. I’m trying to modify a feature (a feature I’ve created with Feature Script) by calling the update feature API with python. But I don’t understand what my functions input/ouput should be, how do I understand that from the documentation? My feature have different parameters I want to change, see picture attached. This is my function in python so far:

def update_feature(self, did, wid ??):

return self.__api.request('post', '/api/partstudios/d/' + did + '/w/' + wid + '/e/' + eid + '/features' + fid ??)

I’ve downloaded Onshapepy so I’m just adding the function to the client module.

Thank you in advance!


Tagged:

Comments

  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,094
    @anna_bengtsson419

    @ethan_keller924 will be able to help! I've sent him a message for you.
    Jake Rosenfeld - Modeling Team
  • ethan_keller924ethan_keller924 Member Posts: 34 ✭✭
    Hi @anna_bengtsson419,

    The best way to do what you're asking for is to build the feature you want to add in the UI, and then use "get feature list" to see the structure of your built feature. Then, if you can call "add feature" using that structure in the API. This works for even custom features. Try that out, and let me know if you run into issues!

  • anna_bengtsson419anna_bengtsson419 Member Posts: 3
    @ethan_keller924

    Thank you for your answer, i appreciate it! I think I understand that from the developer portal and I've been able to get the feature list from my part studio with your Onshapepy.

    My problem is that I don't understand how to write my own function and how to define it when I call it. I can read about the structure in the developer portal, but I don't really understand it. As I said, new to API and this kind of programming. 

    This i what I have so far. 

    Function: 

    def update_feature(self, did, wid, eid):
            return self.__api.request('post', '/api/partstudios/d/' + did + '/w/' + wid + '/e/' + eid + '/features')

    And when i call it:

    from onshapepy.core.client import c
    update_feature = c.update_feature(did, wid, eid)

    I understand I've missed some, but I do not really understand what. Or where I can learn. 

    Thank you in advance!

  • ethan_keller924ethan_keller924 Member Posts: 34 ✭✭
    Hi @anna_bengtsson419,

    The update feature endpoint is POST request that takes a body. Please look in the API explorer to experiment and see how to format it correctly. Right now you're not passing in a body to the rest call. 

    Hope that helps!
    -Ethan
  • billy2billy2 Member, OS Professional, Mentor, Developers Posts: 1,084 PRO
    edited October 29
    @anna_bengtsson419 & @ethan_keller924

    I do what ethan does. It's important to replace the entire leaf vs. one small part of the leaf. It may seem like more data traffic, but in the end it's easier to manage. Download it, modify it and then put it back (or parts of it).

    I usually don't send the whole object back and tend to test and pick a leaf that's 1 or 2 levels from the top. I don't think this is considered good practice though.

    I use api explorer a lot to test what works and what won't. When my process is working, I hard code it. But, I always start in the api explorer.


  • billy2billy2 Member, OS Professional, Mentor, Developers Posts: 1,084 PRO
    edited November 3
    @anna_bengtsson419 I'm not using python, but here's a post showing how to use the api explorer.

    So I want to run 'update configuration' from api explorer:


    Click on 'try it' and below will show up:


    The  'update configuration'  is a post and has a json request body that needs the correct information. What's shown is the format and if you click on the 'make api call' button you'll get a 404 error because this isn't the correct data. So, how do you get the right data? Go get it.

    I run the get configuration api call and print it out on my webpage:


    I copy this response into my copy buffer using command-c and paste it into the request body:




    With the proper request body, you'll get a status 200 and you're talking to the server. The server likes you now.

    You can use the api to get and copy the response, then try update and paste in the response. Me, I print the object out on my web page while developing because I'm constantly pick stuff out of it. Before deploying, shut it off.

    You can now change values in the request body and see what comes back. You just bang on the server until you get it to give you what you want. In my case I have to change 'value' and 'expression'. Why? this stuff isn't documented and I doubt it ever will be. You have to test until it works. I've never broken a server and you won't get a status 200 with something that's wrong.

    The get response object returns both "configurationParameters" & "currentConfiguration" and I wanted to omitted the "configurationParameters" in the update configuration request body. This "configurationParameters" leaf isn't really required for the api call update configuration. It stopped working. So I tried to omit it and save on transmission overhead but couldn't.

    Send what you get. Bang on it and make the server work for you. Once you figure it out, hard code it, in your case, in python.



Sign In or Register to comment.