Chilkat Online Tools

Swift3 / New FreshBooks / Create Single Project

Back to Collection Items

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

    let http = CkoHttp()!
    var success: Bool

    // 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": "Titikaka Hiking Guide",
    //     "client_id": 123,
    //     "project_type": "fixed_price",
    //     "fixed_price": "3000",
    //     "due_date": "2019-04-18",
    //     "services": [
    //       {
    //         "id": 123,
    //         "billable": true,
    //         "vis_state": 0
    //       },
    //       {
    //         "id": 123,
    //         "billable": true,
    //         "vis_state": 0
    //       },
    //       {
    //         "id": 123,
    //         "billable": true,
    //         "vis_state": 0
    //       },
    //       {
    //         "id": 123,
    //         "billable": true,
    //         "vis_state": 0
    //       }
    //     ]
    //   }
    // }

    let json = CkoJsonObject()!
    json.update("project.title", value: "Titikaka Hiking Guide")
    json.updateInt("project.client_id", value: 123)
    json.update("project.project_type", value: "fixed_price")
    json.update("project.fixed_price", value: "3000")
    json.update("project.due_date", value: "2019-04-18")
    json.updateInt("project.services[0].id", value: 123)
    json.updateBool("project.services[0].billable", value: true)
    json.updateInt("project.services[0].vis_state", value: 0)
    json.updateInt("project.services[1].id", value: 123)
    json.updateBool("project.services[1].billable", value: true)
    json.updateInt("project.services[1].vis_state", value: 0)
    json.updateInt("project.services[2].id", value: 123)
    json.updateBool("project.services[2].billable", value: true)
    json.updateInt("project.services[2].vis_state", value: 0)
    json.updateInt("project.services[3].id", value: 123)
    json.updateBool("project.services[3].billable", value: true)
    json.updateInt("project.services[3].vis_state", value: 0)

    // Adds the "Authorization: Bearer <access_token>" header.
    http.authToken = "<access_token>"
    http.setRequestHeader("Content-Type", value: "application/json")

    var resp: CkoHttpResponse? = http.postJson3("https://api.freshbooks.com/projects/business/{{businessId}}/projects", contentType: "application/json", json: json)
    if http.lastMethodSuccess == false {
        print("\(http.lastErrorText!)")
        return
    }

    let sbResponseBody = CkoStringBuilder()!
    resp!.getBodySb(sbResponseBody)

    let jResp = CkoJsonObject()!
    jResp.loadSb(sbResponseBody)
    jResp.emitCompact = false

    print("Response Body:")
    print("\(jResp.emit()!)")

    var respStatusCode: Int = resp!.statusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp!.header!)")
        print("Failed.")
        resp = nil
        return
    }

    resp = nil

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

    // {
    //   "project": {
    //     "id": 3018266,
    //     "title": "Titikaka Hiking Guide",
    //     "description": null,
    //     "due_date": "2019-04-18",
    //     "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:06:55",
    //     "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@freshbooks.com",
    //           "company": "Postman Sandbox",
    //           "active": true
    //         }
    //       ],
    //       "pending_invitations": null
    //     }
    //   }
    // }

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

    var id: Int
    var identity_id: Int
    var role: String?
    var first_name: String?
    var last_name: String?
    var email: String?
    var company: String?
    var active: Bool

    var Id: Int = jResp.int(of: "project.id").intValue
    var Title: String? = jResp.string(of: "project.title")
    var Description: String? = jResp.string(of: "project.description")
    var Due_date: String? = jResp.string(of: "project.due_date")
    var Client_id: Int = jResp.int(of: "project.client_id").intValue
    var Internal: Bool = jResp.bool(of: "project.internal")
    var Budget: String? = jResp.string(of: "project.budget")
    var Fixed_price: String? = jResp.string(of: "project.fixed_price")
    var Rate: String? = jResp.string(of: "project.rate")
    var Billing_method: String? = jResp.string(of: "project.billing_method")
    var Project_type: String? = jResp.string(of: "project.project_type")
    var Active: Bool = jResp.bool(of: "project.active")
    var Complete: Bool = jResp.bool(of: "project.complete")
    var Sample: Bool = jResp.bool(of: "project.sample")
    var Created_at: String? = jResp.string(of: "project.created_at")
    var Updated_at: String? = jResp.string(of: "project.updated_at")
    var Logged_duration: String? = jResp.string(of: "project.logged_duration")
    var Billed_amount: String? = jResp.string(of: "project.billed_amount")
    var Billed_status: String? = jResp.string(of: "project.billed_status")
    var Retainer_id: String? = jResp.string(of: "project.retainer_id")
    var GroupId: Int = jResp.int(of: "project.group.id").intValue
    var Pending_invitations: String? = jResp.string(of: "project.group.pending_invitations")
    var i: Int = 0
    var count_i: Int = jResp.size(ofArray: "project.services").intValue
    while i < count_i {
        jResp.i = i
        i = i + 1
    }

    i = 0
    count_i = jResp.size(ofArray: "project.group.members").intValue
    while i < count_i {
        jResp.i = i
        id = jResp.int(of: "project.group.members[i].id").intValue
        identity_id = jResp.int(of: "project.group.members[i].identity_id").intValue
        role = jResp.string(of: "project.group.members[i].role")
        first_name = jResp.string(of: "project.group.members[i].first_name")
        last_name = jResp.string(of: "project.group.members[i].last_name")
        email = jResp.string(of: "project.group.members[i].email")
        company = jResp.string(of: "project.group.members[i].company")
        active = jResp.bool(of: "project.group.members[i].active")
        i = i + 1
    }


}

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
  "project": {
    "title": "Titikaka Hiking Guide",
    "client_id": {{customerId}},
    "project_type": "fixed_price",
    "fixed_price": "3000",
    "due_date": "2019-04-18",
    "services": 
    [
    	{"id": {{serviceid}}, "billable": true, "vis_state": 0}, {"id": {{serviceid}},"billable": true, "vis_state": 0}, {"id": {{serviceid}}, "billable": true, "vis_state": 0}, {"id": {{serviceid}}, "billable": true, "vis_state": 0}
    	]
  }
}'
https://api.freshbooks.com/projects/business/{{businessId}}/projects

Postman Collection Item JSON

{
  "name": "Create Single Project",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "let jsonData = JSON.parse(responseBody);",
          "pm.environment.set(\"projectId\", jsonData.project.id);",
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "type": "text",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"project\": {\n    \"title\": \"Titikaka Hiking Guide\",\n    \"client_id\": {{customerId}},\n    \"project_type\": \"fixed_price\",\n    \"fixed_price\": \"3000\",\n    \"due_date\": \"2019-04-18\",\n    \"services\": \n    [\n    \t{\"id\": {{serviceid}}, \"billable\": true, \"vis_state\": 0}, {\"id\": {{serviceid}},\"billable\": true, \"vis_state\": 0}, {\"id\": {{serviceid}}, \"billable\": true, \"vis_state\": 0}, {\"id\": {{serviceid}}, \"billable\": true, \"vis_state\": 0}\n    \t]\n  }\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/projects/business/{{businessId}}/projects",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "projects",
        "business",
        "{{businessId}}",
        "projects"
      ]
    }
  },
  "response": [
    {
      "name": "Create Single Project",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "type": "text",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"project\": {\n    \"title\": \"Titikaka Hiking Guide\",\n    \"client_id\": {{customerId}},\n    \"project_type\": \"fixed_price\",\n    \"fixed_price\": \"3000\",\n    \"due_date\": \"2019-04-18\"\n  }\n}"
        },
        "url": {
          "raw": "https://api.freshbooks.com/projects/business/{{businessId}}/projects",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "projects",
            "business",
            "{{businessId}}",
            "projects"
          ]
        }
      },
      "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": "PxQBWV5TCBABVllUBAQOU10TGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4FDFZFVRYSBxlDUhULRERQBxVNEkINUgdbTEccQQBKDkQTUAdeRkUODAZbR0NGTVIbARlUVAcEB1VTVVoBUQhWAQsFBB9UDlQdQ1dSVVBSBFFRD1dVVldVAAIVSgJQWkAHOw=="
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "20"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "8"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1557346076"
        },
        {
          "key": "Retry-After",
          "value": "60"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 08 May 2018 20:06:55 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "759"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 08 May 2019 20:06:55 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\": \"Titikaka Hiking Guide\",\n        \"description\": null,\n        \"due_date\": \"2019-04-18\",\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:06:55\",\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@freshbooks.com\",\n                    \"company\": \"Postman Sandbox\",\n                    \"active\": true\n                }\n            ],\n            \"pending_invitations\": null\n        }\n    }\n}"
    }
  ]
}