New post

Encontrar

Digest
· Dec 30, 2024

InterSystems 开发者出版物,十二月 23 - 29, 2024,摘要

Article
· Dec 29, 2024 4m read

Creating a REST client to get Tracks from Spotify REST API - Part1 Check out token

Git link: https://github.com/ecelg/InterSystems-IRIS-as-a-Spotify-REST-client

 

Recently, I come up an idea in my mind that how can I put my playlist on IRIS.🧐

At the same time, I was told to pay for my Spotify subscription💸💸... ooo.. how about to get some data from the Spotify API... so I started to do study about it.

 

Like most of the development, let's start from Documentation of  the API https://developer.spotify.com/documentation/web-api

In order to get the data, i am required to request an access token from for the token endpoint URL.🧐

curl -X POST "https://accounts.spotify.com/api/token" \
     -H "Content-Type: application/x-www-form-urlencoded" \
     -d "grant_type=client_credentials&client_id=your-client-id&client_secret=your-client-secret"

but before that I should check out my client_id and my client_secret from the Dashboard 

by Create app

then

check the api

check the setting

copy the client_id and client_secret

 


OK every thing is ready😀 Let's go!!!!!😁

1. Setup a  Persistent class to store the API information

replace the code as following and save

Class rest.class.apiinfo Extends (%Persistent, %JSON.Adaptor)
{

Property apiname As %String;
Property clientid As %String(%JSONFIELDNAME = "client_id");
Property clientsecret As %String(%JSONFIELDNAME = "client_secret");
Property granttype As %String(%JSONFIELDNAME = "grant_type");
Property tokentype As %String(%JSONFIELDNAME = "token_type");
Property accesstoken As %String(%JSONFIELDNAME = "access_token", MAXLEN = 200);
Property expiresin As %String(%JSONFIELDNAME = "expires_in");
Property refreshtoken As %String(%JSONFIELDNAME = "refresh_token", MAXLEN = 200);
Property authurl As %String(MAXLEN = 100);
Property authheader As %String(MAXLEN = 100);
Property apiurl As %String(MAXLEN = 100);
Property refreshbefore As %String;
Property updateat As %String;
ClassMethod checkRowid(apiname As %String = "") As %Integer [ Language = objectscript ]
{
	//w ##class(rest.class.apiinfo).checkRowid("Spotify")
	set rtn=0
	set rowid=""
	&sql(select id into :rowid from rest_class.apiinfo where apiname=:apiname )
	//w rowid,!
	if rowid'="" set rtn=rowid
	return rtn
}
}

 

 


2. Insert the API information into the table

insert the api information into the table

example SQL

insert into  rest_class.apiinfo
(apiname, apiurl, authurl, clientid, clientsecret, granttype, authheader)
values
('Spotify', 'https://api.spotify.com/v1', 'https://accounts.spotify.com/api/token', 'b43bf136********************', '45ffde***************', 'client_credentials', 'application/x-www-form-urlencoded')

verify it

SELECT
ID, accesstoken, apiname, apiurl, authheader, authurl, clientid, clientsecret, expiresin, granttype, refreshbefore, refreshtoken, tokentype, updateat
FROM rest_class.apiinfo

 


3. Setup a REST client to check out the token

It seems there are many options, 

Option 1

Creating REST Operations in Productions, but how to insert the header  "Content-Type: application/x-www-form-urlencoded" 😓. Let me check further Using the HTTP Outbound Adapter, ok.... seems... need to check further... Using the HTTP Response, ... oooo sorry... really cannot understand as there no step by step example for me.... give up.😭  

Option 2

Python request, seems much easier to understand, let's start with option 2

 

3a. Install the requests library

If you need to setup your own python for IRIS after version 2024.3 you may reference to Use the Flexible Python Runtime Feature for IRIS on Windows Server

In the powershell type the following to install the requests library

python -m pip install requests

In the powershell type the following to install the iris library

python -m pip install iris

3b. Write a  %RegisteredObject class for check out the token

create the folder utli under the folder rest

create the class requestUtli.cls 

write the class method checkoutToken 

Class rest.utli.requestUtli Extends %RegisteredObject
{

ClassMethod checkoutToken(apiname = "", withgrandtype = 1) As %String [ Language = python ]
{
	#w ##class(rest.utli.requestUtli).checkoutToken("Spotify")
	import requests
	import json
	import iris
	
	# get the apiinfo object by apiname
	rowid=iris.cls('rest.class.apiinfo').checkRowid(apiname)
	a=iris.cls('rest.class.apiinfo')._OpenId(rowid)
	
	# parameter perparation
	api_baseurl=a.authurl
	params=""
	if withgrandtype==1:
		#print ('grandtype ='+a.granttype)
		params="grant_type="+a.granttype+"&client_id="+a.clientid+"&client_secret="+a.clientsecret
	else:
		#print ('without grandtype')
		params="client_id="+a.clientid+"&client_secret="+a.clientsecret
	contenttype=a.authheader
	headers={"Content-Type":contenttype}
	api_url=api_baseurl+"?"+params
	#print(api_url)
	del a
	
	# post it
	response = requests.post(api_url,headers=headers, verify=False)
	#print(response)
	if response.status_code==200:
		return json.dumps(response.json())
	else:
		return response.status_code
}

}

Save the code

 


4. Open a terminal to test if it is working or not

run the following command for testing

w ##class(rest.utli.requestUtli).checkoutToken("Spotify")

Yeah!! the token is check out successfully!!!!😁


2 Comments
Discussion (2)2
Log in or sign up to continue
Article
· Dec 29, 2024 2m read

第六十章 假脱机设备 - WRITE 命令

第六十章 假脱机设备 - WRITE 命令

WRITE 命令

要将一行写入 ^SPOOL 全局变量,请发出 WRITE 命令,以行终止符字符结尾。例如

   /* Writing to the ^SPOOL global */
   OPEN 2 
   USE 2 
     WRITE "First line of text",!
     WRITE "Second line of text",!
   CLOSE 2

   /* Displaying the ^SPOOL global */
   WRITE ^SPOOL(1,1),^SPOOL(1,2)

每行都以行终止符(感叹号)结尾,并存储在单独的全局节点中。

但是,在生成单个打印行时,可能需要使用多个 WRITE 命令;如果 WRITE 不包含行终止符,则下一个 WRITE 命令将附加到同一打印行。两者都写入同一个全局节点。此行保存在缓冲区中,在发出行终止字符或关闭后台处理程序设备之前,不会写入后台处理程序全局。

Discussion (0)1
Log in or sign up to continue
Article
· Dec 28, 2024 1m read

Como chegar no Hall da Fama do Portal de Ideias

Oi comunidade!

Nós esperamos que você saiba que quando tem uma ideia interessante sobre os produtos ou serviços InterSystems, você deve publicá-la no Ideas Portal. E esses membros da Comunidade de Desenvolvedores que implementam as ideias propostas são adicionados ao "Hall of Fame". Você quer ser aceito no InterSystems Ideas Hall of Fame? Leia a seguir para aprender como você pode entrar na lista.

 

Hall of Fame foi criado para fazer um tributo aos heróis que implementaram ideias do Portal de Ideias. Ele contém nomes de ideias implementadas, nomes dos desenvolvedores que realizaram elas e a lista de projetos.

Para ser mencionado nesta página:

1. Escolha uma ideia com tag Community Opportunity que você goste e queira implementar.

2. Implemente essa ideia e publique o projeto no portal Open Exchange portal. Preencha o campo "Ideas Portal" ao publicar sua aplicação com um link para a sua ideia implementada.

OU

Publique um comentário na ideia implementadda no Portal de Ideias com um link para sua aplicação no  Open Exchange.

3. O Time da Comunidade vai revisar seu app publicado. Após isso, bem vindo ao Hall of Fame!

Boa sorte nos seus projetos para implementar ideias!

Discussion (0)1
Log in or sign up to continue
Article
· Dec 27, 2024 2m read

第五十九章 假脱机设备

第五十九章 假脱机设备

介绍

IRIS数据平台使能够将打印输出直接发送到您的打印机或屏幕,或将其保留在后台打印中以供以后打印。IRIS 假脱机独立于您的操作系统执行的假脱机。

IRIS 中的假脱机是一种技术,可让自动将程序的输出保存在 ^SPOOL 下标全局中,而不是立即打印。可以通过将 ^SPOOL 全局的内容发送到打印机来稍后打印输出。本页介绍使用此假脱机工具的两种方法:使用 ObjectScript 命令(OPEN、USE、WRITE、CLOSE)或使用 %IS%SPOOL 实用程序。

打开和使用假脱机设备

要将输出发送到当前命名空间中的 spool 全局变量,请打开 spooler 并将其指定为输出设备。

Discussion (0)1
Log in or sign up to continue