Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swagger_client in python trying to use Strava API

I am trying to use the Stava API in a Flask project. I have seen the following stackoverflow

and installed swagger_client

swagger-codegen generate -i https://developers.strava.com/swagger/swagger.json -l python -o ./StravaPythonClient

as per their instructions. However when i run the app i still get import swagger_client ModuleNotFoundError: No module named 'swagger_client'

My code is here

import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: strava_oauth
swagger_client.configuration.access_token = 'fe931c21b503a46b61b1000000000000000000000'

# create an instance of the API class
api_instance = swagger_client.StreamsApi()
id = 2284367626  # Long | The identifier of the activity.
#keys =  # array[String] | Desired stream types.
keyByType = true  # Boolean | Must be true. (default to true)

try:
    # Get Activity Streams
    api_response = api_instance.getActivityStreams(id, keys, keyByType)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling StreamsApi->getActivityStreams: %s\n" % e)

not sure what packages i should be installing to get this working now.

like image 256
mrpbennett Avatar asked Jun 20 '26 01:06

mrpbennett


1 Answers

First install swagger-codegen and check that it's working, this example is for linux. Easier with mac where you can use homebrew.

wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.13/swagger-codegen-cli-2.4.13.jar -O swagger-codegen-cli.jar
java -jar swagger-codegen-cli.jar help

After that go in your project and generate the swagger-client. The code below tells that it's for python and should be stored in a folder within the project called generated

java -jar swagger-codegen-cli.jar generate -i https://developers.strava.com/swagger/swagger.json -l python -o generated

Go into the generated folder and install the requirements

cd generated && python setup.py install --user && cd ..

Change your import statements to refer to the generated folder.

from generated import swagger_client
from generated.swagger_client.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: strava_oauth
swagger_client.Configuration.access_token = 'fe931c21b503a46b61b1000000000000000000000'

# create an instance of the API class
api_instance = swagger_client.StreamsApi()
id = 2284367626  # Long | The identifier of the activity.
#keys =  # array[String] | Desired stream types.
keyByType = true  # Boolean | Must be true. (default to true)

try:
    # Get Activity Streams
    api_response = api_instance.getActivityStreams(id, keys, keyByType)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling StreamsApi->getActivityStreams: %s\n" % e)

Now you can run the file. Ps when you set the access token: configuration needs to be written with upper case C.

like image 194
Odd Eirik Igland Avatar answered Jun 22 '26 14:06

Odd Eirik Igland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!