Go Examples for adaline.ink
Go code examples for the Adaline platform API using the standard library.
All examples use the Go standard library. No external dependencies.
Setup
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
const baseURL = "https://api.adaline.ink"
var accessToken string
func apiRequest(method, path string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, baseURL+path, body)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+accessToken)
if body != nil && method != "GET" {
req.Header.Set("Content-Type", "application/json")
}
return http.DefaultClient.Do(req)
}
func parseJSON(resp *http.Response) map[string]interface{} {
defer resp.Body.Close()
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
return result
}
Authentication
func login(email, password string) error {
body, _ := json.Marshal(map[string]string{
"email": email, "password": password,
})
resp, err := http.Post(baseURL+"/api/v1/auth/login",
"application/json", bytes.NewReader(body))
if err != nil {
return err
}
result := parseJSON(resp)
accessToken = result["access_token"].(string)
return nil
}
func main() {
login("you@example.com", "your-password")
}
Upload a Document
func uploadDocument(orgID, filePath string) (map[string]interface{}, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
part, _ := writer.CreateFormFile("file", filepath.Base(filePath))
io.Copy(part, file)
writer.Close()
url := fmt.Sprintf("%s/api/v1/documents/upload?org_id=%s", baseURL, orgID)
req, _ := http.NewRequest("POST", url, &buf)
req.Header.Set("Authorization", "Bearer "+accessToken)
req.Header.Set("Content-Type", writer.FormDataContentType())
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
return parseJSON(resp), nil
}
List Documents
func listDocuments(orgID string) (map[string]interface{}, error) {
path := fmt.Sprintf("/api/v1/documents/?org_id=%s", orgID)
resp, err := apiRequest("GET", path, nil)
if err != nil {
return nil, err
}
return parseJSON(resp), nil
}
Run Compliance Check
func runCompliance(documentID string) (map[string]interface{}, error) {
body, _ := json.Marshal(map[string]string{"document_id": documentID})
resp, err := apiRequest("POST", "/api/v1/compliance/test", bytes.NewReader(body))
if err != nil {
return nil, err
}
return parseJSON(resp), nil
}
Start Conversion
func startConversion(documentID string) (map[string]interface{}, error) {
body, _ := json.Marshal(map[string]string{"document_id": documentID})
resp, err := apiRequest("POST", "/api/v1/conversions/", bytes.NewReader(body))
if err != nil {
return nil, err
}
return parseJSON(resp), nil
}
func getConversionStatus(jobID string) (map[string]interface{}, error) {
resp, err := apiRequest("GET", "/api/v1/conversions/"+jobID, nil)
if err != nil {
return nil, err
}
return parseJSON(resp), nil
}
Tags
func createTag(orgID, name, color string) (map[string]interface{}, error) {
body, _ := json.Marshal(map[string]string{
"name": name, "color": color, "org_id": orgID,
})
resp, err := apiRequest("POST", "/api/v1/tags", bytes.NewReader(body))
if err != nil {
return nil, err
}
return parseJSON(resp), nil
}
func tagDocument(tagID, docID string) error {
path := fmt.Sprintf("/api/v1/tags/%s/documents/%s", tagID, docID)
resp, err := apiRequest("POST", path, nil)
if err != nil {
return err
}
resp.Body.Close()
return nil
}
Share Document
func createPublicLink(docID string) (map[string]interface{}, error) {
body, _ := json.Marshal(map[string]interface{}{})
path := fmt.Sprintf("/api/v1/documents/%s/public-share", docID)
resp, err := apiRequest("POST", path, bytes.NewReader(body))
if err != nil {
return nil, err
}
return parseJSON(resp), nil
}
Batch Upload and Score
func batchUploadAndScore(orgID, dir string) {
entries, _ := os.ReadDir(dir)
supported := map[string]bool{".pdf": true, ".docx": true, ".pptx": true}
for _, entry := range entries {
ext := filepath.Ext(entry.Name())
if !supported[ext] {
continue
}
fmt.Printf("Processing %s...\n", entry.Name())
doc, err := uploadDocument(orgID, filepath.Join(dir, entry.Name()))
if err != nil {
fmt.Printf(" Upload error: %v\n", err)
continue
}
report, err := runCompliance(doc["id"].(string))
if err != nil {
fmt.Printf(" Scan error: %v\n", err)
continue
}
fmt.Printf(" Score: %.1f%% (%s)\n",
report["overall_score"], report["grade"])
}
}