Sample code for 30+ languages & platforms
Delphi DLL

Google Cloud SQL - Start Database Instance

See more Google Cloud SQL Examples

Demonstrates how to start a Google Cloud SQL database instance.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, StringBuilder, HttpResponse, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
sbToken: HCkStringBuilder;
http: HCkHttp;
json: HCkJsonObject;
sbRequestBody: HCkStringBuilder;
resp: HCkHttpResponse;
sbResponseBody: HCkStringBuilder;
jResp: HCkJsonObject;
respStatusCode: Integer;
kind: PWideChar;
targetLink: PWideChar;
status: PWideChar;
user: PWideChar;
insertTime: PWideChar;
operationType: PWideChar;
name: PWideChar;
targetId: PWideChar;
selfLink: PWideChar;
targetProject: PWideChar;

begin
success := False;

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

// In this example, Get Google Cloud SQL OAuth2 Access Token, 
// the service account access token was saved to a text file.  This example fetches the access token from the file..
sbToken := CkStringBuilder_Create();
CkStringBuilder_LoadFile(sbToken,'qa_data/tokens/google_cloud_sql_access_token.txt','utf-8');

http := CkHttp_Create();

// Implements the following CURL command:

// curl -X PATCH \
// -H "Authorization: Bearer "$(gcloud auth print-access-token) \
// -H "Content-Type: application/json; charset=utf-8" \
// -d '{
//   "settings": {
//     "activationPolicy": "ALWAYS" 
//   }
// }' \
// https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id

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

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

// {
//   "settings": {
//     "activationPolicy": "ALWAYS"
//   }
// }

// Use "ALWAYS" to start an instance.  Use "NEVER" to stop an instance.
json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'settings.activationPolicy','ALWAYS');

// Causes the "Authorization: Bearer "$(gcloud auth print-access-token)" header to be added.
CkHttp_putAuthToken(http,CkStringBuilder__getAsString(sbToken));

CkHttp_SetRequestHeader(http,'Content-Type','application/json; charset=utf-8');

sbRequestBody := CkStringBuilder_Create();
CkJsonObject_EmitSb(json,sbRequestBody);

// Replace "project-id" with your actual Google project ID.
// Replace "instance-id" with your database instance ID, which is the name of your database.  (For example, when I created my test database I named it "chilkat", and therefore my instance-id is "chilkat".)
resp := CkHttpResponse_Create();
success := CkHttp_HttpSb(http,'PATCH','https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id',sbRequestBody,'utf-8','application/json',resp);
if (success = False) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

sbResponseBody := CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);
jResp := CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,False);

Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkJsonObject__emit(jResp));

respStatusCode := CkHttpResponse_getStatusCode(resp);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode = 401) then
  begin
    Memo1.Lines.Add('It may be that your access token expired.');
    Memo1.Lines.Add('Try refreshing the access token by re-fetching it.');
  end;
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(CkHttpResponse__header(resp));
    Memo1.Lines.Add('Failed.');
    Exit;
  end;

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

// {
//   "kind": "sql#operation",
//   "targetLink": "https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id",
//   "status": "PENDING",
//   "user": "user@example.com",
//   "insertTime": "2020-01-20T21:30:35.667Z",
//   "operationType": "UPDATE",
//   "name": "operation-id",
//   "targetId": "instance-id",
//   "selfLink": "https://www.googleapis.com/sql/v1beta4/projects/project-id/operations/operation-id",
//   "targetProject": "project-id"
// }

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

kind := CkJsonObject__stringOf(jResp,'kind');
targetLink := CkJsonObject__stringOf(jResp,'targetLink');
status := CkJsonObject__stringOf(jResp,'status');
user := CkJsonObject__stringOf(jResp,'user');
insertTime := CkJsonObject__stringOf(jResp,'insertTime');
operationType := CkJsonObject__stringOf(jResp,'operationType');
name := CkJsonObject__stringOf(jResp,'name');
targetId := CkJsonObject__stringOf(jResp,'targetId');
selfLink := CkJsonObject__stringOf(jResp,'selfLink');
targetProject := CkJsonObject__stringOf(jResp,'targetProject');

CkStringBuilder_Dispose(sbToken);
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbRequestBody);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);

end;