API Index

Current-Season Workouts - Add New Workout

add a new workout. returns a json representation of the new workout





status codes:

201: created
400: bad request - probably in the input parameters
401: invalid Concept 2 Online logbook user name or password
403: invalid or missing C2 Logbook REST API key
405: method not allowed
503: throttled


sample contents of the response body to a successful request

{"distance": 1947, "seconds": 33, "age": 41, "comments": "just rowing", "month": 10, "hours": 0, "weightClass": "H", "link": {"href": "http://powertwenty.com/c2logapi/1/currentseason/workout/10870021", "rel": "self"}, "typeOfWorkout": "standard", "year": 2009, "id": 10870021, "minutes": 10, "day": 24, "tenths": 0}

Python using httplib2

import httplib2   
import urllib
import simplejson
import base64 

h = httplib2.Http()
newWorkout = {'distance': 1947, 'seconds': 33, 'minutes': 10, 'age': 41, 'comments': 'just rowing', 'month': 10, 'hours': 0, 'weightClass': 'H', 'tenths': 0, 'year': 2009, 'day': 24, 'typeOfWorkout': 'standard'}
authorizationHeader = "Basic %s"%base64.b64encode("%s:%s"%("user", "password"))
response, content = h.request("http://powertwenty.com/c2logapi/1/currentseason/workouts", "POST", urllib.urlencode(newWorkout), headers={"Authorization":authorizationHeader, "X-API-KEY":"xxxxxxxxxxxxxxx" } )
assert response["status"]=="201"
print content 


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLEncoder;

public class AddNewCurrentSeasonWorkout {

     public static void main(String[] args) throws Exception
         addCurrentSeasonWorkout( "user", "password", 2000, 0, 8, 10, 5, 10, 5, 2009, "standard", "H", 40, "from Java");

     public static void addCurrentSeasonWorkout( final String userName, final String password,
             int distance, 
             int hours, int minutes, int seconds, int tenths, 
             int month, int day, int year, 
             String typeOfWorkout, String weightClass, int age, String comments) throws Exception
        //create a URL encoded string of parameters - StringBuffer would be faster
        String parameters = "distance="+URLEncoder.encode(String.valueOf(distance), "UTF-8");
        parameters += "&hours="+URLEncoder.encode(String.valueOf(hours), "UTF-8");
        parameters += "&minutes="+URLEncoder.encode(String.valueOf(minutes), "UTF-8");
        parameters += "&seconds="+URLEncoder.encode(String.valueOf(seconds), "UTF-8");
        parameters += "&tenths="+URLEncoder.encode(String.valueOf(tenths), "UTF-8");
        parameters += "&month="+URLEncoder.encode(String.valueOf(month), "UTF-8");
        parameters += "&day="+URLEncoder.encode(String.valueOf(day), "UTF-8");
        parameters += "&year="+URLEncoder.encode(String.valueOf(year), "UTF-8");
        parameters += "&typeOfWorkout="+URLEncoder.encode(typeOfWorkout, "UTF-8");
        parameters += "&weightClass="+URLEncoder.encode(weightClass, "UTF-8");
        parameters += "&age="+URLEncoder.encode(String.valueOf(age), "UTF-8");
        parameters += "&comments="+URLEncoder.encode(comments, "UTF-8");      

        Authenticator.setDefault(new Authenticator()
            protected PasswordAuthentication getPasswordAuthentication()
                return new PasswordAuthentication(userName, password.toCharArray());

        URL url;
        HttpURLConnection connection = null;

      //Create connection
      url = new URL("http://powertwenty.com/c2logapi/1/currentseason/workouts");
      connection = (HttpURLConnection)url.openConnection();
      connection.setRequestProperty("X-API-KEY", "xxxxxxxxxxxxxxx");
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      connection.setRequestProperty("Content-Length", "" + Integer.toString(parameters.getBytes().length));

      //Send request
      DataOutputStream wr = new DataOutputStream (connection.getOutputStream());
      wr.writeBytes (parameters);
      wr.flush ();
      wr.close ();

      //Get Response
      InputStream is = connection.getInputStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
      String line;
      StringBuffer response = new StringBuffer();
      while((line = rd.readLine()) != null) {
      //response is a JSON object represnting the new workout
      //normally would convert the json to an object here

Copyright © 2006-2015 by Power Twenty, LLC. All Rights Reserved.
Design by Interspire