Aqui você vê as diferenças entre duas revisões dessa página.
Ambos lados da revisão anterior Revisão anterior | |||
manual_usuario:studio:fluxo:designer:scripts [2016/09/20 12:35] administrador [Repeat ... Until] |
manual_usuario:studio:fluxo:designer:scripts [2017/05/31 11:41] (atual) administrador Aprovado |
||
---|---|---|---|
Linha 2646: | Linha 2646: | ||
CloseFile(myFile); | CloseFile(myFile); | ||
end; | end; | ||
+ | |||
+ | ======Biblioteca de Classes====== | ||
+ | ---- | ||
+ | |||
+ | Este tópico descreve e exemplifica o uso de classes utilizadas pelo WFS. | ||
+ | |||
+ | =====HTTP/REST via script===== | ||
+ | ---- | ||
+ | |||
+ | **Exemplo 1: ** Testes de envio (SLACK) para uma pessoa especifica passando o nome de outras pessoa no username; | ||
+ | |||
+ | var | ||
+ | http: TWHTTP; | ||
+ | response: THTTResponse; | ||
+ | jsonObject: TJsonObject; | ||
+ | token, channel, text, username: String; | ||
+ | | ||
+ | begin | ||
+ | |||
+ | try | ||
+ | try | ||
+ | // Criando a conexão | ||
+ | http := TWHTTP.create(); | ||
+ | // Parâmetros necessários para o método GET. | ||
+ | token := http.encodeParameter( 'xoxb-179556611860-ZhO1pl49DXlJOmbDp6nKU9jd' ); | ||
+ | channel := http.encodeParameter( '@juliano.rafael' ); | ||
+ | text := http.encodeParameter( 'Teste de *integração*' ); | ||
+ | username := http.encodeParameter( '@juliano.rafael' ); | ||
+ | // Precisa de SSL. | ||
+ | http.setSSL( true ); | ||
+ | response := http.doGet( 'https://slack.com/api/chat.postMessage?token=' + token + '&channel=' + channel + '&text=' + text + '&username=' + username ); | ||
+ | jsonObject := response.GetResponseJson(response.GetResponseString()); | ||
+ | except | ||
+ | showMessage( 'ocorreu erro' ); | ||
+ | end; | ||
+ | finally | ||
+ | http.Free(); | ||
+ | response.Free(); | ||
+ | end; | ||
+ | | ||
+ | end; | ||
+ | |||
+ | **Exemplo 2: ** Teste de envio para um canal se passando por um usuário; | ||
+ | |||
+ | var | ||
+ | http: TWHTTP; | ||
+ | response: THTTResponse; | ||
+ | jsonObject: TJsonObject; | ||
+ | token, channel, text, username: String; | ||
+ | | ||
+ | begin | ||
+ | |||
+ | try | ||
+ | try | ||
+ | // Criando a conexão | ||
+ | http := TWHTTP.create(); | ||
+ | // Parâmetros necessários para o método GET. | ||
+ | token := http.encodeParameter( 'xoxb-179556611860-ZhO1pl49DXlJOmbDp6nKU9jd' ); | ||
+ | channel := http.encodeParameter( 'channel' ); | ||
+ | text := http.encodeParameter( 'Teste de *integração*' ); | ||
+ | username := http.encodeParameter( '@juliano.rafael' ); | ||
+ | // Precisa de SSL. | ||
+ | http.setSSL( true ); | ||
+ | response := http.doGet( 'https://slack.com/api/chat.postMessage?token=' + token + '&channel=' + channel + '&text=' + text + '&username=' + username ); | ||
+ | // Convertendo o retorno em JsonObject. | ||
+ | jsonObject := response.GetResponseJson(response.GetResponseString()); | ||
+ | except | ||
+ | showMessage( 'ocorreu erro' ); | ||
+ | end; | ||
+ | finally | ||
+ | http.Free(); | ||
+ | response.Free(); | ||
+ | end; | ||
+ | | ||
+ | end; | ||
+ | |||
+ | **Exemplo 3: ** Carregando lista de usuários do Slack; | ||
+ | |||
+ | var | ||
+ | http: TWHTTP; | ||
+ | response: THTTResponse; | ||
+ | jsonObject: TJsonObject; | ||
+ | jsonPair: TJSonPair; | ||
+ | jsonArray: TJsonArray; | ||
+ | token, url: String; | ||
+ | i: Integer; | ||
+ | | ||
+ | begin | ||
+ | |||
+ | try | ||
+ | try | ||
+ | // Criando a conexão | ||
+ | http := TWHTTP.create(); | ||
+ | // Parâmetros necessários para o método GET. | ||
+ | token := http.encodeParameter( 'xoxb-179556611860-ZhO1pl49DXlJOmbDp6nKU9jd' ); | ||
+ | // Precisa de SSL. | ||
+ | http.setSSL( true ); | ||
+ | url := 'https://slack.com/api/users.list?token=' + token; | ||
+ | response := http.doGet( url ); | ||
+ | // Convertendo o retorno em JsonObject. | ||
+ | jsonObject := response.GetResponseJson(response.GetResponseString()); | ||
+ | jsonPair := response.getByName( jsonObject, 'members' ); | ||
+ | jsonArray := jsonPair.jsonValue; | ||
+ | for i := 0 to jsonArray.size - 1 do | ||
+ | begin | ||
+ | showMessage( TJsonObject( jsonArray.get(i) ).get(2).jsonValue.value ); | ||
+ | end; | ||
+ | | ||
+ | except | ||
+ | showMessage( 'ocorreu erro' ); | ||
+ | end; | ||
+ | finally | ||
+ | http.Free(); | ||
+ | response.Free(); | ||
+ | end; | ||
+ | | ||
+ | end; | ||
+ | |||
+ | **Exemplo 4: ** Inserindo Issue no Jira via basic authentication; | ||
+ | |||
+ | var | ||
+ | http: TWHTTP; | ||
+ | response: THTTResponse; | ||
+ | jsonObject: TJsonObject; | ||
+ | url, paramJson: String; | ||
+ | | ||
+ | begin | ||
+ | |||
+ | try | ||
+ | try | ||
+ | // Criando a conexão | ||
+ | http := TWHTTP.create(); | ||
+ | // Neste caso necessario autenticação básica | ||
+ | http.setBasicAuthentication( true ); | ||
+ | http.setUser( 'board-jira@rh3software.com' ); | ||
+ | http.setPassWord( 'Rh3@1593576482' ); | ||
+ | // Será passado um Json. | ||
+ | http.setContentType( 'application/json' ); | ||
+ | // Default charset. | ||
+ | http.setCharset( 'utf-8' ); | ||
+ | // Precisa de SSL. | ||
+ | http.setSSL( true ); | ||
+ | // Json em forma de string para ser passado como data para o doPost. | ||
+ | paramJson := '{ | ||
+ | "fields": { | ||
+ | "project": | ||
+ | { | ||
+ | "key": "RH3" | ||
+ | }, | ||
+ | "summary": "Segundo testes da API.", | ||
+ | "description": "Creating of an issue using project keys and issue type names using the REST API", | ||
+ | "issuetype": { | ||
+ | "name": "Bug" | ||
+ | } | ||
+ | } | ||
+ | }'; | ||
+ | // URL | ||
+ | url := 'https://rh3software.atlassian.net/rest/api/2/issue'; | ||
+ | // Response do doPost | ||
+ | response := http.doPost( url, paramJson ); | ||
+ | // Convertendo o retorno em JsonObject. | ||
+ | jsonObject := response.GetResponseJson(response.GetResponseString()); | ||
+ | // Pegando o objeto retornado para mostrar a chave que foi incluída. | ||
+ | showMessage( response.getByName( jsonObject, 'key' ).jsonValue.value ); | ||
+ | | ||
+ | except | ||
+ | showMessage( 'ocorreu erro' ); | ||
+ | end; | ||
+ | finally | ||
+ | http.Free(); | ||
+ | response.Free(); | ||
+ | end; | ||
+ | | ||
+ | end; | ||
+ | |||
+ | **Exemplo 5: ** Retornando um statuscode >= 400 da api. | ||
+ | |||
+ | var | ||
+ | http: TWHTTP; | ||
+ | response: THTTResponse; | ||
+ | jsonObject: TJsonObject; | ||
+ | url, paramJson: String; | ||
+ | | ||
+ | begin | ||
+ | |||
+ | try | ||
+ | try | ||
+ | // Criando a conexão | ||
+ | http := TWHTTP.create(); | ||
+ | // Neste caso necessario autenticação básica | ||
+ | http.setBasicAuthentication( true ); | ||
+ | http.setUser( 'board-jira@rh3software.com' ); | ||
+ | http.setPassWord( 'Rh3@1593576482' ); | ||
+ | // Será passado um Json. | ||
+ | http.setContentType( 'application/json' ); | ||
+ | // Default charset. | ||
+ | http.setCharset( 'utf-8' ); | ||
+ | // Precisa de SSL. | ||
+ | http.setSSL( true ); | ||
+ | // Json em forma de string para ser passado como data para o doPost. | ||
+ | paramJson := '{ | ||
+ | "fields": { | ||
+ | "project": | ||
+ | { | ||
+ | "key": "fdsafdas" | ||
+ | }, | ||
+ | "summary": "REST ye merry gentlemen.", | ||
+ | "description": "Creating of an issue using project keys and issue type names using the REST API", | ||
+ | "issuetype": { | ||
+ | "name": "Bug" | ||
+ | } | ||
+ | } | ||
+ | }'; | ||
+ | // URL | ||
+ | url := 'https://rh3software.atlassian.net/rest/api/2/issue'; | ||
+ | // Response do doPost | ||
+ | response := http.doPost( url, paramJson ); | ||
+ | // Convertendo o retorno em JsonObject. | ||
+ | jsonObject := response.GetResponseJson(response.GetResponseString()); | ||
+ | // Pegando o objeto retornado porem o statuscode > 400 | ||
+ | if response.getStatusCode() >= 400 then | ||
+ | showMessage( TJsonObject( response.getByName( jsonObject, 'errors' ).jsonValue ).get(0).jsonValue.value ); | ||
+ | | ||
+ | except | ||
+ | showMessage( 'ocorreu erro' ); | ||
+ | end; | ||
+ | finally | ||
+ | http.Free(); | ||
+ | response.Free(); | ||
+ | end; | ||
+ | | ||
+ | end; | ||
+ |