Find and update UK๐Ÿ‡ฌ๐Ÿ‡ง company information | SupeRails #187

31/08/2024
Transcript
Hello friends. So in a previous episode they learned how to connect via a i to the French National Registry of Companies. And we built small revenue rail application where we have the possibility to create a company. We have this information field that cannot be manually filled in, but we also have a search field. And as you search, you will see information pop up about the different companies. So, uh, let's say this company matches my search result. I will click on this company and this information gets populated in my info field. I can click create company and the company is created with the, the name and the registration number, uh, in the info field. So looks good. But in this episode we're going to focus on, uh, the UK data. So previously we did France, now do the same, but for UK. And uh, here they have this company's house website. And on this website they have the API documentation and you can, uh, go to search for companies. So we have this endpoint, get the API slash search slash companies. We have a Q3 parameter and we are going to make a a p request. But for it to work we'll also need to have an API key. So here you would need to go to the developer hub and create an account. I've already done it and I'm going to go and create an application. Actually, don far it is a free service. You don't have to pay, you just need an account. So create an application. Uh, super rails testing. Uh, now last time I tried test mode. I didn't manage to make API calls. So I'm going to go with live mode, go into the application, create new API, key name, description, rest create key. Okay, here I have the API key. Now the copy doesn't seem to be working, it doesn't work for me, so I'll just have to manually copy and let's copy it somewhere. So I'm going to have to make requests to this URL. Let's first try making not in Ruben Rails, just in the, uh, terminal using the CURL or curl whatever, whatever it's called. Uh, so I'm going to say curl, uh, header authorization. And uh, it'll be basic and I will input this API key and then have the URL. Let's try making the request. Okay, inval authorization header. So it in first because we need to encode this, uh, key to encode the ski will need to type, uh, something like, uh, echo, uh, M and the ski to base 64. So here is the encoded version of this key, And we're going to make the same API request with the encoded version of this, uh, key. Let's make the API request. And here we have, uh, a successful response with no results because we didn't have a search query. So let's add a question mark. Queue equals, I know there exists a company named Skippy. There are two of them. Okay, so I have the results. Uh, there are items, uh, items page, and the so on. So I managed to make the uh, API calls to the uh, UK company's database in the terminal. Let's do it in the application as they built previously for French data. So, uh, what we're going to change is going to company rb. Previously we did search France. Now we're going to do search uk. Uh, we're going to search Now name is not nice peram because the search not only by name, but like company registration number. So let's just say query base. URL is going to be this one. Yeah, it has the base URL for the API. Um, let's not add page per page and code it. The query. Yeah. Also, I will replace name with query everywhere. We don't need page and per page. And uh, we'll have, uh, this year is going to be base year l slash uh, companies slash search slash companies. So here slash companies query our encoded query. And, uh, let's see if this would work. Let's say query equals skip B. Let's try making this, uh, in the rails console. I already have a verday installed. Okay, so again, I have, uh, the authorization, uh, error. And to pass this authorization error with verday, I will need to have something like, uh, connection equals further day new, uh, do connection. And here I will say connection, headers, authorization and equals basic. And I'm going to add the, the encoded API key. So I'll need this, uh, API key and I will need to encode it. Let's say API key equals one that we got from the API. And here I will have basic and the base 64 strict and code 64 API key. Let's try running just this thing in the console. Yeah, we don't have API key defined. Okay, so this works. Let's try making the request. So we'll have, uh, response equals connection. Get URL, this URL results, and we have the results. So this looks, uh, good. Now let's, uh, go and, uh, make it work in our form in the, this form. So now we search for French companies. We need to make the request to search uk, not search France. Let's see that search. France is defined in the company's search controller. So instead of search France, I'm going to have search, uh, uk. I refresh, I'll search for Skippy, and it doesn't seem to be working. Let's see, the console will have an error. So the error is because, uh, we don't have results in our response. You see, we have items. So you see the APIs are slightly different, France and for the uk. So in search, double stream, EIB. Instead of having results, I will need to have, uh, items and the possibly little bit, not noncompliant, but name. Let's just see the results. So let's search for Skippy. And you see we have the UK company information. Let's, uh, select one of them and let's see what information we want to store. We want to store the, let's say address snippet, the company number. This is very important. And the name, yeah, in this case it is the title of the company. Okay, let's, uh, this page, just these results. So search for the company. Skip. You see, I have two results actually, let's search for anything you see, it always gives me the same results because here I forgot, uh, removing skip from the query. Yeah, this is a rookie mistake. Okay, let's search for any company. Let's search for Beckham like David Beckham. Okay, we see a few companies, uh, connected with him. Let's, uh, click on, uh, this funnel and the name of the company, the company registration number, and the, the address are populated. Let's create the company. Okay, so looks, uh, good. This is basically how you can use, uh, the company's house, uh, API for UK to search for different company information. Uh, one more thing that we can cover in the next episode would be to make a country selector. So you'd imagine you can select the country and search in the company, in the country's company registry. So you can search either in the UK registry or in the French registry. So I think this is a good topic for the next episode.
0
Join the conversation
Sign in to leave comments and save lessons to your watchlist.
We collect your email address, name and username to create your account. We do not share your email address with anyone else.