Swift Google Calendar: Update (Patch) a Calendar Entry

Back to Index

Updates an entry on the user's calendar list.
This example updates the "summary" for the calendar having the ID = "chilkatcloud.com_su2u8trmo6rlq2jh6cr9hb032o@group.calendar.google.com". The new summary will be "Party Calendar".

Documentation: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/patch



func chilkatTest() {
    let rest = CkoRest()
    var success: Bool

    //   Provide a previously obtained OAuth2 access token.
    let oauth2 = CkoOAuth2()
    oauth2.AccessToken = "OAUTH2_ACCESS_TOKEN"
    rest.SetAuthOAuth2(oauth2)

    success = rest.Connect("www.googleapis.com", port: 443, tls: true, autoReconnect: true)
    if success != true {
        print("\(rest.LastErrorText)")
        return
    }

    //  The following code creates the JSON request body.
    //  The JSON created by this code is shown below.
    let jsonReq = CkoJsonObject()
    jsonReq.UpdateString("summary", value: "Party Calendar")

    let sbReq = CkoStringBuilder()
    jsonReq.EmitSb(sbReq)

    rest.AddHeader("Content-Type", value: "application/json")

    let sbJson = CkoStringBuilder()
    success = rest.FullRequestSb("PATCH", uriPath: "/calendar/v3/calendars/chilkatcloud.com_su2u8trmo6rlq2jh6cr9hb032o@group.calendar.google.com", requestBody: sbReq, responseBody: sbJson)
    if success != true {
        print("\(rest.LastErrorText)")
        return
    }

    if rest.ResponseStatusCode.integerValue != 200 {
        print("Received error response code: \(rest.ResponseStatusCode.integerValue)")
        print("Response body:")
        print("\(sbJson.GetAsString())")
        return
    }

    let json = CkoJsonObject()
    json.LoadSb(sbJson)

    //  The following code parses the JSON response.
    //  A sample JSON response is shown below the sample code.
    var kind: String?
    var etag: String?
    var id: String?
    var summary: String?
    var timeZone: String?

    kind = json.StringOf("kind")
    etag = json.StringOf("etag")
    id = json.StringOf("id")
    summary = json.StringOf("summary")
    timeZone = json.StringOf("timeZone")

    print("Example Completed.")

}

Sample JSON Request Body

{  
"summary": "Party Calendar" 
}



  

Sample JSON Response Body

{
  "kind": "calendar#calendar",
  "etag": "\"BKjXnZpXR6E5ueFWG3UJk-2POYg/jcCkAVFAjoCovUbrUQlnks64qfY\"",
  "id": "chilkatcloud.com_su2u8trmo6rlq2jh6cr9hb032o@group.calendar.google.com",
  "summary": "Party Calendar",
  "timeZone": "UTC"
}