mpclarkson / StravaSwift

A Swift wrapper for the Strava API v3
MIT License
102 stars 45 forks source link

Syntax for Upload .fit / .tcx file to strava #33

Closed mjunkmyjunk closed 3 years ago

mjunkmyjunk commented 3 years ago

It seems like @DominicHolmes has added the code to enable upload functionality to StravaSwift.

It would be helpful to also include an example on how this upload can be achieved. I tried w/ my rudimentary understanding and am not able to figure out how to get it to work.

what is the correct syntax? Router.uploadFIle?

Appreciate if someone can help.

note: Latest TCX is basically this --> file:///Users/testUser/Library/Developer/CoreSimulator/Devices/B4759D88-DFB6-43C0-A6E9-84FE3F23B9A0/data/Containers/Data/Application/BB9DAF96-51C5-4F16-BC42-C3EC1EBAA5F1/Documents/20201010-test.tcx

// StravaClient.sharedInstance.upload(Router.XXXX , upload: UploadData.init(activityType: "ride", name: "uploadName", description: "uploadDesc", private: true, trainer: true, externalId: "12345", dataType: "tcx", file: latestTCX) // ,result: {UploadData.Status} // ,failure: { (error: NSError) in // debugPrint(error) // })

DominicHolmes commented 3 years ago

Hey @mjunkmyjunk , hopefully this helps. This is the upload function from my own implementation. There are a few types here (GPX, ActivityPayload) that are custom, but essentially I'm just using Codable (swift framework) to encode an Activity into XML, and then uploading the file.

    func upload() {
        let activityPayload = ActivityPayload(date: Date(), name: "test run pls ignore", description: "a description", stream: data.dirtyStream)
        if let gpx = GPX(from: activityPayload) {
            let encoder = XMLEncoder()
            encoder.dateEncodingStrategy = .iso8601
            if let gpxData = try? encoder.encode(gpx, header: XMLHeader(version: 1.0, encoding: "UTF-8")) {
                let uploadData = UploadData(name: activityPayload.name ?? "run", dataType: .gpx, file: gpxData)
                StravaClient.sharedInstance.upload(.uploadFile(upload: uploadData), upload: uploadData, result: { ( status: Activity?) in
                    print("Uploaded...")
                }) { (error) in
                    print(error.localizedDescription)
                }
            }
        }
    }

Looks like the line you're looking for is StravaClient.sharedInstance.upload(.uploadFile(upload: uploadData), upload: uploadData, result: { ( status: Activity?) in .... }.

Haven't worked on this in a while and it looks like we're using different file types, but this should get you started. Make sure to close this issue when you fix this.

mjunkmyjunk commented 3 years ago

hey @DominicHolmes Many Thanks for the pointers. I will take a look and do some experimentation on how it would work for my case. Yes, I'm using TCX files and basically, I"m passing the entire file.

Looks like the uploadData is provided twice (This was one of my confusion. Why it's needed to be sent 2x)

and Yeah, I'll close this when I manage to figure it out.