Chilkat Online Tools

Objective-C / New FreshBooks / Update Project

Back to Collection Items

#import <CkoHttp.h>
#import <CkoJsonObject.h>
#import <CkoStringBuilder.h>
#import <CkoHttpResponse.h>
#import <NSString.h>

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

CkoHttp *http = [[CkoHttp alloc] init];
BOOL success;

// Use this online tool to generate code from sample JSON: Generate Code to Create JSON

// The following JSON is sent in the request body.

// {
//   "project": {
//     "title": "K2 Hiking Guide",
//     "due_date": "2019-04-20",
//     "client_id": 123
//   }
// }

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"project.title" value: @"K2 Hiking Guide"];
[json UpdateString: @"project.due_date" value: @"2019-04-20"];
[json UpdateInt: @"project.client_id" value: [NSNumber numberWithInt: 123]];

// Adds the "Authorization: Bearer <access_token>" header.
http.AuthToken = @"<access_token>";
[http SetRequestHeader: @"Content-Type" value: @"application/json"];

CkoStringBuilder *sbRequestBody = [[CkoStringBuilder alloc] init];
[json EmitSb: sbRequestBody];

CkoHttpResponse *resp = [http PTextSb: @"PUT" url: @"https://api.freshbooks.com/projects/business/{{businessId}}/projects/{{projectId}}" textData: sbRequestBody charset: @"utf-8" contentType: @"application/json" md5: NO gzip: NO];
if (http.LastMethodSuccess == NO) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
[resp GetBodySb: sbResponseBody];

CkoJsonObject *jResp = [[CkoJsonObject alloc] init];
[jResp LoadSb: sbResponseBody];
jResp.EmitCompact = NO;

NSLog(@"%@",@"Response Body:");
NSLog(@"%@",[jResp Emit]);

int respStatusCode = [resp.StatusCode intValue];
NSLog(@"%@%d",@"Response Status Code = ",respStatusCode);
if (respStatusCode >= 400) {
    NSLog(@"%@",@"Response Header:");
    NSLog(@"%@",resp.Header);
    NSLog(@"%@",@"Failed.");

    return;
}

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "project": {
//     "id": 3018266,
//     "title": "K2 Hiking Guide",
//     "description": null,
//     "due_date": "2019-04-20",
//     "client_id": 31006,
//     "internal": false,
//     "budget": null,
//     "fixed_price": "3000.00",
//     "rate": null,
//     "billing_method": null,
//     "project_type": "fixed_price",
//     "active": true,
//     "complete": false,
//     "sample": false,
//     "created_at": "2019-05-08T20:06:55",
//     "updated_at": "2019-05-08T20:07:37",
//     "logged_duration": null,
//     "services": [
//     ],
//     "billed_amount": "0.00",
//     "billed_status": "unbilled",
//     "retainer_id": null,
//     "group": {
//       "id": 5049390,
//       "members": [
//         {
//           "id": 6578346,
//           "identity_id": 37256,
//           "role": "owner",
//           "first_name": "Marshall",
//           "last_name": "Johnston",
//           "email": "api.freshbooks@gmail.com",
//           "company": "Postman Sandbox",
//           "active": true
//         }
//       ],
//       "pending_invitations": [
//       ]
//     }
//   },
//   "abilities": [
//     {
//       "name": "can_edit",
//       "value": true
//     },
//     {
//       "name": "can_archive",
//       "value": true
//     },
//     {
//       "name": "can_generate_invoice",
//       "value": true
//     },
//     {
//       "name": "can_delete",
//       "value": true
//     },
//     {
//       "name": "can_invite_team_member",
//       "value": true
//     },
//     {
//       "name": "can_manage_services",
//       "value": true
//     },
//     {
//       "name": "can_edit_hourly_budget",
//       "value": true
//     },
//     {
//       "name": "can_view_fixed_price",
//       "value": true
//     },
//     {
//       "name": "can_edit_fixed_price",
//       "value": true
//     },
//     {
//       "name": "can_edit_end_date",
//       "value": true
//     },
//     {
//       "name": "can_edit_flat_rate",
//       "value": true
//     },
//     {
//       "name": "can_edit_project_type",
//       "value": true
//     },
//     {
//       "name": "can_edit_title",
//       "value": true
//     },
//     {
//       "name": "can_view_review_tab",
//       "value": true
//     },
//     {
//       "name": "can_view_discuss_tab",
//       "value": true
//     },
//     {
//       "name": "can_track_time",
//       "value": true
//     },
//     {
//       "name": "can_create_comment",
//       "value": true
//     }
//   ]
// }

// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

int id;
int identity_id;
NSString *role = 0;
NSString *first_name = 0;
NSString *last_name = 0;
NSString *email = 0;
NSString *company = 0;
BOOL active;
NSString *name = 0;
BOOL value;

int Id = [[jResp IntOf: @"project.id"] intValue];
NSString *Title = [jResp StringOf: @"project.title"];
NSString *Description = [jResp StringOf: @"project.description"];
NSString *Due_date = [jResp StringOf: @"project.due_date"];
int Client_id = [[jResp IntOf: @"project.client_id"] intValue];
BOOL Internal = [jResp BoolOf: @"project.internal"];
NSString *Budget = [jResp StringOf: @"project.budget"];
NSString *Fixed_price = [jResp StringOf: @"project.fixed_price"];
NSString *Rate = [jResp StringOf: @"project.rate"];
NSString *Billing_method = [jResp StringOf: @"project.billing_method"];
NSString *Project_type = [jResp StringOf: @"project.project_type"];
BOOL Active = [jResp BoolOf: @"project.active"];
BOOL Complete = [jResp BoolOf: @"project.complete"];
BOOL Sample = [jResp BoolOf: @"project.sample"];
NSString *Created_at = [jResp StringOf: @"project.created_at"];
NSString *Updated_at = [jResp StringOf: @"project.updated_at"];
NSString *Logged_duration = [jResp StringOf: @"project.logged_duration"];
NSString *Billed_amount = [jResp StringOf: @"project.billed_amount"];
NSString *Billed_status = [jResp StringOf: @"project.billed_status"];
NSString *Retainer_id = [jResp StringOf: @"project.retainer_id"];
int GroupId = [[jResp IntOf: @"project.group.id"] intValue];
int i = 0;
int count_i = [[jResp SizeOfArray: @"project.services"] intValue];
while (i < count_i) {
    jResp.I = [NSNumber numberWithInt: i];
    i = i + 1;
}

i = 0;
count_i = [[jResp SizeOfArray: @"project.group.members"] intValue];
while (i < count_i) {
    jResp.I = [NSNumber numberWithInt: i];
    id = [[jResp IntOf: @"project.group.members[i].id"] intValue];
    identity_id = [[jResp IntOf: @"project.group.members[i].identity_id"] intValue];
    role = [jResp StringOf: @"project.group.members[i].role"];
    first_name = [jResp StringOf: @"project.group.members[i].first_name"];
    last_name = [jResp StringOf: @"project.group.members[i].last_name"];
    email = [jResp StringOf: @"project.group.members[i].email"];
    company = [jResp StringOf: @"project.group.members[i].company"];
    active = [jResp BoolOf: @"project.group.members[i].active"];
    i = i + 1;
}

i = 0;
count_i = [[jResp SizeOfArray: @"project.group.pending_invitations"] intValue];
while (i < count_i) {
    jResp.I = [NSNumber numberWithInt: i];
    i = i + 1;
}

i = 0;
count_i = [[jResp SizeOfArray: @"abilities"] intValue];
while (i < count_i) {
    jResp.I = [NSNumber numberWithInt: i];
    name = [jResp StringOf: @"abilities[i].name"];
    value = [jResp BoolOf: @"abilities[i].value"];
    i = i + 1;
}

Curl Command

curl -X PUT
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
  "project": {
    "title": "K2 Hiking Guide",
    "due_date": "2019-04-20",
    "client_id": {{customerId}}
  }
}'
https://api.freshbooks.com/projects/business/{{businessId}}/projects/{{projectId}}

Postman Collection Item JSON

{
  "name": "Update Project",
  "request": {
    "method": "PUT",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"project\": {\n    \"title\": \"K2 Hiking Guide\",\n    \"due_date\": \"2019-04-20\",\n    \"client_id\": {{customerId}}\n  }\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/projects/business/{{businessId}}/projects/{{projectId}}",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "projects",
        "business",
        "{{businessId}}",
        "projects",
        "{{projectId}}"
      ]
    },
    "description": "Update project with project title. \n\n*`client_id` in this case is `customerId`*"
  },
  "response": [
    {
      "name": "Update Project",
      "originalRequest": {
        "method": "PUT",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"project\": {\n    \"title\": \"K2 Hiking Guide\",\n    \"due_date\": \"2019-04-20\",\n    \"client_id\": {{customerId}}\n  }\n}"
        },
        "url": {
          "raw": "https://api.freshbooks.com/projects/business/{{businessId}}/projects/{{projectId}}",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "projects",
            "business",
            "{{businessId}}",
            "projects",
            "{{projectId}}"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/vnd.api+json"
        },
        {
          "key": "X-Version",
          "value": "1905.7.0"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVllUBAQOU10TGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4FDFZFVRYSBxlDUhULRERQBxVNEkINUgdbTBpAVhZWFEMCVlgURFgLAwBMERxUTVIZAwRfVQYGBFpfVFMIVApVAQsYAwNSFUMEUlVSVFNUVF5aWgcEUwAAFR1RBwhCU24="
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "20"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "9"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1557346118"
        },
        {
          "key": "Retry-After",
          "value": "60"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 08 May 2018 20:07:37 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "1552"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 08 May 2019 20:07:37 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17361-MDW"
        },
        {
          "key": "X-Cache",
          "value": "MISS"
        },
        {
          "key": "X-Cache-Hits",
          "value": "0"
        },
        {
          "key": "Country",
          "value": "CA"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains; preload"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"project\": {\n        \"id\": 3018266,\n        \"title\": \"K2 Hiking Guide\",\n        \"description\": null,\n        \"due_date\": \"2019-04-20\",\n        \"client_id\": 31006,\n        \"internal\": false,\n        \"budget\": null,\n        \"fixed_price\": \"3000.00\",\n        \"rate\": null,\n        \"billing_method\": null,\n        \"project_type\": \"fixed_price\",\n        \"active\": true,\n        \"complete\": false,\n        \"sample\": false,\n        \"created_at\": \"2019-05-08T20:06:55\",\n        \"updated_at\": \"2019-05-08T20:07:37\",\n        \"logged_duration\": null,\n        \"services\": [],\n        \"billed_amount\": \"0.00\",\n        \"billed_status\": \"unbilled\",\n        \"retainer_id\": null,\n        \"group\": {\n            \"id\": 5049390,\n            \"members\": [\n                {\n                    \"id\": 6578346,\n                    \"identity_id\": 37256,\n                    \"role\": \"owner\",\n                    \"first_name\": \"Marshall\",\n                    \"last_name\": \"Johnston\",\n                    \"email\": \"api.freshbooks@gmail.com\",\n                    \"company\": \"Postman Sandbox\",\n                    \"active\": true\n                }\n            ],\n            \"pending_invitations\": []\n        }\n    },\n    \"abilities\": [\n        {\n            \"name\": \"can_edit\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_archive\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_generate_invoice\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_delete\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_invite_team_member\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_manage_services\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_edit_hourly_budget\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_view_fixed_price\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_edit_fixed_price\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_edit_end_date\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_edit_flat_rate\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_edit_project_type\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_edit_title\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_view_review_tab\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_view_discuss_tab\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_track_time\",\n            \"value\": true\n        },\n        {\n            \"name\": \"can_create_comment\",\n            \"value\": true\n        }\n    ]\n}"
    }
  ]
}