GUID as a Service — Example Code

Here’s some example code of how to create a GUID using the enterprisy microservice GUID as a Service architecture instead of using your programming language’s native functionality, in a handful of somewhat popular programming languages. Some of these examples might even work.

Java

Native code

import java.util.UUID;

final UUID guid = UUID.randomUUID();

Using GUID as a Service

(This uses the Java HTTP Client added in Java 11. You probably need to refer to a different tutorial for whichever HTTP library your project actually uses. You’ll also want to use a real JSON parsing library for anything more complicated rather than relying on getting the right substring.)

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.UUID;

final HttpClient client = HttpClient.newHttpClient();
final HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://api.gaas.cooperjr.name/"))
        .build();
final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
final String responseBody = response.body();
final String guidString = responseBody.substring(1, 37);
final UUID guid = UUID.fromString(guidString);

C#

Native code

Guid g = Guid.NewGuid();

Using GUID as a Service

readonly HttpClient client = new HttpClient();

string guid = await client.GetStringAsync("https://api.gaas.cooperjr.name/");

Python

Native code

import uuid
guid = uuid.uuid4()

Using GUID as a Service

import json
import urllib.request
guid = json.loads(urllib.request.urlopen("https://api.gaas.cooperjr.name/").read().decode("utf-8"))

Powershell

Native code

New-Guid

Using GUID as a Service

[Guid]( (Invoke-RestMethod -Uri "https://api.gaas.cooperjr.name/") )

Javascript (client-side)

Native code

function makeGUID(callback) {
  var crypto = window.crypto || window.msCrypto;
  var values = crypto.getRandomValues(new Uint8Array(16));
  values[8] |= 0x80;
  values[8] &= 0xBF;
  values[6] |= 0x40;
  values[6] &= 0x4F;
  var result = "";
  for (var valueIndex = 0; valueIndex < 16; valueIndex++) {
    var value = values[valueIndex];
    if (value < 0x10) {
      result += "0";
    }
    result += value.toString(16);
    if (valueIndex === 3 || valueIndex === 5 || valueIndex === 7 || valueIndex === 9) {
      result += "-";
    }
  }
  callback(result);
}
makeGUID(console.log);

(Note that there is a proposal to add GUID functionality to the Javascript language, which would undoubtedly make this much easier.)

Using GUID as a Service

function makeGUID(callback) {
  var req = new XMLHttpRequest();

  req.onreadystatechange = function() {
    if (req.readyState == XMLHttpRequest.DONE ) {
      var result = req.response;
      if (!(typeof(result) === "string")) {
        result = JSON.stringify(result);
      }
      callback(result);
    }
  };

  req.open("GET", "https://api.gaas.cooperjr.name/", true);
  req.responseType = "json";
  req.send();
}
makeGUID(console.log);

Javascript (server-side Node.js)

Native code

(Use the uuid library from npm.)

const { v4: uuidv4 } = require('uuid');
const guid = uuidv4();
console.log(guid);

Using GUID as a Service

const https = require('https');

function makeGUID(callback) {
  https.get('https://api.gaas.cooperjr.name/', (res) => {
    let rawData = '';
    res.on('data', (chunk) => { rawData += chunk; });
    res.on('end', () => {
      const parsedData = JSON.parse(rawData);
      callback(parsedData);
    });
  });
}

makeGUID(console.log);