Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

guides: add onboarding snippets #3844

Open
wants to merge 1 commit into
base: feat/guides-gen
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions templates/csharp/guides/search/saveObjectsMovies.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Text.Json;
using System.Net.Http;
using System.Collections.Generic;

{{> snippets/import}}

class Program
{
public static void Main(string[] args)
{
// read json file from url
var url = "https://dashboard.algolia.com/sample_datasets/movie.json";
var httpClient = new HttpClient();
var response = httpClient.GetAsync(url).Result;
var content = response.Content.ReadAsStringAsync().Result;

// parse json
var movies = JsonSerializer.Deserialize<List<dynamic>>(content);

// initiate client and index
{{> snippets/init}}

// push data to algolia
try
{
var result = {{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}.Result;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
43 changes: 43 additions & 0 deletions templates/go/guides/search/saveObjectsMovies.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"encoding/json"
"fmt"
"net/http"
)

{{> snippets/import}}

func main() {
// read json file
url := "https://dashboard.algolia.com/sample_datasets/movie.json"
response, err := http.Get(url)

if err != nil {
fmt.Println("Could not open url")
return
}

defer response.Body.Close()

// parse json file to Movie struct
var movies []map[string]interface{}
err = json.NewDecoder(response.Body).Decode(&movies)

if err != nil {
fmt.Println("Could not decode body")
return
}

// initiate client
{{> snippets/init}}

// push data to algolia
result, err := {{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}
if err != nil {
fmt.Println(err)
return
}

fmt.Println(fmt.Sprintf("Done! Uploaded records in %d batches.", len(result)))
}
28 changes: 28 additions & 0 deletions templates/java/guides/search/saveObjectsMovies.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.List;

{{> snippets/import}}

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
public static void main(String[] args) throws Exception {
// Fetch sample dataset
URL url = new URI("https://dashboard.algolia.com/sample_datasets/movie.json").toURL();
InputStream stream = url.openStream();
ObjectMapper mapper = new ObjectMapper();
List<JsonNode> result = mapper.readValue(stream, new TypeReference<List<JsonNode>>() {});
stream.close();

// Connect and authenticate with your Algolia app
{{> snippets/init}}

// Save records in Algolia index
{{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}
client.close();
}
}
20 changes: 20 additions & 0 deletions templates/kotlin/guides/search/saveObjectsMovies.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.example
{{> snippets/import}}
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import java.net.URI

suspend fun main() {
val url = URI.create("https://dashboard.algolia.com/sample_datasets/movie.json")
val json = url.toURL().readText()
val records: List<JsonObject> = Json.decodeFromString(ListSerializer(JsonObject.serializer()), json)

{{> snippets/init}}

try {
{{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}
} catch(e: Exception) {
println(e.message)
}
}
16 changes: 16 additions & 0 deletions templates/php/guides/search/saveObjectsMovies.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
require(__DIR__."/vendor/autoload.php");
{{> snippets/import}}

// Fetch sample dataset
$url = "https://dashboard.algolia.com/sample_datasets/movie.json";
$response = file_get_contents($url);
$records = json_decode($response, true);

// Connect and authenticate with your Algolia app
{{> snippets/init}}

// Save records in Algolia index
{{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}

print('Done!');
12 changes: 12 additions & 0 deletions templates/python/guides/search/saveObjectsMovies.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import requests
{{> snippets/import}}

# Fetch sample dataset
url = "https://dashboard.algolia.com/sample_datasets/movie.json"
records = requests.get(url).json()

# Connect and authenticate with your Algolia app
{{> snippets/init}}

# Save records in Algolia index
{{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}
15 changes: 15 additions & 0 deletions templates/ruby/guides/search/saveObjectsMovies.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'uri'
{{> snippets/import}}

# Fetch sample dataset
uri = URI('https://dashboard.algolia.com/sample_datasets/movie.json')
response = Net::HTTP.get_response(uri)
records = JSON.parse(response.body)

# Connect and authenticate with your Algolia app
{{> snippets/init}}

# Save records in Algolia index
{{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}

puts('Done!')
33 changes: 33 additions & 0 deletions templates/scala/guides/search/saveObjectsMovies.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import scala.io.Source
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContextExecutor}

{{> snippets/import}}

import org.json4s.native.JsonMethods
import org.json4s.jvalue2extractable

object Main {
def main(args: Array[String]): Unit = {
implicit val ec: ExecutionContextExecutor = scala.concurrent.ExecutionContext.global
implicit val formats: org.json4s.Formats = org.json4s.DefaultFormats

// Fetch sample dataset
val url = "https://dashboard.algolia.com/sample_datasets/movie.json"
val result = Source.fromURL(url).mkString
val records = JsonMethods.parse(result).extract[Seq[Map[String, Any]]]

// Connect and authenticate with your Algolia app
{{> snippets/init}}

// Save records in Algolia index
try {
Await.result(
{{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}},
Duration(100, "sec")
)
} catch {
case e: Exception => println(e)
}
}
}
23 changes: 23 additions & 0 deletions templates/swift/guides/search/saveObjectsMovies.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Foundation
import Core
{{> snippets/import}}

Task {
let url = URL(string: "https://dashboard.algolia.com/sample_datasets/movie.json")!
let (data, _) = try await URLSession.shared.data(from: url)
let movies = try JSONDecoder().decode([AnyCodable].self, from: data)

// Connect and authenticate with your Algolia app
{{> snippets/init}}

do {
// Save records in Algolia index
_ = try await {{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}
exit(EXIT_SUCCESS)
} catch {
print(error.localizedDescription)
exit(EXIT_FAILURE)
}
}

RunLoop.current.run()
Loading