使用客户端创建公司。
包含此代码示例的文档页面
如需查看上下文中使用的代码示例,请参阅以下文档:
代码示例
Go
import (
"context"
"fmt"
"io"
talent "cloud.google.com/go/talent/apiv4beta1"
talentpb "google.golang.org/genproto/googleapis/cloud/talent/v4beta1"
)
// createCompany creates a company as given.
func createCompany(w io.Writer, projectID, externalID, displayName string) (*talentpb.Company, error) {
ctx := context.Background()
// Initializes a companyService client.
c, err := talent.NewCompanyClient(ctx)
if err != nil {
return nil, fmt.Errorf("talent.NewCompanyClient: %v", err)
}
defer c.Close()
// Construct a createCompany request.
req := &talentpb.CreateCompanyRequest{
Parent: fmt.Sprintf("projects/%s", projectID),
Company: &talentpb.Company{
ExternalId: externalID,
DisplayName: displayName,
},
}
resp, err := c.CreateCompany(ctx, req)
if err != nil {
return nil, fmt.Errorf("CreateCompany: %v", err)
}
fmt.Fprintf(w, "Created company: %q\n", resp.GetName())
return resp, nil
}
Java
/*
* Please include the following imports to run this sample.
*
* import com.google.cloud.talent.v4beta1.Company;
* import com.google.cloud.talent.v4beta1.CompanyServiceClient;
* import com.google.cloud.talent.v4beta1.CreateCompanyRequest;
* import com.google.cloud.talent.v4beta1.TenantName;
* import com.google.cloud.talent.v4beta1.TenantOrProjectName;
*/
public static void sampleCreateCompany() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "Your Google Cloud Project ID";
String tenantId = "Your Tenant ID (using tenancy is optional)";
String displayName = "My Company Name";
String externalId = "Identifier of this company in my system";
sampleCreateCompany(projectId, tenantId, displayName, externalId);
}
/**
* Create Company
*
* @param projectId Your Google Cloud Project ID
* @param tenantId Identifier of the Tenant
*/
public static void sampleCreateCompany(
String projectId, String tenantId, String displayName, String externalId) {
try (CompanyServiceClient companyServiceClient = CompanyServiceClient.create()) {
TenantOrProjectName parent = TenantName.of(projectId, tenantId);
Company company =
Company.newBuilder().setDisplayName(displayName).setExternalId(externalId).build();
CreateCompanyRequest request =
CreateCompanyRequest.newBuilder()
.setParent(parent.toString())
.setCompany(company)
.build();
Company response = companyServiceClient.createCompany(request);
System.out.println("Created Company");
System.out.printf("Name: %s\n", response.getName());
System.out.printf("Display Name: %s\n", response.getDisplayName());
System.out.printf("External ID: %s\n", response.getExternalId());
} catch (Exception exception) {
System.err.println("Failed to create the client due to: " + exception);
}
}
Node.js
const talent = require('@google-cloud/talent').v4;
/**
* Create Company
*
* @param projectId {string} Your Google Cloud Project ID
* @param tenantId {string} Identifier of the Tenant
*/
function sampleCreateCompany(projectId, tenantId, displayName, externalId) {
const client = new talent.CompanyServiceClient();
// const projectId = 'Your Google Cloud Project ID';
// const tenantId = 'Your Tenant ID (using tenancy is optional)';
// const displayName = 'My Company Name';
// const externalId = 'Identifier of this company in my system';
const formattedParent = client.tenantPath(projectId, tenantId);
const company = {
displayName: displayName,
externalId: externalId,
};
const request = {
parent: formattedParent,
company: company,
};
client
.createCompany(request)
.then(responses => {
const response = responses[0];
console.log('Created Company');
console.log(`Name: ${response.name}`);
console.log(`Display Name: ${response.displayName}`);
console.log(`External ID: ${response.externalId}`);
})
.catch(err => {
console.error(err);
});
}
Python
from google.cloud import talent
import six
def create_company(project_id, tenant_id, display_name, external_id):
"""Create Company"""
client = talent.CompanyServiceClient()
# project_id = 'Your Google Cloud Project ID'
# tenant_id = 'Your Tenant ID (using tenancy is optional)'
# display_name = 'My Company Name'
# external_id = 'Identifier of this company in my system'
if isinstance(project_id, six.binary_type):
project_id = project_id.decode("utf-8")
if isinstance(tenant_id, six.binary_type):
tenant_id = tenant_id.decode("utf-8")
if isinstance(display_name, six.binary_type):
display_name = display_name.decode("utf-8")
if isinstance(external_id, six.binary_type):
external_id = external_id.decode("utf-8")
parent = f"projects/{project_id}/tenants/{tenant_id}"
company = {"display_name": display_name, "external_id": external_id}
response = client.create_company(parent=parent, company=company)
print("Created Company")
print("Name: {}".format(response.name))
print("Display Name: {}".format(response.display_name))
print("External ID: {}".format(response.external_id))
return response.name
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。