3

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.

mrpbennett
  • 1,527
  • 15
  • 40
  • What is your question? – Kenneth K. Apr 12 '19 at 18:01
  • 1
    sorry...how do i install the swagger_client – mrpbennett Apr 12 '19 at 18:13
  • What version of Swagger Codegen do you use? Check `swagger-codegen version`. – Helen Apr 15 '19 at 08:12
  • `swagger-codegen version 07:49:27.261 [main] DEBUG io.swagger.codegen.v3.cli.SwaggerCodegen - there are not options for command 'langs' 07:49:27.263 [main] DEBUG io.swagger.codegen.v3.cli.SwaggerCodegen - there are not options for command 'version' 3.0.5` I believe 3.0.5 – mrpbennett Apr 16 '19 at 14:49

1 Answers1

3

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.

Helen
  • 87,344
  • 17
  • 243
  • 314