주제 관리

주제 리소스는 할당된 주 또는 과정 과목과 같은 유사성에 따라 분류된 스트림 항목 그룹을 나타냅니다.

각 주제는 서버에서 할당한 고유 ID로 식별됩니다. 이 ID에는 주제가 속한 과정 ID, 클래스룸 UI에 표시되는 실제 주제 이름, 마지막 업데이트 날짜 및 시간이 연결됩니다.

주제 만들기

다음 샘플과 같이 topics.create() 메서드를 사용하여 과정에서 새 주제를 만들 수 있습니다.

Java

classroom/snippets/src/main/java/CreateTopic.java
Topic topic = null;
try {
  // Create the new Topic.
  Topic content = new Topic().setName("Semester 1");
  topic = service.courses().topics().create(courseId, content).execute();
  System.out.println("Topic id: " + topic.getTopicId() + "\n" + "Course id: " + courseId);
} catch (GoogleJsonResponseException e) {
  // TODO (developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The courseId does not exist: %s.\n", courseId);
  } else {
    throw e;
  }
} catch (Exception e) {
  throw e;
}
return topic;

Python

topic = {
    "name": 'Example Topic'
}
response = service.courses().topics().create(
  courseId=<course ID or alias>,
  body=topic).execute()
print('Topic created: ', response['name'])

주제 세부정보 검색

다음 샘플과 같이 topics.get() 메서드를 사용하여 해당 과정의 주제를 검색할 수 있습니다.

Java

classroom/snippets/src/main/java/GetTopic.java
Topic topic = null;
try {
  // Get the topic.
  topic = service.courses().topics().get(courseId, topicId).execute();
  System.out.printf("Topic '%s' found.\n", topic.getName());
} catch (GoogleJsonResponseException e) {
  // TODO (developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The courseId or topicId does not exist: %s, %s.\n", courseId, topicId);
  }
  throw e;
} catch (Exception e) {
  throw e;
}
return topic;

Python

response = service.courses().topics().get(
  courseId=<course ID or alias>,
  id=<topic ID>).execute()
print('{0} ({1})'.format(response['name'], response['topicId']))

과정 목록을 보려면 다음 샘플과 같이 topics.list() 메서드를 사용합니다.

Java

classroom/snippets/src/main/java/ListTopics.java
List<Topic> topics = new ArrayList<>();
String pageToken = null;

try {
  do {
    ListTopicResponse response =
        service
            .courses()
            .topics()
            .list(courseId)
            .setPageSize(100)
            .setPageToken(pageToken)
            .execute();

    /* Ensure that the response is not null before retrieving data from it to avoid errors. */
    if (response.getTopic() != null) {
      topics.addAll(response.getTopic());
      pageToken = response.getNextPageToken();
    }
  } while (pageToken != null);

  if (topics.isEmpty()) {
    System.out.println("No topics found.");
  } else {
    for (Topic topic : topics) {
      System.out.printf("%s (%s)\n", topic.getName(), topic.getTopicId());
    }
  }
} catch (GoogleJsonResponseException e) {
  // TODO (developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The courseId does not exist: %s.\n", courseId);
  } else {
    throw e;
  }
} catch (Exception e) {
  throw e;
}
return topics;

Python

topics = []
page_token = None
while True:
    response = service.courses().topics().list(
        pageToken=page_token,
        pageSize=30,
        courseId=<course ID or alias>).execute()
    topics.extend(response.get('topic', []))
    page_token = response.get('nextPageToken', None)
    if not page_token:
        break
if not topics:
    print('No topics found.')
else:
    print('Topics:')
    for topic in topics:
        print('{0} ({1})'.format(topic['name'], topic['topicId']))

주제 업데이트

다음 샘플과 같이 topics.patch() 메서드를 사용하여 기존 주제 이름을 업데이트할 수 있습니다.

Java

classroom/snippets/src/main/java/UpdateTopic.java
Topic topic = null;
try {
  // Retrieve the topic to update.
  Topic topicToUpdate = service.courses().topics().get(courseId, topicId).execute();

  // Update the name field for the topic retrieved.
  topicToUpdate.setName("Semester 2");

  /* Call the patch endpoint and set the updateMask query parameter to the field that needs to
  be updated. */
  topic =
      service
          .courses()
          .topics()
          .patch(courseId, topicId, topicToUpdate)
          .set("updateMask", "name")
          .execute();

  /* Prints the updated topic. */
  System.out.printf("Topic '%s' updated.\n", topic.getName());
} catch (GoogleJsonResponseException e) {
  // TODO(developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The courseId or topicId does not exist: %s, %s.\n", courseId, topicId);
  } else {
    throw e;
  }
} catch (Exception e) {
  throw e;
}
return topic;

Python

topic = {
  "name": "New Topic Name"
}
response = service.courses().topics().patch(
  courseId=<course ID or alias>,
  id=<topic ID>,
  updateMask="name",
  body=topic).execute()
print('{0} ({1})'.format(response['name'], response['topicId']))

주제 삭제

다음 샘플과 같이 topics.delete() 메서드를 사용하여 기존 주제를 삭제할 수 있습니다.

Java

classroom/snippets/src/main/java/DeleteTopic.java
try {
  service.courses().topics().delete(courseId, topicId).execute();
} catch (GoogleJsonResponseException e) {
  // TODO(developer) - handle error appropriately
  throw e;
} catch (Exception e) {
  throw e;
}