Example Rest API client based using JavaThe purpose of this document to show Rest client Example written in JAVA to be able to inject Work Item data into codebeamer via Rest API.
Minimum requirement: Java SE framework version 1.8
This application was implemented and tested under Windows 10 but is compatible with Linux.
Usagejava -jar importWorkItemsDemo.jar -e [Endpoint] -u [Username] -p [Password] -t [TrackerId] -f [Filename] -e [Endpoint]: the codeBeamer endpoint server address (eg. https://codebeamer.com/cb) -u [Username]: login user account name -p [Password]: login user password -pr [ProjectId]: Target project id where the application should inject the input data -tr [TrackerId]: Target tacker id where the application should inject the input data -f [Filename]: Input file data in csv format Example: java -jar importWorkItemsDemo.jar -e https://codebeamer.com/cb -u cbprojectadmin -p cbpass -pr 12 -tr 1234 -f input.csv CSV File example
Rest Client code examplesBasic interfaces and classesCodeBeamerService Rest Interface contains all necessary methods to create or update work items and upload attachments: /** * Use this method to obtain item schema. * * @param projectId is the id of the project * @param trackerId is the id of the tracker * @param config is the necessary data for connection * @return item schema * @throws ServiceException */ public Map obtainItemSchema(String projectId, String trackerId, RestClientConfig config) throws ServiceException; /** * Use this method to create new item. * * @param item is the item object * @param config is the necessary data for connection * @return response from codeBeamer * @throws ServiceException */ public Map createItem(Map<String, Object> item, RestClientConfig config) throws ServiceException; /** * Use this method to obtain schema of an existing item. * * @param itemId is the id of the item * @param config is the necessary data for connection * @return response from codeBeamer * @throws ServiceException */ public Map getItem(String itemId, RestClientConfig config) throws ServiceException; /** * Use this method to update an existing item. * * @param item is the item object * @param config is the necessary data for connection * @return response from codeBeamer * @throws ServiceException */ public Map updateItem(Map<String, Object> item, RestClientConfig config) throws ServiceException; /** * Use this method to upload an attachment. * * @param itemId is the id of the item * @param file is the uploaded file * @param config is the necessary data for connection * @returnresponse from codeBeamer * @throws ServiceException */ public Map uploadAttachmentToItem(String itemId, File file, RestClientConfig config) throws ServiceException; CodeBeamerServiceImpl uses general Rest Client to communicate with codebeamer: public Map createItem(Map<String, Object> item, RestClientConfig config) throws ServiceException { try { Response<Map> response = RestClient.CLIENT.executePost( String.format(ITEM_URL_PATTERN, config.getHost()), new StringEntity(mapper.writeValueAsString(item)), config, Map.class); if (!checkStatusCode(response.getHttpStatus())) { throw new ServiceException( String.format("Error occurred when tried to create item, http status: %s, response: %s", response.getHttpStatus(), response.getContent())); } return response.getContent(); } catch (Exception ex) { throw new ServiceException(ex); } } RestClient is a general rest client implementation. It contains methods to send GET, POST, PUT requests: Example for POST request implementation: /** * Use this method to send POST request. * * @param url is the target url * @param entity contains the body of the request * @param config contains the necessary data for connection * @param clazz is the type of the response * @return POST request response with http status code * @throws RestClientException */ public <T> Response<T> executePost(String url, HttpEntity entity, RestClientConfig config, Class<T> clazz) throws RestClientException { Response<T> result = null; CloseableHttpResponse response = null; try { HttpPost httpPost = new HttpPost(url); httpPost.setEntity(entity); response = client.execute(httpPost, createContext(config)); HttpEntity responseEntity = response.getEntity(); String content = IOUtils.toString(entity.getContent()); result = new Response<T>(response.getStatusLine().getStatusCode(), StringUtils.isNotEmpty(content) ? mapper.readValue(responseEntity.getContent(), clazz) : null); EntityUtils.consume(responseEntity); return result; } catch (Exception ex) { throw new RestClientException(ex); } finally { try { if (response != null) { response.close(); } } catch (IOException ex) { throw new RestClientException(ex); } } } Examples to use CodeBeamerService Rest InterfaceCreate new items
Example: // set project id and tracker id to download the schema Map<String, Object> schema = service.obtainItemSchema( String.valueOf(options.get(Option.PROJECT_ID)), String.valueOf(options.get(Option.TRACKER_ID)), config); // item schema is under the "item" attribute, so get it Map<String, Object> itemSchema = (Map<String, Object>) schema.get("item"); // clone it to use this schema as template to create new items Map<String, Object> itemSchemaClone = cloner.deepClone(itemSchema) // set new field values ... // for ex.: itemSchemaClone.put("description", "Test description"); // create new item on server service.createItem(itemSchemaClone, config); Update existing items
Example: // obtain existing item schema with field values Map<String, Object> itemSchema = (Map<String, Object>) service.getItem(record.get(ID_FIELD), config).get("item"); // update field values ... // for ex.: itemSchema.put("description", "Test description"); //update item on server service.updateItem(itemSchemaClone, config); Upload attachmentsTo upload files to items, call the uploadAttachmentToItem(id, file, config) method of CodeBeamerService class. service.uploadAttachmentToItem(id, file, config); Reading CSV file
|
Fast Links
codebeamer Overview codebeamer Knowledge Base Services by Intland Software |
This website stores cookies on your computer. These cookies are used to improve your browsing experience, constantly optimize the functionality and content of our website, furthermore helps us to understand your interests and provide more personalized services to you, both on this website and through other media. With your permission we and our partners may use precise geolocation data and identification through device scanning. You may click accept to consent to our and our partners’ processing as described above. Please be aware that some processing of your personal data may not require your consent, but you have a right to object to such processing. By using our website, you acknowledge this notice of our cookie practices. By accepting and continuing to browse this site, you agree to this use. For more information about the cookies we use, please visit our Privacy Policy.Your preferences will apply to this website only.