Skip to content

Commit

Permalink
Update Signer class.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Apr 18, 2022
1 parent 7187d1d commit cb85962
Show file tree
Hide file tree
Showing 23 changed files with 3,688 additions and 2,124 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 mobizt
Copyright (c) 2022 mobizt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# Arduino Google Sheet Client Library for ESP8266 and ESP32


Arduino Google Sheet Client Library for ESP8266 and ESP32 v1.0.0.
Arduino Google Sheet Client Library for ESP8266 and ESP32 v1.0.1.

This library allows devices to authenticate and communicate with Google Sheet APIs using the Service Account.

Devices will be able to read, update, append and clear sheet values. Create, read, list, and delete the spreadsheet are also supported.


The spreadsheet that created using this library owned by the Service Account and shared access to the user.
The spreadsheet that created using this library, owned by the Service Account and shared access to the user.


You can create, edit and deploy the Apps Script code via extension of spreadsheet that created by this library except for run the script due to permission denied.


Spreadsheet created or owned by you needed to share the access with Service Account's client email then library can read, edit except for delete the user's spreadsheet due to permission denied.
Spreadsheet created or owned by you, needed to share the access with Service Account's client email then library can read, and edit except for delete the user's spreadsheet due to permission denied.


## Important Note

The connection to Google Sheet API endpoint takes time and the time from sending request to complete response received can be as much as 3 seconds.
The connection to Google Sheet API endpoint takes time and the time from sending request to the complete response received can be as much as 3 seconds.

The speed is not the library or device issue unless the server issue.

Expand Down Expand Up @@ -1232,7 +1232,7 @@ bool search(FirebaseJson *response, <string> spreadsheetId, FirebaseJsonArray *d

The MIT License (MIT)

Copyright (C) 2021 K. Suwatchai (Mobizt)
Copyright (C) 2022 K. Suwatchai (Mobizt)


Permission is hereby granted, free of charge, to any person returning a copy of
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ESP-Google-Sheet-Client",
"version": "1.0.0",
"version": "1.0.1",
"keywords": "communication, REST, esp32, esp8266, arduino",
"description": "Arduino Google Sheet REST client library for ESP8266 and ESP32. This library allows devices to communicate with Google Sheet API to read, edit and delete the spreadsheets",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=ESP-Google-Sheet-Client

version=1.0.0
version=1.0.1

author=Mobizt

Expand Down
83 changes: 39 additions & 44 deletions src/ESP_Google_Sheet_Client.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
/**
* Google Sheet Client, ESP_Google_Sheet_Client.cpp v1.0.0
* Google Sheet Client, ESP_Google_Sheet_Client.cpp v1.0.1
*
* This library supports Espressif ESP8266 and ESP32 MCUs
*
* Created December 19, 2021
* Created April 18, 2022
*
*
*
* This work is a part of Firebase ESP Client library
* Copyright (c) 2021 K. Suwatchai (Mobizt)
*
* The MIT License (MIT)
* Copyright (c) 2021 K. Suwatchai (Mobizt)
* Copyright (c) 2022 K. Suwatchai (Mobizt)
*
*
* Permission is hereby granted, free of charge, to any person returning a copy of
Expand Down Expand Up @@ -114,7 +109,7 @@ bool GSheetClass::setClock(float gmtOffset)
return config._int.esp_signer_clock_rdy;
}

void GSheetClass::beginRequest(FirebaseJson *response, MBSTRING &req, host_type_t host_type)
void GSheetClass::beginRequest(FirebaseJson *response, MB_String &req, host_type_t host_type)
{

if (response)
Expand All @@ -141,7 +136,7 @@ void GSheetClass::beginRequest(FirebaseJson *response, MBSTRING &req, host_type_
setSecure();
}

void GSheetClass::addHeader(MBSTRING &req, host_type_t host_type, int len)
void GSheetClass::addHeader(MB_String &req, host_type_t host_type, int len)
{
req += FPSTR(" HTTP/1.1\r\n");
if (host_type == host_type_sheet)
Expand All @@ -155,7 +150,7 @@ void GSheetClass::addHeader(MBSTRING &req, host_type_t host_type, int len)
if (len > -1)
{
req += FPSTR("Content-Length: ");
req += NUM2S(len).get();
req += len;
req += FPSTR("\r\n");

req += FPSTR("Content-Type: application/json\r\n");
Expand Down Expand Up @@ -232,7 +227,7 @@ void GSheetClass::setSecure()
}
}

bool GSheetClass::processRequest(FirebaseJson *response, MBSTRING &req, int &httpcode)
bool GSheetClass::processRequest(FirebaseJson *response, MB_String &req, int &httpcode)
{

config.signer.json = new FirebaseJson();
Expand All @@ -242,7 +237,7 @@ bool GSheetClass::processRequest(FirebaseJson *response, MBSTRING &req, int &htt

if (ret == 0)
{
if (this->handleServerResponse(httpcode))
if (this->handleTokenResponse(httpcode))
{
ret = 1;
if (parseJsonResponse(esp_signer_pgm_str_68) || parseJsonResponse(esp_signer_pgm_str_113))
Expand All @@ -264,7 +259,7 @@ bool GSheetClass::mGet(FirebaseJson *response, const char *spreadsheetId, const
if (!checkToken())
return false;

MBSTRING req;
MB_String req;
int httpcode = 0;

beginRequest(response, req, host_type_sheet);
Expand All @@ -285,14 +280,14 @@ bool GSheetClass::mGet(FirebaseJson *response, const char *spreadsheetId, const
req += spreadsheetId;
req += FPSTR("/values:batchGet");

std::vector<MBSTRING> rngs = std::vector<MBSTRING>();
MBSTRING rng = ranges;
std::vector<MB_String> rngs = std::vector<MB_String>();
MB_String rng = ranges;
ut->splitTk(rng, rngs, ",");

if (rngs.size() == 0)
return false;

MBSTRING s;
MB_String s;

for (size_t i = 0; i < rngs.size(); i++)
{
Expand Down Expand Up @@ -438,7 +433,7 @@ bool GSheetClass::mUpdate(bool append, operation_type_t type, FirebaseJson *resp
if (!checkToken())
return false;

MBSTRING req;
MB_String req;
int httpcode = 0;

beginRequest(response, req, host_type_sheet);
Expand Down Expand Up @@ -528,7 +523,7 @@ bool GSheetClass::mClear(FirebaseJson *response, const char *spreadsheetId, cons
if (!checkToken())
return false;

MBSTRING req;
MB_String req;
int httpcode = 0;

beginRequest(response, req, host_type_sheet);
Expand All @@ -554,20 +549,20 @@ bool GSheetClass::mClear(FirebaseJson *response, const char *spreadsheetId, cons
if (type == operation_type_batch)
{
req += FPSTR("/values:batchClear");
std::vector<MBSTRING> rngs = std::vector<MBSTRING>();
MBSTRING rng = ranges;
std::vector<MB_String> rngs = std::vector<MB_String>();
MB_String rng = ranges;
ut->splitTk(rng, rngs, ",");

if (rngs.size() == 0)
return false;

FirebaseJson r;
MBSTRING tmp;
MB_String tmp;

for (size_t i = 0; i < rngs.size(); i++)
{
tmp = (const char *)FPSTR("ranges/[");
tmp += NUM2S(i).get();
tmp += i;
tmp += (const char *)FPSTR("]");

r.set(tmp.c_str(), rngs[i].c_str());
Expand Down Expand Up @@ -602,7 +597,7 @@ bool GSheetClass::copyTo(FirebaseJson *response, const char *spreadsheetId, uint
if (!checkToken())
return false;

MBSTRING req;
MB_String req;
int httpcode = 0;

beginRequest(response, req, host_type_sheet);
Expand All @@ -611,10 +606,10 @@ bool GSheetClass::copyTo(FirebaseJson *response, const char *spreadsheetId, uint
req += spreadsheetId;

req += FPSTR("/sheets/");
req += NUM2S(sheetId).get();
req += sheetId;
req += FPSTR(":copyTo");

MBSTRING s;
MB_String s;
s = FPSTR("{\"destinationSpreadsheetId\":\"");
s += destinationSpreadsheetId;
s += "\"}";
Expand All @@ -632,7 +627,7 @@ bool GSheetClass::batchUpdate(FirebaseJson *response, const char *spreadsheetId,
if (!checkToken())
return false;

MBSTRING req;
MB_String req;
int httpcode = 0;

beginRequest(response, req, host_type_sheet);
Expand Down Expand Up @@ -666,18 +661,18 @@ bool GSheetClass::batchUpdate(FirebaseJson *response, const char *spreadsheetId,

if (strlen(responseRanges) > 0)
{
std::vector<MBSTRING> rngs = std::vector<MBSTRING>();
MBSTRING rng = responseRanges;
std::vector<MB_String> rngs = std::vector<MB_String>();
MB_String rng = responseRanges;
ut->splitTk(rng, rngs, ",");

if (rngs.size() == 0)
return false;
MBSTRING tmp;
MB_String tmp;

for (size_t i = 0; i < rngs.size(); i++)
{
tmp = (const char *)FPSTR("responseRanges/[");
tmp += NUM2S(i).get();
tmp += i;
tmp += (const char *)FPSTR("]");

js.set(tmp.c_str(), rngs[i].c_str());
Expand All @@ -702,7 +697,7 @@ bool GSheetClass::create(FirebaseJson *response, FirebaseJson *spreadsheet, cons
if (!checkToken())
return false;

MBSTRING req;
MB_String req;
int httpcode = 0;

beginRequest(response, req, host_type_sheet);
Expand Down Expand Up @@ -737,15 +732,15 @@ bool GSheetClass::getMetadata(FirebaseJson *response, const char *spreadsheetId,
if (!checkToken())
return false;

MBSTRING req;
MB_String req;
int httpcode = 0;

beginRequest(response, req, host_type_sheet);

req = FPSTR("GET /v4/spreadsheets/");
req += spreadsheetId;
req += FPSTR("/developerMetadata/");
req += NUM2S(metadataId).get();
req += metadataId;

addHeader(req, host_type_sheet);

Expand All @@ -761,7 +756,7 @@ bool GSheetClass::searchMetadata(FirebaseJson *response, const char *spreadsheet

if (dataFiltersArray)
{
MBSTRING req;
MB_String req;
int httpcode = 0;

beginRequest(response, req, host_type_sheet);
Expand Down Expand Up @@ -790,20 +785,20 @@ bool GSheetClass::getSpreadsheet(FirebaseJson *response, const char *spreadsheet
if (!checkToken())
return false;

MBSTRING req;
MB_String req;
int httpcode = 0;

beginRequest(response, req, host_type_drive);

req = FPSTR("GET /v4/spreadsheets/");
req += spreadsheetId;

MBSTRING s;
MB_String s;

if (strlen(ranges) > 0)
{
std::vector<MBSTRING> rngs = std::vector<MBSTRING>();
MBSTRING rng = ranges;
std::vector<MB_String> rngs = std::vector<MB_String>();
MB_String rng = ranges;
ut->splitTk(rng, rngs, ",");

if (rngs.size() == 0)
Expand Down Expand Up @@ -852,7 +847,7 @@ bool GSheetClass::getSpreadsheetByDataFilter(FirebaseJson *response, const char

if (dataFiltersArray)
{
MBSTRING req;
MB_String req;
int httpcode = 0;

beginRequest(response, req, host_type_drive);
Expand Down Expand Up @@ -891,7 +886,7 @@ bool GSheetClass::deleteFile(FirebaseJson *response, const char *spreadsheetId,
config.signer.wcs = NULL;
}

MBSTRING req;
MB_String req;
int httpcode = 0;

beginRequest(response, req, host_type_drive);
Expand Down Expand Up @@ -973,7 +968,7 @@ bool GSheetClass::listFiles(FirebaseJson *response, uint32_t pageSize, const cha
delete config.signer.wcs;
config.signer.wcs = NULL;

MBSTRING req;
MB_String req;
int httpcode = 0;

if (pageSize == 0 || pageSize > 10)
Expand All @@ -982,7 +977,7 @@ bool GSheetClass::listFiles(FirebaseJson *response, uint32_t pageSize, const cha
beginRequest(response, req, host_type_drive);

req = FPSTR("GET /drive/v3/files?pageSize=");
req += NUM2S(pageSize).get();
req += pageSize;

if (strlen(orderBy) > 0)
{
Expand Down Expand Up @@ -1015,7 +1010,7 @@ bool GSheetClass::createPermission(FirebaseJson *response, const char *fileid, c
delete config.signer.wcs;
config.signer.wcs = NULL;

MBSTRING req;
MB_String req;
int httpcode = 0;

beginRequest(response, req, host_type_drive);
Expand Down
Loading

0 comments on commit cb85962

Please sign in to comment.