The FastComments Nim SDK. You can use this to build secure and scalable backend applications that interact with FastComments, or build reactive client applications.
- Nim >= 1.6.0
- nimcrypto >= 0.5.4
nimble install fastcommentsnimble buildThis library contains the generated API client and the SSO utilities to make working with the API easier.
For the API client, there are two API modules, api_default and api_public. The api_default contains methods that require your API key, and api_public contains api calls
that can be made directly from a browser/mobile device/etc without authentication.
Important: Authenticated endpoints require your API key to be set as the x-api-key header.
import httpclient
import fastcomments
import fastcomments/apis/api_default
import fastcomments/models/model_comment_data
let client = newHttpClient()
client.headers["x-api-key"] = "your-api-key"
# Make authenticated API calls
let (response, httpResponse) = getComments(
httpClient = client,
tenantId = "your-tenant-id",
page = 0,
limit = 0,
skip = 0,
asTree = false,
skipChildren = 0,
limitChildren = 0,
maxTreeDepth = 0,
urlId = "your-url-id",
userId = "",
anonUserId = "",
contextUserId = "",
hashTag = "",
parentId = "",
direction = SortDirections.DESC
)
if response.isSome:
let resp = response.get()
if resp.comments.isSome:
echo "Found ", resp.comments.get().len, " comments"Public endpoints don't require authentication:
import httpclient
import fastcomments
import fastcomments/apis/api_public
let client = newHttpClient()
# Make public API calls
let (response, httpResponse) = getCommentsPublic(
httpClient = client,
tenantId = "your-tenant-id",
urlId = "your-url-id",
page = 0,
direction = SortDirections.DESC,
sso = "",
skip = 0,
skipChildren = 0,
limit = 0,
limitChildren = 0,
countChildren = false,
fetchPageForCommentId = "",
includeConfig = false,
countAll = false,
includei10n = false,
locale = "",
modules = "",
isCrawler = false,
includeNotificationCount = false,
asTree = false,
maxTreeDepth = 0,
useFullTranslationIds = false,
parentId = "",
searchText = "",
hashTags = @[],
userId = "",
customConfigStr = "",
afterCommentId = "",
beforeCommentId = ""
)
if response.isSome:
let resp = response.get()
if resp.comments.isSome:
echo "Found ", resp.comments.get().len, " comments"- 401 authentication error: Make sure you set the
x-api-keyheader on your HttpClient before making DefaultAPI requests:client.headers["x-api-key"] = "your-api-key" - Wrong API class: Use
api_defaultfor server-side authenticated requests,api_publicfor client-side/public requests.
All API methods in this SDK return tuples of (Option[ResponseType], Response). The first element contains the parsed response if successful, and the second element is the raw HTTP response.
import httpclient
import options
import fastcomments
import fastcomments/apis/api_default
let client = newHttpClient()
client.headers["x-api-key"] = "your-api-key"
let (response, httpResponse) = getComments(
httpClient = client,
tenantId = "your-tenant-id",
page = 0,
limit = 0,
skip = 0,
asTree = false,
skipChildren = 0,
limitChildren = 0,
maxTreeDepth = 0,
urlId = "your-url-id",
userId = "",
anonUserId = "",
contextUserId = "",
hashTag = "",
parentId = "",
direction = SortDirections.DESC
)
if httpResponse.code == Http200:
if response.isSome:
let resp = response.get()
if resp.comments.isSome:
echo "Found ", resp.comments.get().len, " comments"You'll see you're supposed to pass a broadcastId in some API calls. When you receive events, you'll get this ID back, so you know to ignore the event if you plan to optimistically apply changes on the client
(which you'll probably want to do since it offers the best experience). Pass a UUID here. The ID should be unique enough to not occur twice in a browser session.
For SSO examples, see below.
import fastcomments/sso
let user = newSimpleSSOUserData(
userId = "user-123",
email = "user@example.com",
avatar = "https://example.com/avatar.jpg"
)
let sso = newSimple(simpleUserData = user)
let token = sso.createToken()
echo "SSO Token: ", tokenimport fastcomments/sso
let user = newSecureSSOUserData(
userId = "user-123",
email = "user@example.com",
username = "johndoe",
avatar = "https://example.com/avatar.jpg"
)
let apiKey = "your-api-key"
let sso = newSecure(apiKey = apiKey, secureUserData = user)
let token = sso.createToken()
echo "Secure SSO Token: ", tokenSet the required environment variables:
export FASTCOMMENTS_API_KEY="your-api-key"
export FASTCOMMENTS_TENANT_ID="your-tenant-id"Run the tests:
nimble testOr run specific tests:
nim c -r tests/test_sso.nim
nim c -r tests/test_sso_integration.nimTo update the generated client from the OpenAPI spec:
./update.shMIT License - see LICENSE file for details
For support, please visit https://fastcomments.com/auth/my-account/help or email support@fastcomments.com