How do i show embed image from api - reactjs

"content": [
{
"type": "full_richtext",
"value": "<p data-block-key=\"a9kn6\">গেমটির কাহিনি গড়ে উঠেছে ইয়ারা নামের এক দেশকে কেন্দ্র করে। কাল্পনিক এ দেশটি শাসন করছে অ্যান্টন কাস্টিলো। নিষ্ঠুর এ শাসক সাধারণ মানুষের ওপর অত্যাচারের পাশাপাশি দেশ পরিচালনায়ও ব্যর্থ। আর তাই একসময়ের সমৃদ্ধ দেশটি অর্থনৈতিকভাবে এবং প্রযুক্তি ব্যবহারে অনেক পিছিয়ে পড়েছে। অত্যাচারী শাসকের কারণে প্রতিবেশী দেশগুলোও ইয়ারার সঙ্গে সম্পর্ক ত্যাগ করেছে। আর তাই অ্যান্টন কাস্টিলোর কাছ থেকে ইয়ারাকে মুক্ত করতে গেরিলাযুদ্ধে নামে ‘ডানি রোজাস’। এই বীর গেরিলাযোদ্ধার পরিচয়েও নিজেকে খুঁজে পাওয়া যাবে গেমটিতে।</p>",
"id": "c52035cd-c313-4e69-8123-98b2d2247228"
},
{
"type": "full_richtext",
"value": "<p data-block-key=\"a9kn6\"></p><embed alt=\"\" embedtype=\"image\" format=\"fullwidth\" id=\"4\"/><p data-block-key=\"6q2ur\"></p>",
"id": "516a6751-3788-44b9-a984-a81807baf455"
},
{
"type": "full_richtext",
"value": "<p data-block-key=\"a9kn6\">অ্যাকশননির্ভর গেমটির গ্রাফিকস খুবই উন্নত মানের। আর তাই গহিন জঙ্গলে গা ঢাকা দিয়ে অ্যান্টন কাস্টিলো বাহিনীর বিরুদ্ধে লড়াইয়ের সময় প্রতি পদে মিলবে যুদ্ধের আবহ। গেমটিতে তিনটি স্থানে গেরিলা হামলার সুযোগ মিলবে। তিনটি স্থানে হামলার সময় ভিন্ন পথে আলাদা চরিত্রের গেরিলা বাহিনীর মাধ্যমে শত্রুর বিরুদ্ধে লড়াই করতে হবে। এ সময় প্রধান ক্যাম্প পরিবর্তনের পাশাপাশি নতুন অস্ত্র, হামলার স্থান ও কৌশলে পরিবর্তন আনতে হওয়ায় প্রতিটি হামলার সময়ই মনে হবে নতুন গেম খেলছেন।</p><p data-block-key=\"4a3f3\"></p><p data-block-key=\"3j3go\">গেমটিতে গেরিলাযুদ্ধের সময় সহযোগী হিসেবে দেখা মিলবে বেশ কিছু চরিত্রের, যাদের মধ্যে জুয়ান কর্টেজ অন্যতম। যুদ্ধের ময়দানে আপনার মনোবল ঠিক রাখতে হাস্যকর বিভিন্ন ঘটনা নিয়মিত ঘটাবে মজার এ চরিত্র। এল টেগরের নাম না বললেই নয়, সাবেক এ গেরিলাযোদ্ধা অভিজ্ঞতা ও পরামর্শ দিয়ে যুদ্ধে জয়ী হতে ভূমিকা রাখবে। এ ছাড়া বিভিন্ন এলাকায় থাকা প্রধান ক্যাম্পে দেখা মিলবে বিভিন্ন চরিত্রের।<br/>ইউবিসফটের তৈরি গেমটি উইন্ডোজের পাশাপাশি প্লেস্টেশন ৪, প্লেস্টেশন ৫, এক্সবক্স ওয়ান, সিরিজ এক্স ও সিরিজ এস এবং গুগল স্ট্যাডিয়াতেও খেলা যাবে।<br/></p><h3 data-block-key=\"12s3k\"><b>কম্পিউটারে খেলতে কমপক্ষে যা লাগবে</b></h3><p data-block-key=\"1bcsg\">অপারেটিং সিস্টেম: উইন্ডোজ ১০<br/>প্রসেসর: ইন্টেল কোর আইসেভেন বা এমএমডি রাইজেন ফাইভ<br/>মেমোরি: ১৬ গিগাবাইট র‍্যাম<br/>গ্রাফিকস: ৮ গিগাবাইটের এএমডি আরএক্স ৪৬০ অথবা এনভিডিয়া জিফোর্স জিটিএক্স ৯৬০।<br/>জায়গা: ৬০ গিগাবাইট</p>",
"id": "7754100b-d91b-477c-92da-45cee0962dca"
}
],
I have this type of array from api.
I can showed the string data, but cannot show the embedded image.
I am using below code for shown the string from the api. Help me to find out the problem, Thanks to All
<div className='Details-news-content'>
{
newss.content.map((data, index) => (
<p key={index} dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(data.value) }}>
</p>))}
</div>

It's codded in Wagtail.
In wagtail API if the content's Richtext is not serialized the API will show wired type HTML format in API.
in this case, add this code in blocks.py
from wagtail.core import blocks
from wagtail.core.templatetags.wagtailcore_tags import richtext
class RichtextBlock(blocks.RichTextBlock):
def get_api_representation(self, value, context=None):
return richtext(value.source)
Source: https://learnwagtail.com/tutorials/headless-cms-serializing-richtext-blocks/

Related

Added Custom Claim, showing in ID token missing in Access token

I have a .NET Core Identity Provider (which also uses IdentityServer4) which authenticates SPA applications with Azure AD. I am adding an "oid" claim with the object identifier value received from Azure. The problem is that from the SPA application I can see the "oid" claim in the ID token but cannot see it in the access token. I need the oid in the access token as well. Here is the relevant code:
Startup.cs
services.AddAuthentication()
.AddCookie("Cookies", options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(10);
})
.AddOpenIdConnect(ActiveDirectoryTenants.TenantA, ActiveDirectoryTenants.TenantA, options => Configuration.Bind("TenantAAzureAd", options))
.AddOpenIdConnect(ActiveDirectoryTenants.TenantB, ActiveDirectoryTenants.TenantB, options => Configuration.Bind("TenantBAzureAd", options));
AddActiveDirectoryOpenIdConnectOptions(services, ActiveDirectoryTenants.TenantA);
AddActiveDirectoryOpenIdConnectOptions(services, ActiveDirectoryTenants.TenantB);
I have a common function to add other options to these configurations. I tried to add the oid claim in OnTokenValidated but didn't receive the oid claim in the access token.
protected virtual void AddActiveDirectoryOpenIdConnectOptions(IServiceCollection services, string tenant)
{
services.Configure<OpenIdConnectOptions>(tenant, options =>
{
options.Authority = options.Authority + "/v2.0/";
options.TokenValidationParameters.ValidateIssuer = false;
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProvider = ctx =>
{
ctx.ProtocolMessage.LoginHint = ctx.Properties.GetString("username");
return Task.CompletedTask;
},
OnTokenValidated = ctx =>
{
//Maybe need to add oid here???
}
};
});
}
The oid claim is being added after successfully logging in to Azure AD.
AccountController.cs
public async Task<IActionResult> ExternalLoginCallback(string returnUrl, string remoteError = null, string openIdScheme = null)
{
var authResult = await HttpContext.AuthenticateAsync(openIdScheme ?? ActiveDirectoryTenants.TenantA);
var externalUser = authResult.Principal;
var claims = externalUser.Claims.ToList();
var applicationUser = //gets the user based on the email found in claims, omitted for brevity
await userManager.AddClaimAsync(applicationUser, new Claim("oid", claims.First(x => x.Type == http://schemas.microsoft.com/identity/claims/objectidentifier).Value));
await signInManager.SignInAsync(applicationUser, false, "AzureAD");
return Redirect("~/");
}
The ID token received in the SPA application (note the oid claim):
{
"nbf": xxx,
"exp": xxx,
"iss": "https://localhost:3000",
"aud": "xxx-spa-test",
"iat": xxx,
"at_hash": "",
"s_hash": "",
"sid": "",
"sub": "guid",
"auth_time": 1620026953,
"idp": "AzureAD",
"display_name": "Test User",
"oid": "guid",
"role": [
"Staff",
],
"name": "test#azureaddomain",
"amr": [
"external"
]
}
The access token received in the SPA application (note the missing oid claim):
{
"nbf": xxx,
"exp": xxx,
"iss": "https://localhost:3000",
"aud": [
"https://localhost:3000/resources",
"xxx-api-test-scope"
],
"client_id": "xxx-spa-test",
"sub": "guid",
"auth_time": 1620026953,
"idp": "AzureAD",
"role": [
"Staff",
],
"name": "test#azureaddomain",
"scope": [
"openid",
"profile",
"xxx-api-test-scope"
],
"amr": [
"external"
]
}
For the claim to end up in the access token, you need to add a ApiScope and add the Userclaim name to it. Alternatively, add an ApiScope and an ApiResource that contains the UserClaim.
Like
var apiresource1 = new ApiResource()
{
Name = "apiresource1", //This is the name of the API
ApiSecrets = new List<Secret>
{
new Secret("myapisecret".Sha256())
},
Description = "This is the order Api-resource description",
Enabled = true,
DisplayName = "Orders API Service",
Scopes = new List<string> { "apiscope1"},
UserClaims = new List<string>
{
//Custom user claims that should be provided when requesting access to this API.
//These claims will be added to the access token, not the ID-token!
"apiresource1-userclaim3",
}
};
See my answer here for more details
To complement this answer, I write a blog post that goes into more detail about this topic:
IdentityServer – IdentityResource vs. ApiResource vs. ApiScope

Cakephp .json ext giving Missing Method

I have a dropdown that onChange I use ajax to load some .json from a method in a controller.
However I am getting error 404 returned
If I remove the .json extension I get error 500 missing template which I have not been able to resolve either. I have tried different solution. I would rather use the .json ext anyway and let cakephp return the correct formatted JSON.
Missing Method in StrategiesConditionsController
Cake\Controller\Exception\MissingActionException
Error The action conditions.json is not defined in StrategiesConditionsController
Create StrategiesConditionsController::conditions.json() in file: src/Controller/StrategiesConditionsController.php.
From reading the doc a couple of times I am sure I have the routes correct.
routes.php
<?php
/**
* Routes configuration.
*
* In this file, you set up routes to your controllers and their actions.
* Routes are very important mechanism that allows you to freely connect
* different URLs to chosen controllers and their actions (functions).
*
* It's loaded within the context of `Application::routes()` method which
* receives a `RouteBuilder` instance `$routes` as method argument.
*
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* #copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* #link https://cakephp.org CakePHP(tm) Project
* #license https://opensource.org/licenses/mit-license.php MIT License
*/
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
/*
* The default class to use for all routes
*
* The following route classes are supplied with CakePHP and are appropriate
* to set as the default:
*
* - Route
* - InflectedRoute
* - DashedRoute
*
* If no call is made to `Router::defaultRouteClass()`, the class used is
* `Route` (`Cake\Routing\Route\Route`)
*
* Note that `Route` does not do any inflections on URLs which will result in
* inconsistently cased URLs when used with `:plugin`, `:controller` and
* `:action` markers.
*/
/** #var \Cake\Routing\RouteBuilder $routes */
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $routes) {
$routes->setExtensions(['json']);
$routes->resources('StrategiesConditions', [
'map' => [
'conditions' => [
'action' => 'conditions',
'method' => 'post'
]
],
'only' => ['conditions']
]);
});
$routes->scope('/', function (RouteBuilder $builder) {
// Register scoped middleware for in scopes.
$builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
'httpOnly' => true,
]));
/*
* Apply a middleware to the current route scope.
* Requires middleware to be registered through `Application::routes()` with `registerMiddleware()`
*/
$builder->applyMiddleware('csrf');
/*
* Here, we are connecting '/' (base path) to a controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, templates/Pages/home.php)...
*/
$builder->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
/*
* ...and connect the rest of 'Pages' controller's URLs.
*/
$builder->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
/*
* Connect catchall routes for all controllers.
*
* The `fallbacks` method is a shortcut for
*
* ```
* $builder->connect('/:controller', ['action' => 'index']);
* $builder->connect('/:controller/:action/*', []);
* ```
*
* You can remove these routes once you've connected the
* routes you want in your application.
*/
$builder->fallbacks();
});
Router::prefix('admin', function (RouteBuilder $routes) {
$routes->connect('/', ['controller' => 'Users', 'action' => 'index']);
$routes->fallbacks(DashedRoute::class);
});
/*
* If you need a different set of middleware or none at all,
* open new scope and define routes there.
*
* ```
* $routes->scope('/api', function (RouteBuilder $builder) {
* // No $builder->applyMiddleware() here.
* // Connect API actions here.
* });
* ```
*/
strategiesConditionsController
public function conditions()
{
$this->request->allowMethod(['post']);
if ($this->request->is('ajax')) {
$this->response = $this->response->withDisabledCache();
}
$strategy_id = $this->request->getData('strategy_id');
$strategiesConditions = $this->StrategiesConditions->find('all', [
'where' => ['strategy_id' => $strategy_id],
]);
$this->viewBuilder()->setOption('serialize', ['strategiesConditions']);
}
Cakephp console has 8 routes showing
Route name URI template Defaults
pages:display /
{
"controller": "Pages",
"action": "display",
"0": "home",
"plugin": null
}
pages:display /pages/*
{
"controller": "Pages",
"action": "display",
"plugin": null
}
_controller:index /{controller}
{
"action": "index",
"plugin": null
}
_controller:_action /{controller}/{action}/*
{
"plugin": null,
"action": "index"
}
admin:users:index /admin
{
"controller": "Users",
"action": "index",
"prefix": "Admin",
"plugin": null
}
admin:_controller:index /admin/{controller}
{
"action": "index",
"prefix": "Admin",
"plugin": null
}
admin:_controller:_action /admin/{controller}/{action}/*
{
"prefix": "Admin",
"plugin": null,
"action": "index"
}
strategiesconditions:conditions /strategies-conditions/conditions
{
"controller": "StrategiesConditions",
"action": "conditions",
"_method": "post",
"plugin": null
}
Update from the history tab in debug kit
Request
Routing Params
controller StrategiesConditions
action conditions.json
pass (empty)
plugin (null)
_matchedRoute /{controller}/{action}/*
_ext (null)
Post data
strategy_id 11
Query string
No querystring data.
Cookie
csrfToken dd12f852560a384d39206e511f1857f77f71da2eadb023a6c67ae346
PHPSESSID 3d2jt7dgpo09af8b1908quljpn
Matched Route
template /{controller}/{action}/*
and the routes tab:
Routes
Toggle debugkit internal routes
Route name URI template Defaults
pages:display /
{
"controller": "Pages",
"action": "display",
"0": "home",
"plugin": null
}
pages:display /pages/*
{
"controller": "Pages",
"action": "display",
"plugin": null
}
_controller:index /{controller}
{
"action": "index",
"plugin": null
}
_controller:_action /{controller}/{action}/*
{
"plugin": null,
"action": "index"
}
admin:users:index /admin
{
"controller": "Users",
"action": "index",
"prefix": "Admin",
"plugin": null
}
admin:_controller:index /admin/{controller}
{
"action": "index",
"prefix": "Admin",
"plugin": null
}
admin:_controller:_action /admin/{controller}/{action}/*
{
"prefix": "Admin",
"plugin": null,
"action": "index"
}
strategiesconditions:conditions /strategies-conditions/conditions
{
"controller": "StrategiesConditions",
"action": "conditions",
"_method": "post",
"plugin": null
}
Request URL
http://localhost:8888/trading-journal/strategies-conditions/conditions.json
Application.php
// Add routing middleware.
// If you have a large number of routes connected, turning on routes
// caching in production could improve performance. For that when
// creating the middleware instance specify the cache config name by
// using it's second constructor argument:
// `new RoutingMiddleware($this, '_cake_routes_')`
->add(new RoutingMiddleware($this))
'method' => 'POST' should have been uppercase. This was not document. A pull request has been made to change so it is not case sensitive.
$routes->scope('/', function (RouteBuilder $routes) {
$routes->setExtensions(['json']);
$routes->resources('StrategiesConditions', [
'map' => [
'conditions' => [
'action' => 'conditions',
'method' => 'POST'
]
],
'only' => ['conditions']
]);
});

Flat file to json conversion using beanIo

I am trying to parse a fixedlength flat file using beanIo to json
Code:
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Map;
import org.beanio.BeanIOConfigurationException;
import org.beanio.BeanReader;
import org.beanio.StreamFactory;
import org.junit.Test;
import com.google.gson.Gson;
public class EmployeeBeanIOHandlerTest {
#Test
public void testHandleEmployee() {
// mapping pattern file
String mappingPatternFile = "pattern-mapping.xml";
// data file (csv)
String objectFile = "employee.csv";
// stream name defined in pattern mapping file
String streamName = "empData";
Gson gson = new Gson();
BeanReader beanReader = null;
Reader reader = null;
StreamFactory factory = null;
InputStream in = null;
try {
System.out.println("## RESULT FOR " + objectFile + " ##");
// create a StreamFactory
factory = StreamFactory.newInstance();
// load the setting file
in = this.getClass().getClassLoader()
.getResourceAsStream(mappingPatternFile);
// get input stream reader of object file (data file)
reader = new InputStreamReader(this.getClass().getClassLoader()
.getResourceAsStream(objectFile));
// load input stream to stream factory
factory.load(in);
beanReader = factory.createReader(streamName, reader);
Map<?, ?> record = null;
while ((record = (Map<?, ?>) beanReader.read()) != null) {
System.out.println(beanReader.getRecordName() + ": "
+ gson.toJson(record));
}
} catch (BeanIOConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
if (beanReader != null) {
beanReader.close();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
However the output i see:
header: {"id":"Header","date":"01012013"}
emp: {"lastName":"Lilik","title":"Senior Developer","hireDate":"Oct 1, 2009
12:00:00 AM","salary":7500000,"firstName":"Robertus"}
emp: {"lastName":"Doe","title":"Architect","hireDate":"Jan 15, 2008 12:00:00 AM","salary":8000000,"firstName":"Jane"}
emp: {"lastName":"Anderson","title":"Manager","hireDate":"Mar 18, 2006 12:00:00 AM","salary":9000000,"firstName":"Jon"}
trailer: {"id":"Trailer","count":"3"}
Which generates separate json object for each record found.
Reference site:
http://www.sourcefreak.com/2013/06/painless-flat-file-parsing-with-beanio/
Below is my requirement:
I want to have a consolidated Json file.
In case of duplicate record it should form an json array.
I would appreciate a help on this.
This answer is based on the data and pattern-mapping.xml file found in the link provided by the OP.
Data:
Header,01012013
Robertus,Lilik,Senior Developer,"75,000,00",10012009
Jane,Doe,Architect,"80,000,00",01152008
Jon,Anderson,Manager,"90,000,00",03182006
Footer,3
Mapping File:
This is the modified pattern-mapping.xml file. Note the use of a <group> element (myGroup) to encapsulate everything into a single group, which will force the BeanReader to read everything in one go. I also changed the maxOccurs to be 1 (one) for both the Header and the Footer records. Also, added the collection="list" attribute to theemp` record
<?xml version='1.0' encoding='UTF-8' ?>
<beanio xmlns="http://www.beanio.org/2012/03"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">
<stream name="empData" format="csv">
<group name="myGroup" class="map">
<record name="header" class="map" ridLength="0-2" maxOccurs="1">
<field name="id" rid="true" maxOccurs="1" literal="Header" />
<field name="date" />
</record>
<record name="emp" class="map" ridLength="4-5" collection="list">
<field name="firstName" />
<field name="lastName" />
<field name="title" />
<field name="salary" type="java.math.BigDecimal" format="#,###,###,00" />
<field name="hireDate" type="java.util.Date" format="MMddyyyy" minOccurs="0" />
</record>
<record name="trailer" class="map" ridLength="2" maxOccurs="1">
<field name="id" />
<field name="count" />
</record>
</group>
</stream>
</beanio>
Using the test case supplied and the modified mapping file, we get this result (reformatted by me):
myGroup: {
"trailer": {
"count": "3",
"id": "Footer"
},
"header": {
"date": "01012013",
"id": "Header"
},
"emp": [
{
"firstName": "Robertus",
"lastName": "Lilik",
"hireDate": "Oct 1, 2009 12:00:00 AM",
"title": "Senior Developer",
"salary": 7500000
},
{
"firstName": "Jane",
"lastName": "Doe",
"hireDate": "Jan 15, 2008 12:00:00 AM",
"title": "Architect",
"salary": 8000000
},
{
"firstName": "Jon",
"lastName": "Anderson",
"hireDate": "Mar 18, 2006 12:00:00 AM",
"title": "Manager",
"salary": 9000000
}
]
}
Hope this helps

Show Post with its comments using ng-repeat not working

I am facing an issue in building below format in MVC using AngularJS
I am showing the posts from the facebook using api with ng-repeat which consists of a Image.
Now the problem i am facing to show the comments for each of the posts using nested ng-repeat.
Dont have any idea how to proceed with it.
ng-repeat nested sample
http://plnkr.co/edit/NGWOZ4?p=info
I think you need something like that?
<body ng-controller="programController">
<div class="panel-group" ng-repeat="row in osk.programs">
<div>{{row.day}}</div>
<div>
<h4>{{row.head}}</h4>
<p>{{row.content}}</p>
<aside class="warning" ng-repeat="row in row.extra">
<strong>Ekstra Gezi:</strong> {{row.exhead}} <span>{{row.price}}</span>
<p>{{row.excontent}}</p>
</aside>
</div>
</div>
</body>
var myApp = angular.module('myApp', []);
myApp.controller('programController', function($scope) {
var json = {
"programs":
[
{
"day":"1.GÜN",
"head":"İstanbul-Amsterdam",
"content":"Gezimizin ilk günü, kalkıştan iki saat önce Sabiha Gökçen Havalimanı Dış Hatlar Terminali Etstur Kontuarı önünde buluşarak başlıyor. Pegasus Hava Yolları tarifeli seferi ile Amsterdam’a uçuş. Havalimanında bizleri bekleyen otobüsümüz ile Amsterdam şehir turu. Bu gezide Tarihi Tren İstasyonu, Dam Meydanı, Ajax Arena Stadı, Tarihi Çiçek Pazarı ve yel değirmenleri görülecek yerlerden bazıları. Misafirlerimiz ekstra olarak düzenlenecek Amsterdam Kanallar Gezisi'ne katılabilirler. Şehir turu sonrası otele transfer. Öğleden sonra serbest zaman. Geceleme otelimizde.",
"extra":
[
{
"exhead":"Amsterdam kanallar gezisi",
"price":"55 Euro",
"excontent":"Amsterdam’a farklı açıdan bakmak isteyenler için üzeri cam olan teknelerle Amsterdam’ın simgesi haline gelmiş kanalları arasında eğlenceli gezi sizi bekliyor."
},
{
"exhead":"Tekne Turu",
"price":"15 Euro",
"excontent":"Amsterdam’a farklı açıdan bakmak isteyenler için üzeri"
}
]
},
{
"day":"2.GÜN",
"head":"Amsterdam-Paris",
"content":"Gezimizin ilk günü, kalkıştan iki saat önce Sabiha Gökçen Havalimanı Dış Hatlar Terminali Etstur Kontuarı önünde buluşarak başlıyor. Pegasus Hava Yolları tarifeli seferi ile Amsterdam’a uçuş. Havalimanında bizleri bekleyen otobüsümüz ile Amsterdam şehir turu. Bu gezide Tarihi Tren İstasyonu, Dam Meydanı, Ajax Arena Stadı, Tarihi Çiçek Pazarı ve yel değirmenleri görülecek yerlerden bazıları. Misafirlerimiz ekstra olarak düzenlenecek Amsterdam Kanallar Gezisi'ne katılabilirler. Şehir turu sonrası otele transfer. Öğleden sonra serbest zaman. Geceleme otelimizde.",
"extra":
[
{
"exhead":"Amsterdam kanallar gezisi",
"price":"55 Euro",
"excontent":"Amsterdam’a farklı açıdan bakmak isteyenler için üzeri cam olan teknelerle Amsterdam’ın simgesi haline gelmiş kanalları arasında eğlenceli gezi sizi bekliyor."
},
{
"exhead":"Tekne Turu",
"price":"15 Euro",
"excontent":"Amsterdam’a farklı açıdan bakmak isteyenler için üzeri"
}
]
}
]
};
$scope.osk = json;
});

Specifying bigquery table schema that resides on a file in a multipart http request

I have a text file schema.txt in which the schema for the table that I want to create is defined.
I want to include this file in the multipart HTTP request that I'm using to create my table.
How do I specify the schema.txt file in the multipart HTTP request?
Below is what I'm currently doing (not working though):
def loadTable(service, projectId, datasetId, targetTableId, sourceCsv, filenm):
try:
jobCollection = service.jobs()
jobData = {
'projectId': projectId,
'configuration': {
'load': {
'sourceUris': [sourceCsv],
'schema': filenm,
'destinationTable': {
'projectId': projectId,
'datasetId': datasetId,
'tableId': targetTableId
},
'createDisposition': 'CREATE_IF_NEEDED',
'writeDisposition': 'WRITE_TRUNCATE',
'encoding': 'UTF-8'
}
}
}
Where filenm will be 'schema.txt'.
I know I can specify the schema directly as:
'schema': {
'fields': [
{
'name': 'level',
'type': 'STRING',
},
{
'name': 'message',
'type': 'STRING',
}
]
},
But instead I want to specify the file containing the schema.
Hmm, not sure why you need a "multipart HTTP request" unless you are ingesting directly from a file. Here you are specifying a CSV input, indicating a Cloud Storage object.
See here for more info:
https://developers.google.com/bigquery/docs/developers_guide#storageimport
In any case, this is not really a BigQuery question, more a Python question.. do you mean this?
import json
def loadTable(project_id, dataset_id, target_table, source_csv, filename):
file = open(filename, 'r')
schema = file.read()
file.close()
schema_json = json.loads('{%s}' % schema)
job_data = {
"projectId": project_id,
"configuration": {
"load": {
"sourceUris": [source_csv],
"schema": schema_json,
"destinationTable": {
"projectId": project_id,
"datasetId": dataset_id,
"tableId": target_table
},
"createDisposition": "CREATE_IF_NEEDED",
"writeDisposition": "WRITE_TRUNCATE",
"encoding": "UTF-8"
}
}
}
print json.dumps(job_data, indent=2)
loadTable('project_id', 'dataset_id', 'target_table', 'source_csv', '/tmp/schema.txt')

Resources