Skip to content

Commit 3001a9d

Browse files
levimichaelmillotp
authored andcommitted
feat: add onboarding snippets
1 parent 1ead138 commit 3001a9d

File tree

9 files changed

+224
-0
lines changed

9 files changed

+224
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Net.Http;
4+
using System.Collections.Generic;
5+
6+
{{> snippets/import}}
7+
8+
class Program
9+
{
10+
public static void Main(string[] args)
11+
{
12+
// read json file from url
13+
var url = "https://dashboard.algolia.com/sample_datasets/movie.json";
14+
var httpClient = new HttpClient();
15+
var response = httpClient.GetAsync(url).Result;
16+
var content = response.Content.ReadAsStringAsync().Result;
17+
18+
// parse json
19+
var movies = JsonSerializer.Deserialize<List<dynamic>>(content);
20+
21+
// initiate client and index
22+
{{> snippets/init}}
23+
24+
// push data to algolia
25+
try
26+
{
27+
var result = {{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}.Result;
28+
}
29+
catch (Exception e)
30+
{
31+
Console.WriteLine(e.Message);
32+
}
33+
}
34+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
{{> snippets/import}}
10+
11+
func main() {
12+
// read json file
13+
url := "https://dashboard.algolia.com/sample_datasets/movie.json"
14+
response, err := http.Get(url)
15+
16+
if err != nil {
17+
fmt.Println("Could not open url")
18+
return
19+
}
20+
21+
defer response.Body.Close()
22+
23+
// parse json file to Movie struct
24+
var movies []map[string]interface{}
25+
err = json.NewDecoder(response.Body).Decode(&movies)
26+
27+
if err != nil {
28+
fmt.Println("Could not decode body")
29+
return
30+
}
31+
32+
// initiate client
33+
{{> snippets/init}}
34+
35+
// push data to algolia
36+
result, err := {{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}
37+
if err != nil {
38+
fmt.Println(err)
39+
return
40+
}
41+
42+
fmt.Println(fmt.Sprintf("Done! Uploaded records in %d batches.", len(result)))
43+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.io.InputStream;
2+
import java.net.URI;
3+
import java.net.URL;
4+
import java.util.List;
5+
6+
{{> snippets/import}}
7+
8+
import com.fasterxml.jackson.core.type.TypeReference;
9+
import com.fasterxml.jackson.databind.JsonNode;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
12+
public class Main {
13+
public static void main(String[] args) throws Exception {
14+
// Fetch sample dataset
15+
URL url = new URI("https://dashboard.algolia.com/sample_datasets/movie.json").toURL();
16+
InputStream stream = url.openStream();
17+
ObjectMapper mapper = new ObjectMapper();
18+
List<JsonNode> result = mapper.readValue(stream, new TypeReference<List<JsonNode>>() {});
19+
stream.close();
20+
21+
// Connect and authenticate with your Algolia app
22+
{{> snippets/init}}
23+
24+
// Save records in Algolia index
25+
{{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}
26+
client.close();
27+
}
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.example
2+
{{> snippets/import}}
3+
import kotlinx.serialization.builtins.ListSerializer
4+
import kotlinx.serialization.json.Json
5+
import kotlinx.serialization.json.JsonObject
6+
import java.net.URI
7+
8+
suspend fun main() {
9+
val url = URI.create("https://dashboard.algolia.com/sample_datasets/movie.json")
10+
val json = url.toURL().readText()
11+
val records: List<JsonObject> = Json.decodeFromString(ListSerializer(JsonObject.serializer()), json)
12+
13+
{{> snippets/init}}
14+
15+
try {
16+
{{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}
17+
} catch(e: Exception) {
18+
println(e.message)
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
require(__DIR__."/vendor/autoload.php");
3+
{{> snippets/import}}
4+
5+
// Fetch sample dataset
6+
$url = "https://dashboard.algolia.com/sample_datasets/movie.json";
7+
$response = file_get_contents($url);
8+
$records = json_decode($response, true);
9+
10+
// Connect and authenticate with your Algolia app
11+
{{> snippets/init}}
12+
13+
// Save records in Algolia index
14+
{{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}
15+
16+
print('Done!');
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import requests
2+
{{> snippets/import}}
3+
4+
# Fetch sample dataset
5+
url = "https://dashboard.algolia.com/sample_datasets/movie.json"
6+
records = requests.get(url).json()
7+
8+
# Connect and authenticate with your Algolia app
9+
{{> snippets/init}}
10+
11+
# Save records in Algolia index
12+
{{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'uri'
2+
{{> snippets/import}}
3+
4+
# Fetch sample dataset
5+
uri = URI('https://dashboard.algolia.com/sample_datasets/movie.json')
6+
response = Net::HTTP.get_response(uri)
7+
records = JSON.parse(response.body)
8+
9+
# Connect and authenticate with your Algolia app
10+
{{> snippets/init}}
11+
12+
# Save records in Algolia index
13+
{{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}
14+
15+
puts('Done!')
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import scala.io.Source
2+
import scala.concurrent.duration.Duration
3+
import scala.concurrent.{Await, ExecutionContextExecutor}
4+
5+
{{> snippets/import}}
6+
7+
import org.json4s.native.JsonMethods
8+
import org.json4s.jvalue2extractable
9+
10+
object Main {
11+
def main(args: Array[String]): Unit = {
12+
implicit val ec: ExecutionContextExecutor = scala.concurrent.ExecutionContext.global
13+
implicit val formats: org.json4s.Formats = org.json4s.DefaultFormats
14+
15+
// Fetch sample dataset
16+
val url = "https://dashboard.algolia.com/sample_datasets/movie.json"
17+
val result = Source.fromURL(url).mkString
18+
val records = JsonMethods.parse(result).extract[Seq[Map[String, Any]]]
19+
20+
// Connect and authenticate with your Algolia app
21+
{{> snippets/init}}
22+
23+
// Save records in Algolia index
24+
try {
25+
Await.result(
26+
{{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}},
27+
Duration(100, "sec")
28+
)
29+
} catch {
30+
case e: Exception => println(e)
31+
}
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Foundation
2+
import Core
3+
{{> snippets/import}}
4+
5+
Task {
6+
let url = URL(string: "https://dashboard.algolia.com/sample_datasets/movie.json")!
7+
let (data, _) = try await URLSession.shared.data(from: url)
8+
let movies = try JSONDecoder().decode([AnyCodable].self, from: data)
9+
10+
// Connect and authenticate with your Algolia app
11+
{{> snippets/init}}
12+
13+
do {
14+
// Save records in Algolia index
15+
_ = try await {{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}
16+
exit(EXIT_SUCCESS)
17+
} catch {
18+
print(error.localizedDescription)
19+
exit(EXIT_FAILURE)
20+
}
21+
}
22+
23+
RunLoop.current.run()

0 commit comments

Comments
 (0)