使用更长但有效的方式是一个好方法吗?

问题描述 投票:0回答:1

我的英语会酸化你的眼睛,阅读风险自负

你好社区!

这不是关于“我的代码中的问题”的典型问题, 请问我走的方向对吗?

一个例子来介绍我的想法-SmartSheet. 很快,Smartsheet = 一些更好但绝对不完美的“在云中表现出色”的服务, 可能比谷歌表强(但 idk)。


所以:

我使用 Java/Spring 工作。

我应该创建一个应用程序,可以实时更改大量工作表上的大量单元格。 Smartsheet 有一个开放的 SDK 来使用 API 并且他们有不错的文档

但是.

不幸的是,这个 SDK 对我来说真的很难/不舒服,所以我使用 unirest 库 来处理遗留的 http 请求。

例如:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import me.vitaliygo.imsmartsheetbackend.entity.sheet.tab.SheetTabRoot;
import me.vitaliygo.imsmartsheetbackend.entity.workspace.WorkspaceRoot;
import me.vitaliygo.imsmartsheetbackend.entity.workspace.solo.WorkspaceSoloRoot;
import org.springframework.http.HttpHeaders;

import java.io.IOException;
import java.util.HashMap;
    
public class JSONManipulator {

        private final String authValue = "Bearer Token123Token123";
        private final String authHead = "Authorization";
        HttpHeaders headers = new HttpHeaders();
        HttpResponse<String> response;
        HashMap<String,String> responseMap;
        String sheetsUri = "https://api.smartsheet.com/2.0/sheets/";
        String workspaceUri = "https://api.smartsheet.com/2.0/workspaces/";
        String foldersUri = "https://api.smartsheet.com/2.0/folders/";
        String reportsUri = "https://api.smartsheet.com/2.0/reports/";
        String homeUri = "https://api.smartsheet.com/2.0/home/";


    public String workspaceUri(String id){
        return workspaceUri+"/"+id;
    }
    public String sheetUri(String id){
        return sheetsUri+"/"+id;
    }
    public String columnsUri(String sheetId){
        return sheetUri(sheetId)+"/columns";
    }
    public String foldersUri(String id){
        return foldersUri+"/"+id;
    }

 public void putValue(String sheetsId, String rowsId, String jsonContext) throws IOException {
        try {
            HttpResponse<String> response = Unirest.put(sheetUri(sheetsId)+"/rows/"+rowsId)
                    .header(authHead, authValue)
                    .header("Content-Type", "application/json")
                    .body(jsonContext)
                    .asString();
        } catch (UnirestException e) {
            throw new RuntimeException(e);
        }
    }
    public String getWorkspaceTree(String id) throws IOException {
        try {
            response = Unirest.get(workspaceUri(id))
                    .header(authHead, authValue)
                    .asString();
        } catch (UnirestException e) {
            throw new RuntimeException(e);
        }
        return response.getBody();
    }
    public String getSheet(String id) throws IOException {
        try {
            response = Unirest.get(sheetUri(id)).queryString(new HashMap<>())
                    .header(authHead, authValue)
                    .asString();
        } catch (UnirestException e) {
            throw new RuntimeException(e);
        }
        return response.getBody().intern();
    }

从关于 SDK 的 SmartSheet Doc 可以是:

// Sample 1: Get sheet
// Omit all parameters
Sheet sheet = smartsheet.sheetResources().getSheet(
        4583173393803140L,      // long sheetId
        null,                   // EnumSet<SheetInclusion> includes
        null,                   // EnumSet<ObjectExclusion> excludes
        null,                   // Set<Long> rowIds
        null,                   // Set<Integer> rowNumbers
        null,                   // Set<Long> columnIds
        null,                   // Integer pageSize
        null                    // Integer page
        );

// Sample 2: Get sheet as Excel
smartsheet.sheetResources().getSheetAsExcel(
        4583173393803140L,       // long sheetId
        outputStream
        );

// Sample 3: Get sheet as PDF
smartsheet.sheetResources().getSheetAsPDF(
        4583173393803140L,       // long sheetId
        outputStream,
        PaperSize.A1
        );

// Sample 4: Get sheet as CSV
smartsheet.sheetResources().getSheetAsCSV(
        4583173393803140L,       // long sheetId
        outputStream
        );

所以...

有意义,如果对我来说更好的话,使用大量代码或者我应该学习 sdk 来掌握它?

java json http curl smartsheet-api
1个回答
0
投票

一句话回答:绝对.

写一段代码的目的应该是:

  1. 添加功能
  2. 其他人应该易于阅读和理解
  3. 代码应该干净,并遵循语言的原则,例如 SOLID 原则。

因此,只要你的代码服务于目的,并且其他人不费吹灰之力就能看懂代码,那是完全可以接受的。

© www.soinside.com 2019 - 2024. All rights reserved.